Close the socket after shutdown.
This commit is contained in:
@@ -208,7 +208,5 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
list_client.ListBooks();
|
list_client.ListBooks();
|
||||||
|
|
||||||
getchar();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-12
@@ -100,23 +100,31 @@ void HttpConnection::AsyncWrite() {
|
|||||||
// This is ensured by Asio.
|
// This is ensured by Asio.
|
||||||
void HttpConnection::WriteHandler(boost::system::error_code ec,
|
void HttpConnection::WriteHandler(boost::system::error_code ec,
|
||||||
std::size_t length) {
|
std::size_t length) {
|
||||||
if (!ec) {
|
|
||||||
LOG_INFO("Response has been sent back, length: %u.", length);
|
|
||||||
|
|
||||||
// Initiate graceful connection closure.
|
|
||||||
LOG_INFO("Close socket gracefully...");
|
|
||||||
// TODO: shutdown(both) should be identical to close().
|
|
||||||
boost::system::error_code ec;
|
|
||||||
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_ERRO("Failed to close socket.");
|
LOG_ERRO("Socket write error: %s", ec.message().c_str());
|
||||||
}
|
|
||||||
} else {
|
|
||||||
LOG_ERRO("Sending response error: %s", ec.message().c_str());
|
|
||||||
|
|
||||||
if (ec != boost::asio::error::operation_aborted) {
|
if (ec != boost::asio::error::operation_aborted) {
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
LOG_INFO("Response has been sent back, length: %u.", length);
|
||||||
|
|
||||||
|
Shutdown();
|
||||||
|
Close(); // Necessary even after shutdown!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Socket close VS. Shutdown:
|
||||||
|
// https://stackoverflow.com/questions/4160347/close-vs-shutdown-socket
|
||||||
|
void HttpConnection::Shutdown() {
|
||||||
|
LOG_INFO("Shutdown socket...");
|
||||||
|
|
||||||
|
// Initiate graceful connection closure.
|
||||||
|
boost::system::error_code ec;
|
||||||
|
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||||
|
|
||||||
|
if (ec) {
|
||||||
|
LOG_ERRO("Socket shutdown error: %s", ec.message().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,9 @@ class HttpConnection : public std::enable_shared_from_this<HttpConnection> {
|
|||||||
void AsyncWrite();
|
void AsyncWrite();
|
||||||
void WriteHandler(boost::system::error_code ec, std::size_t length);
|
void WriteHandler(boost::system::error_code ec, std::size_t length);
|
||||||
|
|
||||||
|
// Shutdown the socket.
|
||||||
|
void Shutdown();
|
||||||
|
|
||||||
// Socket for the connection.
|
// Socket for the connection.
|
||||||
boost::asio::ip::tcp::socket socket_;
|
boost::asio::ip::tcp::socket socket_;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user