From 76eab3faa0adbc2217a56629b2eb8fe7c9eb0ff1 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 4 Mar 2015 01:23:35 +0900 Subject: [PATCH] asio: Pass connected address to connect_cb --- examples/asio-cl.cc | 4 ++-- src/asio_client_session.cc | 2 +- src/asio_client_session_impl.cc | 8 ++++---- src/asio_client_session_impl.h | 8 ++++---- src/asio_client_session_tcp_impl.cc | 2 +- src/asio_client_session_tls_impl.cc | 17 +++++++++-------- src/includes/nghttp2/asio_http2.h | 4 +++- 7 files changed, 24 insertions(+), 21 deletions(-) diff --git a/examples/asio-cl.cc b/examples/asio-cl.cc index 7fa7ce44..8a7c6627 100644 --- a/examples/asio-cl.cc +++ b/examples/asio-cl.cc @@ -69,8 +69,8 @@ int main(int argc, char *argv[]) { configure_tls_context(tls_ctx); session sess(io_service, tls_ctx, "localhost", "3000"); - sess.on_connect([&sess]() { - std::cerr << "connected" << std::endl; + sess.on_connect([&sess](tcp::resolver::iterator endpoint_it) { + std::cerr << "connected to " << (*endpoint_it).endpoint() << std::endl; boost::system::error_code ec; auto req = sess.submit(ec, "GET", "https://localhost:3000/", "hello world", {{"cookie", {"foobar", true}}}); diff --git a/src/asio_client_session.cc b/src/asio_client_session.cc index 046a48a6..e5956a8d 100644 --- a/src/asio_client_session.cc +++ b/src/asio_client_session.cc @@ -49,7 +49,7 @@ session::session(boost::asio::io_service &io_service, session::~session() {} -void session::on_connect(void_cb cb) { impl_->on_connect(std::move(cb)); } +void session::on_connect(connect_cb cb) { impl_->on_connect(std::move(cb)); } void session::on_error(error_cb cb) { impl_->on_error(std::move(cb)); } diff --git a/src/asio_client_session_impl.cc b/src/asio_client_session_impl.cc index 3292d51e..c0773270 100644 --- a/src/asio_client_session_impl.cc +++ b/src/asio_client_session_impl.cc @@ -68,7 +68,7 @@ void session_impl::start_resolve(const std::string &host, }); } -void session_impl::connected() { +void session_impl::connected(tcp::resolver::iterator endpoint_it) { if (!setup_session()) { return; } @@ -84,7 +84,7 @@ void session_impl::connected() { auto &connect_cb = on_connect(); if (connect_cb) { - connect_cb(); + connect_cb(endpoint_it); } } @@ -95,11 +95,11 @@ void session_impl::not_connected(const boost::system::error_code &ec) { } } -void session_impl::on_connect(void_cb cb) { connect_cb_ = std::move(cb); } +void session_impl::on_connect(connect_cb cb) { connect_cb_ = std::move(cb); } void session_impl::on_error(error_cb cb) { error_cb_ = std::move(cb); } -const void_cb &session_impl::on_connect() const { return connect_cb_; } +const connect_cb &session_impl::on_connect() const { return connect_cb_; } const error_cb &session_impl::on_error() const { return error_cb_; } diff --git a/src/asio_client_session_impl.h b/src/asio_client_session_impl.h index 913998cd..3746cf93 100644 --- a/src/asio_client_session_impl.h +++ b/src/asio_client_session_impl.h @@ -50,13 +50,13 @@ public: void start_resolve(const std::string &host, const std::string &service); - void connected(); + void connected(tcp::resolver::iterator endpoint_it); void not_connected(const boost::system::error_code &ec); - void on_connect(void_cb cb); + void on_connect(connect_cb cb); void on_error(error_cb cb); - const void_cb &on_connect() const; + const connect_cb &on_connect() const; const error_cb &on_error() const; void cancel(stream &strm); @@ -104,7 +104,7 @@ private: std::map> streams_; - void_cb connect_cb_; + connect_cb connect_cb_; error_cb error_cb_; nghttp2_session *session_; diff --git a/src/asio_client_session_tcp_impl.cc b/src/asio_client_session_tcp_impl.cc index db65d177..41195bad 100644 --- a/src/asio_client_session_tcp_impl.cc +++ b/src/asio_client_session_tcp_impl.cc @@ -46,7 +46,7 @@ void session_tcp_impl::start_connect(tcp::resolver::iterator endpoint_it) { return; } - connected(); + connected(endpoint_it); }); } diff --git a/src/asio_client_session_tls_impl.cc b/src/asio_client_session_tls_impl.cc index 3a5182f1..cde2ab31 100644 --- a/src/asio_client_session_tls_impl.cc +++ b/src/asio_client_session_tls_impl.cc @@ -52,14 +52,15 @@ void session_tls_impl::start_connect(tcp::resolver::iterator endpoint_it) { return; } - socket_.async_handshake(boost::asio::ssl::stream_base::client, - [this](const boost::system::error_code &ec) { - if (ec) { - not_connected(ec); - return; - } - connected(); - }); + socket_.async_handshake( + boost::asio::ssl::stream_base::client, + [this, endpoint_it](const boost::system::error_code &ec) { + if (ec) { + not_connected(ec); + return; + } + connected(endpoint_it); + }); }); } diff --git a/src/includes/nghttp2/asio_http2.h b/src/includes/nghttp2/asio_http2.h index 5f029145..65f17f50 100644 --- a/src/includes/nghttp2/asio_http2.h +++ b/src/includes/nghttp2/asio_http2.h @@ -314,6 +314,8 @@ class request; using response_cb = std::function; using request_cb = std::function; +using connect_cb = + std::function; class request_impl; @@ -351,7 +353,7 @@ public: const std::string &service); ~session(); - void on_connect(void_cb cb); + void on_connect(connect_cb cb); void on_error(error_cb cb); void shutdown();