@@ -70,6 +70,21 @@ namespace web { namespace http
70
70
bool m_is_reused;
71
71
72
72
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
+ }
73
88
};
74
89
75
90
class linux_connection_pool : public std ::enable_shared_from_this<linux_connection_pool>
@@ -87,10 +102,7 @@ namespace web { namespace http
87
102
for (auto & connection : m_connections)
88
103
{
89
104
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 ();
94
106
}
95
107
}
96
108
@@ -213,12 +225,9 @@ namespace web { namespace http
213
225
if (!ec)
214
226
{
215
227
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 ())
220
229
{
221
- report_error (" Failed to cancel the socket" , error );
230
+ report_error (" Failed to cancel the socket" , boost::system::error_code () );
222
231
}
223
232
}
224
233
}
@@ -353,10 +362,8 @@ namespace web { namespace http
353
362
{
354
363
ctx->m_cancellationRegistration = request_ctx->m_request ._cancellation_token ().register_callback ([ctx]()
355
364
{
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 ();
360
367
ctx->m_close_socket_in_destructor = true ;
361
368
});
362
369
}
@@ -437,9 +444,7 @@ namespace web { namespace http
437
444
{
438
445
ctx->m_timeout_timer .cancel ();
439
446
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 ();
443
448
ctx->m_connection = m_pool->obtain ();
444
449
445
450
auto endpoint = *endpoints;
@@ -694,9 +699,7 @@ namespace web { namespace http
694
699
{
695
700
// Connection was closed by the server for some reason during the connection was
696
701
// 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 ();
700
703
ctx->m_close_socket_in_destructor = true ;
701
704
702
705
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
1034
1037
1035
1038
linux_client_request_context::~linux_client_request_context ()
1036
1039
{
1037
- boost::system::error_code error;
1038
-
1039
1040
m_timeout_timer.cancel ();
1040
1041
1041
1042
if (m_close_socket_in_destructor)
1042
1043
{
1043
- m_connection->m_socket .shutdown (tcp::socket::shutdown_both, error);
1044
- m_connection->m_socket .close (error);
1044
+ m_connection->close ();
1045
1045
}
1046
1046
else
1047
1047
{
1048
- m_connection->m_socket . cancel (error );
1048
+ m_connection->cancel ();
1049
1049
std::static_pointer_cast<linux_client>(m_http_client)->m_pool ->release (m_connection);
1050
1050
}
1051
1051
}
0 commit comments