diff --git a/src/asio_client_session.cc b/src/asio_client_session.cc index bc835c38..5762c4c5 100644 --- a/src/asio_client_session.cc +++ b/src/asio_client_session.cc @@ -39,15 +39,33 @@ using boost::asio::ip::tcp; session::session(boost::asio::io_service &io_service, const std::string &host, const std::string &service) - : impl_(std::make_shared(io_service, host, service)) { + : impl_(std::make_shared( + io_service, 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) + : impl_(std::make_shared(io_service, 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) + : impl_(std::make_shared( + io_service, tls_ctx, host, service, boost::posix_time::seconds(60))) { + 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, + const boost::posix_time::time_duration &connect_timeout) : impl_(std::make_shared(io_service, tls_ctx, host, - service)) { + service, connect_timeout)) { impl_->start_resolve(host, service); } @@ -97,10 +115,6 @@ const request *session::submit(boost::system::error_code &ec, return impl_->submit(ec, method, uri, std::move(cb), std::move(h)); } -void session::connect_timeout(const boost::posix_time::time_duration &t) { - impl_->connect_timeout(t); -} - void session::read_timeout(const boost::posix_time::time_duration &t) { impl_->read_timeout(t); } diff --git a/src/asio_client_session_impl.cc b/src/asio_client_session_impl.cc index fa275f8e..2ea65cd1 100644 --- a/src/asio_client_session_impl.cc +++ b/src/asio_client_session_impl.cc @@ -38,12 +38,14 @@ namespace nghttp2 { namespace asio_http2 { namespace client { -session_impl::session_impl(boost::asio::io_service &io_service) +session_impl::session_impl( + boost::asio::io_service &io_service, + const boost::posix_time::time_duration &connect_timeout) : wblen_(0), io_service_(io_service), resolver_(io_service), deadline_(io_service), - connect_timeout_(boost::posix_time::seconds(60)), + connect_timeout_(connect_timeout), read_timeout_(boost::posix_time::seconds(60)), session_(nullptr), data_pending_(nullptr), @@ -705,9 +707,7 @@ void session_impl::stop() { stopped_ = true; } -void session_impl::connect_timeout(const boost::posix_time::time_duration &t) { - connect_timeout_ = t; -} +bool session_impl::stopped() const { return stopped_; } void session_impl::read_timeout(const boost::posix_time::time_duration &t) { read_timeout_ = t; diff --git a/src/asio_client_session_impl.h b/src/asio_client_session_impl.h index 5e936c37..d62d252a 100644 --- a/src/asio_client_session_impl.h +++ b/src/asio_client_session_impl.h @@ -43,7 +43,8 @@ using boost::asio::ip::tcp; class session_impl : public std::enable_shared_from_this { public: - session_impl(boost::asio::io_service &io_service); + session_impl(boost::asio::io_service &io_service, + const boost::posix_time::time_duration &connect_timeout); virtual ~session_impl(); void start_resolve(const std::string &host, const std::string &service); @@ -91,10 +92,10 @@ public: void do_read(); void do_write(); - void connect_timeout(const boost::posix_time::time_duration &t); void read_timeout(const boost::posix_time::time_duration &t); void stop(); + bool stopped() const; protected: boost::array rb_; diff --git a/src/asio_client_session_tcp_impl.cc b/src/asio_client_session_tcp_impl.cc index 478f1c16..398ce767 100644 --- a/src/asio_client_session_tcp_impl.cc +++ b/src/asio_client_session_tcp_impl.cc @@ -28,10 +28,11 @@ namespace nghttp2 { namespace asio_http2 { namespace client { -session_tcp_impl::session_tcp_impl(boost::asio::io_service &io_service, - const std::string &host, - const std::string &service) - : session_impl(io_service), socket_(io_service) {} +session_tcp_impl::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_impl(io_service, connect_timeout), socket_(io_service) {} session_tcp_impl::~session_tcp_impl() {} @@ -39,6 +40,10 @@ void session_tcp_impl::start_connect(tcp::resolver::iterator endpoint_it) { boost::asio::async_connect(socket_, endpoint_it, [this](const boost::system::error_code &ec, tcp::resolver::iterator endpoint_it) { + if (stopped()) { + return; + } + if (ec) { not_connected(ec); return; diff --git a/src/asio_client_session_tcp_impl.h b/src/asio_client_session_tcp_impl.h index 6138828b..70ede36a 100644 --- a/src/asio_client_session_tcp_impl.h +++ b/src/asio_client_session_tcp_impl.h @@ -38,7 +38,8 @@ using boost::asio::ip::tcp; class session_tcp_impl : public session_impl { public: session_tcp_impl(boost::asio::io_service &io_service, const std::string &host, - const std::string &service); + 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/asio_client_session_tls_impl.cc b/src/asio_client_session_tls_impl.cc index 486c52d7..e37d7b02 100644 --- a/src/asio_client_session_tls_impl.cc +++ b/src/asio_client_session_tls_impl.cc @@ -29,11 +29,11 @@ namespace nghttp2 { namespace asio_http2 { namespace client { -session_tls_impl::session_tls_impl(boost::asio::io_service &io_service, - boost::asio::ssl::context &tls_ctx, - const std::string &host, - const std::string &service) - : session_impl(io_service), socket_(io_service, tls_ctx) { +session_tls_impl::session_tls_impl( + boost::asio::io_service &io_service, boost::asio::ssl::context &tls_ctx, + 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, tls_ctx) { // this callback setting is no effect is // ssl::context::set_verify_mode(boost::asio::ssl::verify_peer) is // not used, which is what we want. @@ -46,6 +46,10 @@ void session_tls_impl::start_connect(tcp::resolver::iterator endpoint_it) { boost::asio::async_connect( socket(), endpoint_it, [this](const boost::system::error_code &ec, tcp::resolver::iterator endpoint_it) { + if (stopped()) { + return; + } + if (ec) { not_connected(ec); return; @@ -54,6 +58,10 @@ void session_tls_impl::start_connect(tcp::resolver::iterator endpoint_it) { socket_.async_handshake( boost::asio::ssl::stream_base::client, [this, endpoint_it](const boost::system::error_code &ec) { + if (stopped()) { + return; + } + if (ec) { not_connected(ec); return; diff --git a/src/asio_client_session_tls_impl.h b/src/asio_client_session_tls_impl.h index 484e3d45..51106541 100644 --- a/src/asio_client_session_tls_impl.h +++ b/src/asio_client_session_tls_impl.h @@ -41,7 +41,8 @@ class session_tls_impl : public session_impl { public: session_tls_impl(boost::asio::io_service &io_service, boost::asio::ssl::context &tls_ctx, const std::string &host, - const std::string &service); + const std::string &service, + const boost::posix_time::time_duration &connect_timeout); virtual ~session_tls_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 55ef9f68..c3662e2e 100644 --- a/src/includes/nghttp2/asio_http2_client.h +++ b/src/includes/nghttp2/asio_http2_client.h @@ -123,15 +123,33 @@ class session_impl; class session { public: // Starts HTTP/2 session by connecting to |host| and |service| - // (e.g., "80") using clear text TCP connection. + // (e.g., "80") using clear text TCP connection with connect timeout + // 60 seconds. session(boost::asio::io_service &io_service, const std::string &host, const std::string &service); // Starts HTTP/2 session by connecting to |host| and |service| - // (e.g., "443") using encrypted SSL/TLS connection. + // (e.g., "80") using clear text TCP connection with given connect + // timeout. + session(boost::asio::io_service &io_service, 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. session(boost::asio::io_service &io_service, boost::asio::ssl::context &tls_context, const std::string &host, const std::string &service); + + // Starts HTTP/2 session by connecting to |host| and |service| + // (e.g., "443") using encrypted SSL/TLS connection with given + // connect timeout. + session(boost::asio::io_service &io_service, + boost::asio::ssl::context &tls_context, const std::string &host, + const std::string &service, + const boost::posix_time::time_duration &connect_timeout); + ~session(); session(session &&other) noexcept; @@ -144,9 +162,6 @@ public: // and session is terminated. void on_error(error_cb cb) const; - // Sets connect timeout, which defaults to 60 seconds. - void connect_timeout(const boost::posix_time::time_duration &t); - // Sets read timeout, which defaults to 60 seconds. void read_timeout(const boost::posix_time::time_duration &t);