@@ -56,6 +56,7 @@ namespace web { namespace http
56
56
class linux_connection_pool ;
57
57
class linux_connection : public std ::enable_shared_from_this<linux_connection>
58
58
{
59
+ friend class linux_connection_pool ;
59
60
public:
60
61
linux_connection (std::weak_ptr<linux_connection_pool> pool_weak, boost::asio::io_service& io_service) :
61
62
m_socket (io_service),
@@ -64,12 +65,23 @@ namespace web { namespace http
64
65
m_is_reused (false )
65
66
{}
66
67
68
+ void handle_pool_timer (const boost::system::error_code& ec);
69
+
70
+ tcp::socket& get_socket ()
71
+ {
72
+ return m_socket;
73
+ }
74
+
75
+ bool is_reused ()
76
+ {
77
+ return m_is_reused;
78
+ }
79
+
80
+ private:
67
81
std::weak_ptr<linux_connection_pool> m_pool_weak;
68
82
tcp::socket m_socket;
69
83
boost::asio::deadline_timer m_pool_timer;
70
84
bool m_is_reused;
71
-
72
- void handle_pool_timer (const boost::system::error_code& ec);
73
85
};
74
86
75
87
class linux_connection_pool : public std ::enable_shared_from_this<linux_connection_pool>
@@ -215,7 +227,7 @@ namespace web { namespace http
215
227
m_timedout = true ;
216
228
217
229
boost::system::error_code error;
218
- m_connection->m_socket .cancel (error);
230
+ m_connection->get_socket () .cancel (error);
219
231
if (error)
220
232
{
221
233
report_error (" Failed to cancel the socket" , error);
@@ -256,7 +268,7 @@ namespace web { namespace http
256
268
{
257
269
boost::asio::ssl::context context (boost::asio::ssl::context::sslv23);
258
270
context.set_default_verify_paths ();
259
- ctx->m_ssl_stream .reset (new boost::asio::ssl::stream<boost::asio::ip::tcp::socket &>(ctx->m_connection ->m_socket , context));
271
+ ctx->m_ssl_stream .reset (new boost::asio::ssl::stream<boost::asio::ip::tcp::socket &>(ctx->m_connection ->get_socket () , context));
260
272
}
261
273
262
274
auto encoded_resource = uri_builder (m_uri).append (ctx->m_request .relative_uri ()).to_uri ().resource ().to_string ();
@@ -331,7 +343,7 @@ namespace web { namespace http
331
343
332
344
ctx->set_timer (static_cast <int >(client_config ().timeout ().count ()));
333
345
334
- if (ctx->m_connection ->m_socket .is_open ())
346
+ if (ctx->m_connection ->get_socket () .is_open ())
335
347
{
336
348
write_request (ctx);
337
349
}
@@ -388,7 +400,7 @@ namespace web { namespace http
388
400
ctx->m_ssl_stream ->set_verify_mode (boost::asio::ssl::context::verify_none);
389
401
}
390
402
}
391
- ctx->m_connection ->m_socket .async_connect (endpoint, boost::bind (&linux_client::handle_connect, shared_from_this (), boost::asio::placeholders::error, ++endpoints, ctx));
403
+ ctx->m_connection ->get_socket () .async_connect (endpoint, boost::bind (&linux_client::handle_connect, shared_from_this (), boost::asio::placeholders::error, ++endpoints, ctx));
392
404
}
393
405
}
394
406
@@ -400,7 +412,7 @@ namespace web { namespace http
400
412
}
401
413
else
402
414
{
403
- boost::asio::async_write (ctx->m_connection ->m_socket , ctx->m_body_buf , boost::bind (&linux_client::handle_write_request, shared_from_this (), boost::asio::placeholders::error, ctx));
415
+ boost::asio::async_write (ctx->m_connection ->get_socket () , ctx->m_body_buf , boost::bind (&linux_client::handle_write_request, shared_from_this (), boost::asio::placeholders::error, ctx));
404
416
}
405
417
}
406
418
@@ -419,16 +431,16 @@ namespace web { namespace http
419
431
ctx->m_timeout_timer .cancel ();
420
432
421
433
boost::system::error_code error;
422
- ctx->m_connection ->m_socket .shutdown (tcp::socket::shutdown_both, error);
423
- ctx->m_connection ->m_socket .close (error);
434
+ ctx->m_connection ->get_socket () .shutdown (tcp::socket::shutdown_both, error);
435
+ ctx->m_connection ->get_socket () .close (error);
424
436
ctx->m_connection = m_pool->obtain ();
425
437
426
438
auto endpoint = *endpoints;
427
439
if (ctx->m_ssl_stream )
428
440
{
429
441
boost::asio::ssl::context context (boost::asio::ssl::context::sslv23);
430
442
context.set_default_verify_paths ();
431
- ctx->m_ssl_stream .reset (new boost::asio::ssl::stream<boost::asio::ip::tcp::socket &>(ctx->m_connection ->m_socket , context));
443
+ ctx->m_ssl_stream .reset (new boost::asio::ssl::stream<boost::asio::ip::tcp::socket &>(ctx->m_connection ->get_socket () , context));
432
444
433
445
// Check to turn off server certificate verification.
434
446
if (client_config ().validate_certificates ())
@@ -442,7 +454,7 @@ namespace web { namespace http
442
454
}
443
455
}
444
456
445
- ctx->m_connection ->m_socket .async_connect (endpoint, boost::bind (&linux_client::handle_connect, shared_from_this (), boost::asio::placeholders::error, ++endpoints, ctx));
457
+ ctx->m_connection ->get_socket () .async_connect (endpoint, boost::bind (&linux_client::handle_connect, shared_from_this (), boost::asio::placeholders::error, ++endpoints, ctx));
446
458
}
447
459
}
448
460
@@ -507,7 +519,7 @@ namespace web { namespace http
507
519
}
508
520
else
509
521
{
510
- boost::asio::async_write (ctx->m_connection ->m_socket , ctx->m_body_buf ,
522
+ boost::asio::async_write (ctx->m_connection ->get_socket () , ctx->m_body_buf ,
511
523
boost::bind (readSize != 0 ? &linux_client::handle_write_chunked_body : &linux_client::handle_write_body,
512
524
shared_from_this (), boost::asio::placeholders::error, ctx));
513
525
}
@@ -562,7 +574,7 @@ namespace web { namespace http
562
574
}
563
575
else
564
576
{
565
- boost::asio::async_write (ctx->m_connection ->m_socket , ctx->m_body_buf ,
577
+ boost::asio::async_write (ctx->m_connection ->get_socket () , ctx->m_body_buf ,
566
578
boost::bind (&linux_client::handle_write_large_body, shared_from_this (), boost::asio::placeholders::error, ctx));
567
579
}
568
580
});
@@ -615,7 +627,7 @@ namespace web { namespace http
615
627
}
616
628
else
617
629
{
618
- boost::asio::async_read_until (ctx->m_connection ->m_socket , ctx->m_body_buf , CRLF+CRLF,
630
+ boost::asio::async_read_until (ctx->m_connection ->get_socket () , ctx->m_body_buf , CRLF+CRLF,
619
631
boost::bind (&linux_client::handle_status_line, shared_from_this (), boost::asio::placeholders::error, ctx));
620
632
}
621
633
}
@@ -657,13 +669,13 @@ namespace web { namespace http
657
669
const bool socket_was_closed ((boost::asio::error::eof == ec)
658
670
|| (boost::asio::error::connection_reset == ec)
659
671
|| (boost::asio::error::connection_aborted == ec));
660
- if (socket_was_closed && ctx->m_connection ->m_is_reused && ctx->m_connection ->m_socket .is_open ())
672
+ if (socket_was_closed && ctx->m_connection ->is_reused () && ctx->m_connection ->get_socket () .is_open ())
661
673
{
662
674
// Connection was closed by the server for some reason during the connection was
663
675
// being pooled. We re-send the request to get a new connection.
664
676
boost::system::error_code error;
665
- ctx->m_connection ->m_socket .shutdown (tcp::socket::shutdown_both, error);
666
- ctx->m_connection ->m_socket .close (error);
677
+ ctx->m_connection ->get_socket () .shutdown (tcp::socket::shutdown_both, error);
678
+ ctx->m_connection ->get_socket () .close (error);
667
679
ctx->m_close_socket_in_destructor = true ;
668
680
669
681
auto new_ctx = details::linux_client_request_context::create_request_context (ctx->m_http_client , ctx->m_request );
@@ -756,7 +768,7 @@ namespace web { namespace http
756
768
}
757
769
else
758
770
{
759
- boost::asio::async_read_until (ctx->m_connection ->m_socket , ctx->m_body_buf , CRLF,
771
+ boost::asio::async_read_until (ctx->m_connection ->get_socket () , ctx->m_body_buf , CRLF,
760
772
boost::bind (&linux_client::handle_chunk_header, shared_from_this (), boost::asio::placeholders::error, ctx));
761
773
}
762
774
}
@@ -778,7 +790,7 @@ namespace web { namespace http
778
790
}
779
791
else
780
792
{
781
- boost::asio::async_read (ctx->m_connection ->m_socket , ctx->m_body_buf , boost::asio::transfer_at_least (size_to_read), handler);
793
+ boost::asio::async_read (ctx->m_connection ->get_socket () , ctx->m_body_buf , boost::asio::transfer_at_least (size_to_read), handler);
782
794
}
783
795
}
784
796
@@ -873,7 +885,7 @@ namespace web { namespace http
873
885
}
874
886
else
875
887
{
876
- boost::asio::async_read_until (ctx->m_connection ->m_socket , ctx->m_body_buf , CRLF,
888
+ boost::asio::async_read_until (ctx->m_connection ->get_socket () , ctx->m_body_buf , CRLF,
877
889
boost::bind (&linux_client::handle_chunk_header, shared_from_this (), boost::asio::placeholders::error, ctx));
878
890
}
879
891
});
@@ -1007,12 +1019,12 @@ namespace web { namespace http
1007
1019
1008
1020
if (m_close_socket_in_destructor)
1009
1021
{
1010
- m_connection->m_socket .shutdown (tcp::socket::shutdown_both, error);
1011
- m_connection->m_socket .close (error);
1022
+ m_connection->get_socket () .shutdown (tcp::socket::shutdown_both, error);
1023
+ m_connection->get_socket () .close (error);
1012
1024
}
1013
1025
else
1014
1026
{
1015
- m_connection->m_socket .cancel (error);
1027
+ m_connection->get_socket () .cancel (error);
1016
1028
std::static_pointer_cast<linux_client>(m_http_client)->m_pool ->release (m_connection);
1017
1029
}
1018
1030
}
0 commit comments