asio: Pass connected address to connect_cb

This commit is contained in:
Tatsuhiro Tsujikawa 2015-03-04 01:23:35 +09:00
parent 70ea774f23
commit 76eab3faa0
7 changed files with 24 additions and 21 deletions

View File

@ -69,8 +69,8 @@ int main(int argc, char *argv[]) {
configure_tls_context(tls_ctx); configure_tls_context(tls_ctx);
session sess(io_service, tls_ctx, "localhost", "3000"); session sess(io_service, tls_ctx, "localhost", "3000");
sess.on_connect([&sess]() { sess.on_connect([&sess](tcp::resolver::iterator endpoint_it) {
std::cerr << "connected" << std::endl; std::cerr << "connected to " << (*endpoint_it).endpoint() << std::endl;
boost::system::error_code ec; boost::system::error_code ec;
auto req = sess.submit(ec, "GET", "https://localhost:3000/", auto req = sess.submit(ec, "GET", "https://localhost:3000/",
"hello world", {{"cookie", {"foobar", true}}}); "hello world", {{"cookie", {"foobar", true}}});

View File

@ -49,7 +49,7 @@ session::session(boost::asio::io_service &io_service,
session::~session() {} 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)); } void session::on_error(error_cb cb) { impl_->on_error(std::move(cb)); }

View File

@ -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()) { if (!setup_session()) {
return; return;
} }
@ -84,7 +84,7 @@ void session_impl::connected() {
auto &connect_cb = on_connect(); auto &connect_cb = on_connect();
if (connect_cb) { 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); } 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_; } const error_cb &session_impl::on_error() const { return error_cb_; }

View File

@ -50,13 +50,13 @@ public:
void start_resolve(const std::string &host, const std::string &service); 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 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); void on_error(error_cb cb);
const void_cb &on_connect() const; const connect_cb &on_connect() const;
const error_cb &on_error() const; const error_cb &on_error() const;
void cancel(stream &strm); void cancel(stream &strm);
@ -104,7 +104,7 @@ private:
std::map<int32_t, std::unique_ptr<stream>> streams_; std::map<int32_t, std::unique_ptr<stream>> streams_;
void_cb connect_cb_; connect_cb connect_cb_;
error_cb error_cb_; error_cb error_cb_;
nghttp2_session *session_; nghttp2_session *session_;

View File

@ -46,7 +46,7 @@ void session_tcp_impl::start_connect(tcp::resolver::iterator endpoint_it) {
return; return;
} }
connected(); connected(endpoint_it);
}); });
} }

View File

@ -52,13 +52,14 @@ void session_tls_impl::start_connect(tcp::resolver::iterator endpoint_it) {
return; return;
} }
socket_.async_handshake(boost::asio::ssl::stream_base::client, socket_.async_handshake(
[this](const boost::system::error_code &ec) { boost::asio::ssl::stream_base::client,
[this, endpoint_it](const boost::system::error_code &ec) {
if (ec) { if (ec) {
not_connected(ec); not_connected(ec);
return; return;
} }
connected(); connected(endpoint_it);
}); });
}); });
} }

View File

@ -314,6 +314,8 @@ class request;
using response_cb = std::function<void(const response &)>; using response_cb = std::function<void(const response &)>;
using request_cb = std::function<void(const request &)>; using request_cb = std::function<void(const request &)>;
using connect_cb =
std::function<void(boost::asio::ip::tcp::resolver::iterator)>;
class request_impl; class request_impl;
@ -351,7 +353,7 @@ public:
const std::string &service); const std::string &service);
~session(); ~session();
void on_connect(void_cb cb); void on_connect(connect_cb cb);
void on_error(error_cb cb); void on_error(error_cb cb);
void shutdown(); void shutdown();