Do async-resove in ctor
This commit is contained in:
parent
dd741a58ae
commit
0676535caf
|
@ -62,10 +62,7 @@ int main(int argc, char *argv[]) {
|
||||||
boost::asio::ssl::context tls_ctx(boost::asio::ssl::context::sslv23);
|
boost::asio::ssl::context tls_ctx(boost::asio::ssl::context::sslv23);
|
||||||
configure_tls_context(tls_ctx);
|
configure_tls_context(tls_ctx);
|
||||||
|
|
||||||
tcp::resolver resolver(io_service);
|
session sess(io_service, tls_ctx, "localhost", "3000");
|
||||||
auto endpoint_it = resolver.resolve({"localhost", "3000"});
|
|
||||||
|
|
||||||
session sess(io_service, tls_ctx, endpoint_it);
|
|
||||||
sess.on_connect([&sess]() {
|
sess.on_connect([&sess]() {
|
||||||
std::cerr << "connected" << std::endl;
|
std::cerr << "connected" << std::endl;
|
||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
|
|
|
@ -37,14 +37,15 @@ namespace client {
|
||||||
|
|
||||||
using boost::asio::ip::tcp;
|
using boost::asio::ip::tcp;
|
||||||
|
|
||||||
session::session(boost::asio::io_service &io_service,
|
session::session(boost::asio::io_service &io_service, const std::string &host,
|
||||||
tcp::resolver::iterator endpoint_it)
|
const std::string &service)
|
||||||
: impl_(make_unique<session_tcp_impl>(io_service, endpoint_it)) {}
|
: impl_(make_unique<session_tcp_impl>(io_service, host, service)) {}
|
||||||
|
|
||||||
session::session(boost::asio::io_service &io_service,
|
session::session(boost::asio::io_service &io_service,
|
||||||
boost::asio::ssl::context &tls_ctx,
|
boost::asio::ssl::context &tls_ctx, const std::string &host,
|
||||||
tcp::resolver::iterator endpoint_it)
|
const std::string &service)
|
||||||
: impl_(make_unique<session_tls_impl>(io_service, tls_ctx, endpoint_it)) {}
|
: impl_(make_unique<session_tls_impl>(io_service, tls_ctx, host, service)) {
|
||||||
|
}
|
||||||
|
|
||||||
session::~session() {}
|
session::~session() {}
|
||||||
|
|
||||||
|
@ -54,6 +55,8 @@ void session::on_error(error_cb cb) { impl_->on_error(std::move(cb)); }
|
||||||
|
|
||||||
void session::shutdown() { impl_->shutdown(); }
|
void session::shutdown() { impl_->shutdown(); }
|
||||||
|
|
||||||
|
boost::asio::io_service &session::io_service() { return impl_->io_service(); }
|
||||||
|
|
||||||
request *session::submit(boost::system::error_code &ec,
|
request *session::submit(boost::system::error_code &ec,
|
||||||
const std::string &method, const std::string &uri,
|
const std::string &method, const std::string &uri,
|
||||||
header_map h) {
|
header_map h) {
|
||||||
|
|
|
@ -38,8 +38,9 @@ namespace nghttp2 {
|
||||||
namespace asio_http2 {
|
namespace asio_http2 {
|
||||||
namespace client {
|
namespace client {
|
||||||
|
|
||||||
session_impl::session_impl()
|
session_impl::session_impl(boost::asio::io_service &io_service)
|
||||||
: wblen_(0), session_(nullptr), data_pending_(nullptr), data_pendinglen_(0),
|
: wblen_(0), io_service_(io_service), resolver_(io_service),
|
||||||
|
session_(nullptr), data_pending_(nullptr), data_pendinglen_(0),
|
||||||
writing_(false), inside_callback_(false) {}
|
writing_(false), inside_callback_(false) {}
|
||||||
|
|
||||||
session_impl::~session_impl() {
|
session_impl::~session_impl() {
|
||||||
|
@ -53,6 +54,20 @@ session_impl::~session_impl() {
|
||||||
nghttp2_session_del(session_);
|
nghttp2_session_del(session_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void session_impl::start_resolve(const std::string &host,
|
||||||
|
const std::string &service) {
|
||||||
|
resolver_.async_resolve({host, service},
|
||||||
|
[this](const boost::system::error_code &ec,
|
||||||
|
tcp::resolver::iterator endpoint_it) {
|
||||||
|
if (ec) {
|
||||||
|
not_connected(ec);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
start_connect(endpoint_it);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void session_impl::connected() {
|
void session_impl::connected() {
|
||||||
if (!setup_session()) {
|
if (!setup_session()) {
|
||||||
return;
|
return;
|
||||||
|
@ -411,6 +426,8 @@ void session_impl::shutdown() {
|
||||||
signal_write();
|
signal_write();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boost::asio::io_service &session_impl::io_service() { return io_service_; }
|
||||||
|
|
||||||
void session_impl::signal_write() {
|
void session_impl::signal_write() {
|
||||||
if (!inside_callback_) {
|
if (!inside_callback_) {
|
||||||
do_write();
|
do_write();
|
||||||
|
|
|
@ -45,9 +45,11 @@ using boost::asio::ip::tcp;
|
||||||
|
|
||||||
class session_impl {
|
class session_impl {
|
||||||
public:
|
public:
|
||||||
session_impl();
|
session_impl(boost::asio::io_service &io_service);
|
||||||
virtual ~session_impl();
|
virtual ~session_impl();
|
||||||
|
|
||||||
|
void start_resolve(const std::string &host, const std::string &service);
|
||||||
|
|
||||||
void connected();
|
void connected();
|
||||||
void not_connected(const boost::system::error_code &ec);
|
void not_connected(const boost::system::error_code &ec);
|
||||||
|
|
||||||
|
@ -67,6 +69,7 @@ public:
|
||||||
request *submit(boost::system::error_code &ec, const std::string &method,
|
request *submit(boost::system::error_code &ec, const std::string &method,
|
||||||
const std::string &uri, read_cb cb, header_map h);
|
const std::string &uri, read_cb cb, header_map h);
|
||||||
|
|
||||||
|
virtual void start_connect(tcp::resolver::iterator endpoint_it) = 0;
|
||||||
virtual tcp::socket &socket() = 0;
|
virtual tcp::socket &socket() = 0;
|
||||||
virtual void read_socket(std::function<
|
virtual void read_socket(std::function<
|
||||||
void(const boost::system::error_code &ec, std::size_t n)> h) = 0;
|
void(const boost::system::error_code &ec, std::size_t n)> h) = 0;
|
||||||
|
@ -76,6 +79,8 @@ public:
|
||||||
|
|
||||||
void shutdown();
|
void shutdown();
|
||||||
|
|
||||||
|
boost::asio::io_service &io_service();
|
||||||
|
|
||||||
void signal_write();
|
void signal_write();
|
||||||
|
|
||||||
void enter_callback();
|
void enter_callback();
|
||||||
|
@ -93,6 +98,9 @@ private:
|
||||||
bool should_stop() const;
|
bool should_stop() const;
|
||||||
bool setup_session();
|
bool setup_session();
|
||||||
|
|
||||||
|
boost::asio::io_service &io_service_;
|
||||||
|
tcp::resolver resolver_;
|
||||||
|
|
||||||
std::map<int32_t, std::unique_ptr<stream>> streams_;
|
std::map<int32_t, std::unique_ptr<stream>> streams_;
|
||||||
|
|
||||||
void_cb connect_cb_;
|
void_cb connect_cb_;
|
||||||
|
|
|
@ -29,22 +29,27 @@ 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(boost::asio::io_service &io_service,
|
||||||
tcp::resolver::iterator endpoint_it)
|
const std::string &host,
|
||||||
: socket_(io_service) {
|
const std::string &service)
|
||||||
|
: session_impl(io_service), socket_(io_service) {
|
||||||
boost::asio::async_connect(socket_, endpoint_it,
|
start_resolve(host, service);
|
||||||
[this](boost::system::error_code ec,
|
|
||||||
tcp::resolver::iterator endpoint_it) {
|
|
||||||
if (!ec) {
|
|
||||||
connected();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
not_connected(ec);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
session_tcp_impl::~session_tcp_impl() {}
|
session_tcp_impl::~session_tcp_impl() {}
|
||||||
|
|
||||||
|
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 (ec) {
|
||||||
|
not_connected(ec);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
connected();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
tcp::socket &session_tcp_impl::socket() { return socket_; }
|
tcp::socket &session_tcp_impl::socket() { return socket_; }
|
||||||
|
|
||||||
void session_tcp_impl::read_socket(
|
void session_tcp_impl::read_socket(
|
||||||
|
|
|
@ -39,10 +39,11 @@ 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,
|
session_tcp_impl(boost::asio::io_service &io_service, const std::string &host,
|
||||||
tcp::resolver::iterator endpoint_it);
|
const std::string &service);
|
||||||
virtual ~session_tcp_impl();
|
virtual ~session_tcp_impl();
|
||||||
|
|
||||||
|
virtual void start_connect(tcp::resolver::iterator endpoint_it);
|
||||||
virtual tcp::socket &socket();
|
virtual tcp::socket &socket();
|
||||||
virtual void read_socket(std::function<
|
virtual void read_socket(std::function<
|
||||||
void(const boost::system::error_code &ec, std::size_t n)> h);
|
void(const boost::system::error_code &ec, std::size_t n)> h);
|
||||||
|
|
|
@ -30,11 +30,17 @@ namespace client {
|
||||||
|
|
||||||
session_tls_impl::session_tls_impl(boost::asio::io_service &io_service,
|
session_tls_impl::session_tls_impl(boost::asio::io_service &io_service,
|
||||||
boost::asio::ssl::context &tls_ctx,
|
boost::asio::ssl::context &tls_ctx,
|
||||||
tcp::resolver::iterator endpoint_it)
|
const std::string &host,
|
||||||
: socket_(io_service, tls_ctx) {
|
const std::string &service)
|
||||||
|
: session_impl(io_service), socket_(io_service, tls_ctx) {
|
||||||
|
start_resolve(host, service);
|
||||||
|
}
|
||||||
|
|
||||||
|
session_tls_impl::~session_tls_impl() {}
|
||||||
|
|
||||||
|
void session_tls_impl::start_connect(tcp::resolver::iterator endpoint_it) {
|
||||||
boost::asio::async_connect(socket(), endpoint_it,
|
boost::asio::async_connect(socket(), endpoint_it,
|
||||||
[this](boost::system::error_code ec,
|
[this](const boost::system::error_code &ec,
|
||||||
tcp::resolver::iterator endpoint_it) {
|
tcp::resolver::iterator endpoint_it) {
|
||||||
if (ec) {
|
if (ec) {
|
||||||
not_connected(ec);
|
not_connected(ec);
|
||||||
|
@ -52,8 +58,6 @@ session_tls_impl::session_tls_impl(boost::asio::io_service &io_service,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
session_tls_impl::~session_tls_impl() {}
|
|
||||||
|
|
||||||
tcp::socket &session_tls_impl::socket() { return socket_.next_layer(); }
|
tcp::socket &session_tls_impl::socket() { return socket_.next_layer(); }
|
||||||
|
|
||||||
void session_tls_impl::read_socket(
|
void session_tls_impl::read_socket(
|
||||||
|
|
|
@ -42,10 +42,11 @@ using ssl_socket = boost::asio::ssl::stream<tcp::socket>;
|
||||||
class session_tls_impl : public session_impl {
|
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,
|
boost::asio::ssl::context &tls_ctx, const std::string &host,
|
||||||
tcp::resolver::iterator endpoint_it);
|
const std::string &service);
|
||||||
virtual ~session_tls_impl();
|
virtual ~session_tls_impl();
|
||||||
|
|
||||||
|
virtual void start_connect(tcp::resolver::iterator endpoint_it);
|
||||||
virtual tcp::socket &socket();
|
virtual tcp::socket &socket();
|
||||||
virtual void read_socket(std::function<
|
virtual void read_socket(std::function<
|
||||||
void(const boost::system::error_code &ec, std::size_t n)> h);
|
void(const boost::system::error_code &ec, std::size_t n)> h);
|
||||||
|
|
|
@ -337,11 +337,11 @@ class session_impl;
|
||||||
|
|
||||||
class session {
|
class session {
|
||||||
public:
|
public:
|
||||||
|
session(boost::asio::io_service &io_service, const std::string &host,
|
||||||
|
const std::string &service);
|
||||||
session(boost::asio::io_service &io_service,
|
session(boost::asio::io_service &io_service,
|
||||||
boost::asio::ip::tcp::resolver::iterator endpoint_it);
|
boost::asio::ssl::context &tls_context, const std::string &host,
|
||||||
session(boost::asio::io_service &io_service,
|
const std::string &service);
|
||||||
boost::asio::ssl::context &tls_context,
|
|
||||||
boost::asio::ip::tcp::resolver::iterator endpoint_it);
|
|
||||||
~session();
|
~session();
|
||||||
|
|
||||||
void on_connect(void_cb cb);
|
void on_connect(void_cb cb);
|
||||||
|
@ -349,6 +349,8 @@ public:
|
||||||
|
|
||||||
void shutdown();
|
void shutdown();
|
||||||
|
|
||||||
|
boost::asio::io_service &io_service();
|
||||||
|
|
||||||
request *submit(boost::system::error_code &ec, const std::string &method,
|
request *submit(boost::system::error_code &ec, const std::string &method,
|
||||||
const std::string &uri, header_map h = {});
|
const std::string &uri, header_map h = {});
|
||||||
request *submit(boost::system::error_code &ec, const std::string &method,
|
request *submit(boost::system::error_code &ec, const std::string &method,
|
||||||
|
|
Loading…
Reference in New Issue