asio: client: Call error_cb on error occurred in do_read and do_write

Fixes GH-207
This commit is contained in:
Tatsuhiro Tsujikawa 2015-04-13 21:33:43 +09:00
parent a8ea86cfe5
commit 5c2ca28706
2 changed files with 17 additions and 8 deletions

View File

@ -89,10 +89,7 @@ void session_impl::connected(tcp::resolver::iterator endpoint_it) {
} }
void session_impl::not_connected(const boost::system::error_code &ec) { void session_impl::not_connected(const boost::system::error_code &ec) {
auto &error_cb = on_error(); call_error_cb(ec);
if (error_cb) {
error_cb(ec);
}
} }
void session_impl::on_connect(connect_cb cb) { connect_cb_ = std::move(cb); } void session_impl::on_connect(connect_cb cb) { connect_cb_ = std::move(cb); }
@ -103,6 +100,14 @@ 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_; }
void session_impl::call_error_cb(const boost::system::error_code &ec) {
auto &error_cb = on_error();
if (!error_cb) {
return;
}
error_cb(ec);
}
namespace { namespace {
int on_begin_headers_callback(nghttp2_session *session, int on_begin_headers_callback(nghttp2_session *session,
const nghttp2_frame *frame, void *user_data) { const nghttp2_frame *frame, void *user_data) {
@ -308,10 +313,7 @@ bool session_impl::setup_session() {
auto rv = nghttp2_session_client_new(&session_, callbacks, this); auto rv = nghttp2_session_client_new(&session_, callbacks, this);
if (rv != 0) { if (rv != 0) {
auto &error_cb = on_error(); call_error_cb(make_error_code(static_cast<nghttp2_error>(rv)));
if (error_cb) {
error_cb(make_error_code(static_cast<nghttp2_error>(rv)));
}
return false; return false;
} }
@ -528,6 +530,7 @@ void session_impl::do_read() {
std::size_t bytes_transferred) { std::size_t bytes_transferred) {
if (ec) { if (ec) {
if (ec.value() == boost::asio::error::operation_aborted) { if (ec.value() == boost::asio::error::operation_aborted) {
call_error_cb(ec);
shutdown_socket(); shutdown_socket();
} }
return; return;
@ -540,6 +543,8 @@ void session_impl::do_read() {
nghttp2_session_mem_recv(session_, rb_.data(), bytes_transferred); nghttp2_session_mem_recv(session_, 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(
static_cast<nghttp2_error>(rv < 0 ? rv : NGHTTP2_ERR_PROTO)));
shutdown_socket(); shutdown_socket();
return; return;
} }
@ -577,6 +582,7 @@ void session_impl::do_write() {
const uint8_t *data; const uint8_t *data;
auto n = nghttp2_session_mem_send(session_, &data); auto n = nghttp2_session_mem_send(session_, &data);
if (n < 0) { if (n < 0) {
call_error_cb(make_error_code(static_cast<nghttp2_error>(n)));
shutdown_socket(); shutdown_socket();
return; return;
} }
@ -606,6 +612,8 @@ void session_impl::do_write() {
write_socket([this](const boost::system::error_code &ec, std::size_t n) { write_socket([this](const boost::system::error_code &ec, std::size_t n) {
if (ec) { if (ec) {
call_error_cb(ec);
shutdown_socket();
return; return;
} }

View File

@ -97,6 +97,7 @@ protected:
private: private:
bool should_stop() const; bool should_stop() const;
bool setup_session(); bool setup_session();
void call_error_cb(const boost::system::error_code &ec);
boost::asio::io_service &io_service_; boost::asio::io_service &io_service_;
tcp::resolver resolver_; tcp::resolver resolver_;