Skip to content

Commit 49d0245

Browse files
author
Valtteri Heikkila
committed
Refactored close() and cancel() to linux_connection class.
1 parent bc7abb3 commit 49d0245

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

Release/src/http/client/http_linux.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ namespace web { namespace http
7070
bool m_is_reused;
7171

7272
void handle_pool_timer(const boost::system::error_code& ec);
73+
74+
bool close()
75+
{
76+
boost::system::error_code error;
77+
m_socket.shutdown(tcp::socket::shutdown_both, error);
78+
m_socket.close(error);
79+
return !error;
80+
}
81+
82+
bool cancel()
83+
{
84+
boost::system::error_code error;
85+
m_socket.cancel(error);
86+
return !error;
87+
}
7388
};
7489

7590
class linux_connection_pool : public std::enable_shared_from_this<linux_connection_pool>
@@ -87,10 +102,7 @@ namespace web { namespace http
87102
for (auto& connection : m_connections)
88103
{
89104
connection->m_pool_timer.cancel();
90-
91-
boost::system::error_code error;
92-
connection->m_socket.shutdown(tcp::socket::shutdown_both, error);
93-
connection->m_socket.close(error);
105+
connection->close();
94106
}
95107
}
96108

@@ -213,12 +225,9 @@ namespace web { namespace http
213225
if (!ec)
214226
{
215227
m_timedout = true;
216-
217-
boost::system::error_code error;
218-
m_connection->m_socket.cancel(error);
219-
if (error)
228+
if (!m_connection->cancel())
220229
{
221-
report_error("Failed to cancel the socket", error);
230+
report_error("Failed to cancel the socket", boost::system::error_code());
222231
}
223232
}
224233
}
@@ -353,10 +362,8 @@ namespace web { namespace http
353362
{
354363
ctx->m_cancellationRegistration = request_ctx->m_request._cancellation_token().register_callback([ctx]()
355364
{
356-
boost::system::error_code error;
357-
ctx->m_connection->m_socket.cancel(error);
358-
ctx->m_connection->m_socket.shutdown(tcp::socket::shutdown_both, error);
359-
ctx->m_connection->m_socket.close(error);
365+
ctx->m_connection->cancel();
366+
ctx->m_connection->close();
360367
ctx->m_close_socket_in_destructor = true;
361368
});
362369
}
@@ -437,9 +444,7 @@ namespace web { namespace http
437444
{
438445
ctx->m_timeout_timer.cancel();
439446

440-
boost::system::error_code error;
441-
ctx->m_connection->m_socket.shutdown(tcp::socket::shutdown_both, error);
442-
ctx->m_connection->m_socket.close(error);
447+
ctx->m_connection->close();
443448
ctx->m_connection = m_pool->obtain();
444449

445450
auto endpoint = *endpoints;
@@ -694,9 +699,7 @@ namespace web { namespace http
694699
{
695700
// Connection was closed by the server for some reason during the connection was
696701
// being pooled. We re-send the request to get a new connection.
697-
boost::system::error_code error;
698-
ctx->m_connection->m_socket.shutdown(tcp::socket::shutdown_both, error);
699-
ctx->m_connection->m_socket.close(error);
702+
ctx->m_connection->close();
700703
ctx->m_close_socket_in_destructor = true;
701704

702705
auto new_ctx = details::linux_client_request_context::create_request_context(ctx->m_http_client, ctx->m_request);
@@ -1034,18 +1037,15 @@ namespace web { namespace http
10341037

10351038
linux_client_request_context::~linux_client_request_context()
10361039
{
1037-
boost::system::error_code error;
1038-
10391040
m_timeout_timer.cancel();
10401041

10411042
if (m_close_socket_in_destructor)
10421043
{
1043-
m_connection->m_socket.shutdown(tcp::socket::shutdown_both, error);
1044-
m_connection->m_socket.close(error);
1044+
m_connection->close();
10451045
}
10461046
else
10471047
{
1048-
m_connection->m_socket.cancel(error);
1048+
m_connection->cancel();
10491049
std::static_pointer_cast<linux_client>(m_http_client)->m_pool->release(m_connection);
10501050
}
10511051
}

0 commit comments

Comments
 (0)