Holding more shared_ptrs instead of raw ptrs to make sure called objects don't get deleted.

This commit is contained in:
clemahieu 2017-02-08 23:01:37 -06:00 committed by Tatsuhiro Tsujikawa
parent a231874e1e
commit 298808f276
3 changed files with 38 additions and 36 deletions

View File

@ -69,17 +69,17 @@ void session_impl::start_resolve(const std::string &host,
const std::string &service) { const std::string &service) {
deadline_.expires_from_now(connect_timeout_); deadline_.expires_from_now(connect_timeout_);
auto self = this->shared_from_this(); auto self = shared_from_this();
resolver_.async_resolve({host, service}, resolver_.async_resolve({host, service},
[this, self](const boost::system::error_code &ec, [self](const boost::system::error_code &ec,
tcp::resolver::iterator endpoint_it) { tcp::resolver::iterator endpoint_it) {
if (ec) { if (ec) {
not_connected(ec); self->not_connected(ec);
return; return;
} }
start_connect(endpoint_it); self->start_connect(endpoint_it);
}); });
deadline_.async_wait(std::bind(&session_impl::handle_deadline, self)); deadline_.async_wait(std::bind(&session_impl::handle_deadline, self));
@ -597,38 +597,38 @@ void session_impl::do_read() {
auto self = this->shared_from_this(); auto self = this->shared_from_this();
read_socket([this, self](const boost::system::error_code &ec, read_socket([self](const boost::system::error_code &ec,
std::size_t bytes_transferred) { std::size_t bytes_transferred) {
if (ec) { if (ec) {
if (!should_stop()) { if (!self->should_stop()) {
call_error_cb(ec); self->call_error_cb(ec);
} }
stop(); self->stop();
return; return;
} }
{ {
callback_guard cg(*this); callback_guard cg(*self);
auto rv = auto rv =
nghttp2_session_mem_recv(session_, rb_.data(), bytes_transferred); nghttp2_session_mem_recv(self->session_, self->rb_.data(), bytes_transferred);
if (rv != static_cast<ssize_t>(bytes_transferred)) { if (rv != static_cast<ssize_t>(bytes_transferred)) {
call_error_cb(make_error_code( self->call_error_cb(make_error_code(
static_cast<nghttp2_error>(rv < 0 ? rv : NGHTTP2_ERR_PROTO))); static_cast<nghttp2_error>(rv < 0 ? rv : NGHTTP2_ERR_PROTO)));
stop(); self->stop();
return; return;
} }
} }
do_write(); self->do_write();
if (should_stop()) { if (self->should_stop()) {
stop(); self->stop();
return; return;
} }
do_read(); self->do_read();
}); });
} }
@ -695,17 +695,17 @@ void session_impl::do_write() {
auto self = this->shared_from_this(); auto self = this->shared_from_this();
write_socket( write_socket(
[this, self](const boost::system::error_code &ec, std::size_t n) { [self](const boost::system::error_code &ec, std::size_t n) {
if (ec) { if (ec) {
call_error_cb(ec); self->call_error_cb(ec);
stop(); self->stop();
return; return;
} }
wblen_ = 0; self->wblen_ = 0;
writing_ = false; self->writing_ = false;
do_write(); self->do_write();
}); });
} }

View File

@ -37,19 +37,20 @@ session_tcp_impl::session_tcp_impl(
session_tcp_impl::~session_tcp_impl() {} session_tcp_impl::~session_tcp_impl() {}
void session_tcp_impl::start_connect(tcp::resolver::iterator endpoint_it) { void session_tcp_impl::start_connect(tcp::resolver::iterator endpoint_it) {
auto self = shared_from_this();
boost::asio::async_connect(socket_, endpoint_it, boost::asio::async_connect(socket_, endpoint_it,
[this](const boost::system::error_code &ec, [self](const boost::system::error_code &ec,
tcp::resolver::iterator endpoint_it) { tcp::resolver::iterator endpoint_it) {
if (stopped()) { if (self->stopped()) {
return; return;
} }
if (ec) { if (ec) {
not_connected(ec); self->not_connected(ec);
return; return;
} }
connected(endpoint_it); self->connected(endpoint_it);
}); });
} }

View File

@ -43,37 +43,38 @@ session_tls_impl::session_tls_impl(
session_tls_impl::~session_tls_impl() {} session_tls_impl::~session_tls_impl() {}
void session_tls_impl::start_connect(tcp::resolver::iterator endpoint_it) { void session_tls_impl::start_connect(tcp::resolver::iterator endpoint_it) {
auto self = std::static_pointer_cast<session_tls_impl>(shared_from_this());
boost::asio::async_connect( boost::asio::async_connect(
socket(), endpoint_it, [this](const boost::system::error_code &ec, socket(), endpoint_it, [self](const boost::system::error_code &ec,
tcp::resolver::iterator endpoint_it) { tcp::resolver::iterator endpoint_it) {
if (stopped()) { if (self->stopped()) {
return; return;
} }
if (ec) { if (ec) {
not_connected(ec); self->not_connected(ec);
return; return;
} }
socket_.async_handshake( self->socket_.async_handshake(
boost::asio::ssl::stream_base::client, boost::asio::ssl::stream_base::client,
[this, endpoint_it](const boost::system::error_code &ec) { [self, endpoint_it](const boost::system::error_code &ec) {
if (stopped()) { if (self->stopped()) {
return; return;
} }
if (ec) { if (ec) {
not_connected(ec); self->not_connected(ec);
return; return;
} }
if (!tls_h2_negotiated(socket_)) { if (!tls_h2_negotiated(self->socket_)) {
not_connected(make_error_code( self->not_connected(make_error_code(
NGHTTP2_ASIO_ERR_TLS_NO_APP_PROTO_NEGOTIATED)); NGHTTP2_ASIO_ERR_TLS_NO_APP_PROTO_NEGOTIATED));
return; return;
} }
connected(endpoint_it); self->connected(endpoint_it);
}); });
}); });
} }