From c6d8c4013cc746ec4cd37e90ac192aa4c7a6b68f Mon Sep 17 00:00:00 2001 From: Alexandros Konstantinakis-Karmis Date: Mon, 23 Jul 2018 08:44:40 +0300 Subject: [PATCH] support definition of local endpoint for cleartext client session --- src/asio_client_session.cc | 18 ++++++++++++++ src/asio_client_session_tcp_impl.cc | 31 +++++++++++++++--------- src/asio_client_session_tcp_impl.h | 4 +++ src/includes/nghttp2/asio_http2_client.h | 11 +++++++++ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/asio_client_session.cc b/src/asio_client_session.cc index e142dedc..a5acffc7 100644 --- a/src/asio_client_session.cc +++ b/src/asio_client_session.cc @@ -44,6 +44,15 @@ session::session(boost::asio::io_service &io_service, const std::string &host, impl_->start_resolve(host, service); } +session::session(boost::asio::io_service &io_service, + const boost::asio::ip::tcp::endpoint &local_endpoint, + const std::string &host, const std::string &service) + : impl_(std::make_shared( + io_service, local_endpoint, host, service, + boost::posix_time::seconds(60))) { + impl_->start_resolve(host, service); +} + session::session(boost::asio::io_service &io_service, const std::string &host, const std::string &service, const boost::posix_time::time_duration &connect_timeout) @@ -52,6 +61,15 @@ session::session(boost::asio::io_service &io_service, const std::string &host, impl_->start_resolve(host, service); } +session::session(boost::asio::io_service &io_service, + const boost::asio::ip::tcp::endpoint &local_endpoint, + const std::string &host, const std::string &service, + const boost::posix_time::time_duration &connect_timeout) + : impl_(std::make_shared(io_service, local_endpoint, host, + service, connect_timeout)) { + impl_->start_resolve(host, service); +} + session::session(boost::asio::io_service &io_service, boost::asio::ssl::context &tls_ctx, const std::string &host, const std::string &service) diff --git a/src/asio_client_session_tcp_impl.cc b/src/asio_client_session_tcp_impl.cc index 77aa676e..9ef73b8f 100644 --- a/src/asio_client_session_tcp_impl.cc +++ b/src/asio_client_session_tcp_impl.cc @@ -34,24 +34,31 @@ session_tcp_impl::session_tcp_impl( const boost::posix_time::time_duration &connect_timeout) : session_impl(io_service, connect_timeout), socket_(io_service) {} +session_tcp_impl::session_tcp_impl( + boost::asio::io_service &io_service, + const boost::asio::ip::tcp::endpoint &local_endpoint, + const std::string &host, const std::string &service, + const boost::posix_time::time_duration &connect_timeout) + : session_impl(io_service, connect_timeout), + socket_(io_service, local_endpoint) {} + session_tcp_impl::~session_tcp_impl() {} void session_tcp_impl::start_connect(tcp::resolver::iterator endpoint_it) { auto self = shared_from_this(); - boost::asio::async_connect(socket_, endpoint_it, - [self](const boost::system::error_code &ec, - tcp::resolver::iterator endpoint_it) { - if (self->stopped()) { - return; - } + socket_.async_connect( + *endpoint_it, [self, endpoint_it](const boost::system::error_code &ec) { + if (self->stopped()) { + return; + } - if (ec) { - self->not_connected(ec); - return; - } + if (ec) { + self->not_connected(ec); + return; + } - self->connected(endpoint_it); - }); + self->connected(endpoint_it); + }); } tcp::socket &session_tcp_impl::socket() { return socket_; } diff --git a/src/asio_client_session_tcp_impl.h b/src/asio_client_session_tcp_impl.h index 0d1a48d5..0b6ae93f 100644 --- a/src/asio_client_session_tcp_impl.h +++ b/src/asio_client_session_tcp_impl.h @@ -40,6 +40,10 @@ public: session_tcp_impl(boost::asio::io_service &io_service, const std::string &host, const std::string &service, const boost::posix_time::time_duration &connect_timeout); + session_tcp_impl(boost::asio::io_service &io_service, + const boost::asio::ip::tcp::endpoint &local_endpoint, + const std::string &host, const std::string &service, + const boost::posix_time::time_duration &connect_timeout); virtual ~session_tcp_impl(); virtual void start_connect(tcp::resolver::iterator endpoint_it); diff --git a/src/includes/nghttp2/asio_http2_client.h b/src/includes/nghttp2/asio_http2_client.h index c6c1947b..7b256a76 100644 --- a/src/includes/nghttp2/asio_http2_client.h +++ b/src/includes/nghttp2/asio_http2_client.h @@ -150,6 +150,11 @@ public: session(boost::asio::io_service &io_service, const std::string &host, const std::string &service); + // Same as previous but with pegged local endpoint + session(boost::asio::io_service &io_service, + const boost::asio::ip::tcp::endpoint &local_endpoint, + const std::string &host, const std::string &service); + // Starts HTTP/2 session by connecting to |host| and |service| // (e.g., "80") using clear text TCP connection with given connect // timeout. @@ -157,6 +162,12 @@ public: const std::string &service, const boost::posix_time::time_duration &connect_timeout); + // Same as previous but with pegged local endpoint + session(boost::asio::io_service &io_service, + const boost::asio::ip::tcp::endpoint &local_endpoint, + const std::string &host, const std::string &service, + const boost::posix_time::time_duration &connect_timeout); + // Starts HTTP/2 session by connecting to |host| and |service| // (e.g., "443") using encrypted SSL/TLS connection with connect // timeout 60 seconds.