asio: client: Fix connect timeout does not work, return from cb if session stopped
This change removed client::session::connect_timeout() functon, and connect timeout should be given to client::session constructor. This is required since async operation starts in the constructor.
This commit is contained in:
parent
231665d67b
commit
95ca4f55d5
|
@ -39,15 +39,33 @@ using boost::asio::ip::tcp;
|
||||||
|
|
||||||
session::session(boost::asio::io_service &io_service, const std::string &host,
|
session::session(boost::asio::io_service &io_service, const std::string &host,
|
||||||
const std::string &service)
|
const std::string &service)
|
||||||
: impl_(std::make_shared<session_tcp_impl>(io_service, host, service)) {
|
: impl_(std::make_shared<session_tcp_impl>(
|
||||||
|
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<session_tcp_impl>(io_service, host, service,
|
||||||
|
connect_timeout)) {
|
||||||
impl_->start_resolve(host, service);
|
impl_->start_resolve(host, service);
|
||||||
}
|
}
|
||||||
|
|
||||||
session::session(boost::asio::io_service &io_service,
|
session::session(boost::asio::io_service &io_service,
|
||||||
boost::asio::ssl::context &tls_ctx, const std::string &host,
|
boost::asio::ssl::context &tls_ctx, const std::string &host,
|
||||||
const std::string &service)
|
const std::string &service)
|
||||||
|
: impl_(std::make_shared<session_tls_impl>(
|
||||||
|
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<session_tls_impl>(io_service, tls_ctx, host,
|
: impl_(std::make_shared<session_tls_impl>(io_service, tls_ctx, host,
|
||||||
service)) {
|
service, connect_timeout)) {
|
||||||
impl_->start_resolve(host, service);
|
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));
|
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) {
|
void session::read_timeout(const boost::posix_time::time_duration &t) {
|
||||||
impl_->read_timeout(t);
|
impl_->read_timeout(t);
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,12 +38,14 @@ namespace nghttp2 {
|
||||||
namespace asio_http2 {
|
namespace asio_http2 {
|
||||||
namespace client {
|
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),
|
: wblen_(0),
|
||||||
io_service_(io_service),
|
io_service_(io_service),
|
||||||
resolver_(io_service),
|
resolver_(io_service),
|
||||||
deadline_(io_service),
|
deadline_(io_service),
|
||||||
connect_timeout_(boost::posix_time::seconds(60)),
|
connect_timeout_(connect_timeout),
|
||||||
read_timeout_(boost::posix_time::seconds(60)),
|
read_timeout_(boost::posix_time::seconds(60)),
|
||||||
session_(nullptr),
|
session_(nullptr),
|
||||||
data_pending_(nullptr),
|
data_pending_(nullptr),
|
||||||
|
@ -705,9 +707,7 @@ void session_impl::stop() {
|
||||||
stopped_ = true;
|
stopped_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void session_impl::connect_timeout(const boost::posix_time::time_duration &t) {
|
bool session_impl::stopped() const { return stopped_; }
|
||||||
connect_timeout_ = t;
|
|
||||||
}
|
|
||||||
|
|
||||||
void session_impl::read_timeout(const boost::posix_time::time_duration &t) {
|
void session_impl::read_timeout(const boost::posix_time::time_duration &t) {
|
||||||
read_timeout_ = t;
|
read_timeout_ = t;
|
||||||
|
|
|
@ -43,7 +43,8 @@ using boost::asio::ip::tcp;
|
||||||
|
|
||||||
class session_impl : public std::enable_shared_from_this<session_impl> {
|
class session_impl : public std::enable_shared_from_this<session_impl> {
|
||||||
public:
|
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();
|
virtual ~session_impl();
|
||||||
|
|
||||||
void start_resolve(const std::string &host, const std::string &service);
|
void start_resolve(const std::string &host, const std::string &service);
|
||||||
|
@ -91,10 +92,10 @@ public:
|
||||||
void do_read();
|
void do_read();
|
||||||
void do_write();
|
void do_write();
|
||||||
|
|
||||||
void connect_timeout(const boost::posix_time::time_duration &t);
|
|
||||||
void read_timeout(const boost::posix_time::time_duration &t);
|
void read_timeout(const boost::posix_time::time_duration &t);
|
||||||
|
|
||||||
void stop();
|
void stop();
|
||||||
|
bool stopped() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
boost::array<uint8_t, 8_k> rb_;
|
boost::array<uint8_t, 8_k> rb_;
|
||||||
|
|
|
@ -28,10 +28,11 @@ namespace nghttp2 {
|
||||||
namespace asio_http2 {
|
namespace asio_http2 {
|
||||||
namespace client {
|
namespace client {
|
||||||
|
|
||||||
session_tcp_impl::session_tcp_impl(boost::asio::io_service &io_service,
|
session_tcp_impl::session_tcp_impl(
|
||||||
const std::string &host,
|
boost::asio::io_service &io_service, const std::string &host,
|
||||||
const std::string &service)
|
const std::string &service,
|
||||||
: session_impl(io_service), socket_(io_service) {}
|
const boost::posix_time::time_duration &connect_timeout)
|
||||||
|
: session_impl(io_service, connect_timeout), socket_(io_service) {}
|
||||||
|
|
||||||
session_tcp_impl::~session_tcp_impl() {}
|
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,
|
boost::asio::async_connect(socket_, endpoint_it,
|
||||||
[this](const boost::system::error_code &ec,
|
[this](const boost::system::error_code &ec,
|
||||||
tcp::resolver::iterator endpoint_it) {
|
tcp::resolver::iterator endpoint_it) {
|
||||||
|
if (stopped()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (ec) {
|
if (ec) {
|
||||||
not_connected(ec);
|
not_connected(ec);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -38,7 +38,8 @@ using boost::asio::ip::tcp;
|
||||||
class session_tcp_impl : public session_impl {
|
class session_tcp_impl : public session_impl {
|
||||||
public:
|
public:
|
||||||
session_tcp_impl(boost::asio::io_service &io_service, const std::string &host,
|
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 ~session_tcp_impl();
|
||||||
|
|
||||||
virtual void start_connect(tcp::resolver::iterator endpoint_it);
|
virtual void start_connect(tcp::resolver::iterator endpoint_it);
|
||||||
|
|
|
@ -29,11 +29,11 @@ namespace nghttp2 {
|
||||||
namespace asio_http2 {
|
namespace asio_http2 {
|
||||||
namespace client {
|
namespace client {
|
||||||
|
|
||||||
session_tls_impl::session_tls_impl(boost::asio::io_service &io_service,
|
session_tls_impl::session_tls_impl(
|
||||||
boost::asio::ssl::context &tls_ctx,
|
boost::asio::io_service &io_service, boost::asio::ssl::context &tls_ctx,
|
||||||
const std::string &host,
|
const std::string &host, const std::string &service,
|
||||||
const std::string &service)
|
const boost::posix_time::time_duration &connect_timeout)
|
||||||
: session_impl(io_service), socket_(io_service, tls_ctx) {
|
: session_impl(io_service, connect_timeout), socket_(io_service, tls_ctx) {
|
||||||
// this callback setting is no effect is
|
// this callback setting is no effect is
|
||||||
// ssl::context::set_verify_mode(boost::asio::ssl::verify_peer) is
|
// ssl::context::set_verify_mode(boost::asio::ssl::verify_peer) is
|
||||||
// not used, which is what we want.
|
// 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(
|
boost::asio::async_connect(
|
||||||
socket(), endpoint_it, [this](const boost::system::error_code &ec,
|
socket(), endpoint_it, [this](const boost::system::error_code &ec,
|
||||||
tcp::resolver::iterator endpoint_it) {
|
tcp::resolver::iterator endpoint_it) {
|
||||||
|
if (stopped()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (ec) {
|
if (ec) {
|
||||||
not_connected(ec);
|
not_connected(ec);
|
||||||
return;
|
return;
|
||||||
|
@ -54,6 +58,10 @@ void session_tls_impl::start_connect(tcp::resolver::iterator endpoint_it) {
|
||||||
socket_.async_handshake(
|
socket_.async_handshake(
|
||||||
boost::asio::ssl::stream_base::client,
|
boost::asio::ssl::stream_base::client,
|
||||||
[this, endpoint_it](const boost::system::error_code &ec) {
|
[this, endpoint_it](const boost::system::error_code &ec) {
|
||||||
|
if (stopped()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (ec) {
|
if (ec) {
|
||||||
not_connected(ec);
|
not_connected(ec);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -41,7 +41,8 @@ class session_tls_impl : public session_impl {
|
||||||
public:
|
public:
|
||||||
session_tls_impl(boost::asio::io_service &io_service,
|
session_tls_impl(boost::asio::io_service &io_service,
|
||||||
boost::asio::ssl::context &tls_ctx, const std::string &host,
|
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 ~session_tls_impl();
|
||||||
|
|
||||||
virtual void start_connect(tcp::resolver::iterator endpoint_it);
|
virtual void start_connect(tcp::resolver::iterator endpoint_it);
|
||||||
|
|
|
@ -123,15 +123,33 @@ class session_impl;
|
||||||
class session {
|
class session {
|
||||||
public:
|
public:
|
||||||
// Starts HTTP/2 session by connecting to |host| and |service|
|
// 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,
|
session(boost::asio::io_service &io_service, const std::string &host,
|
||||||
const std::string &service);
|
const std::string &service);
|
||||||
|
|
||||||
// Starts HTTP/2 session by connecting to |host| and |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,
|
session(boost::asio::io_service &io_service,
|
||||||
boost::asio::ssl::context &tls_context, const std::string &host,
|
boost::asio::ssl::context &tls_context, const std::string &host,
|
||||||
const std::string &service);
|
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(session &&other) noexcept;
|
session(session &&other) noexcept;
|
||||||
|
@ -144,9 +162,6 @@ public:
|
||||||
// and session is terminated.
|
// and session is terminated.
|
||||||
void on_error(error_cb cb) const;
|
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.
|
// Sets read timeout, which defaults to 60 seconds.
|
||||||
void read_timeout(const boost::posix_time::time_duration &t);
|
void read_timeout(const boost::posix_time::time_duration &t);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue