From 6020b727d8921c83656d0c8bf8d933be7f599f78 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 5 Mar 2015 22:00:14 +0900 Subject: [PATCH] asio: Add signal_write() to server::http2_handler to avoid scattered ifs --- src/asio_server_http2_handler.cc | 14 ++++++++++++-- src/asio_server_http2_handler.h | 3 ++- src/asio_server_response_impl.cc | 13 ------------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/asio_server_http2_handler.cc b/src/asio_server_http2_handler.cc index cd53c0f2..c6ca65d2 100644 --- a/src/asio_server_http2_handler.cc +++ b/src/asio_server_http2_handler.cc @@ -343,6 +343,8 @@ int http2_handler::start_response(stream &strm) { return -1; } + signal_write(); + return 0; } @@ -356,16 +358,22 @@ void http2_handler::leave_callback() { inside_callback_ = false; } -bool http2_handler::inside_callback() const { return inside_callback_; } - void http2_handler::stream_error(int32_t stream_id, uint32_t error_code) { ::nghttp2::asio_http2::server::stream_error(session_, stream_id, error_code); + signal_write(); +} + +void http2_handler::signal_write() { + if (!inside_callback_) { + initiate_write(); + } } void http2_handler::initiate_write() { writefun_(); } void http2_handler::resume(stream &strm) { nghttp2_session_resume_data(session_, strm.get_stream_id()); + signal_write(); } response *http2_handler::push_promise(boost::system::error_code &ec, @@ -412,6 +420,8 @@ response *http2_handler::push_promise(boost::system::error_code &ec, auto &promised_res = promised_strm->response().impl(); promised_res.pushed(true); + signal_write(); + return &promised_strm->response(); } diff --git a/src/asio_server_http2_handler.h b/src/asio_server_http2_handler.h index bc8f5507..d08475a2 100644 --- a/src/asio_server_http2_handler.h +++ b/src/asio_server_http2_handler.h @@ -76,7 +76,6 @@ public: void enter_callback(); void leave_callback(); - bool inside_callback() const; void resume(stream &s); @@ -84,6 +83,8 @@ public: std::string method, std::string raw_path_query, header_map h); + void signal_write(); + boost::asio::io_service &io_service(); template diff --git a/src/asio_server_response_impl.cc b/src/asio_server_response_impl.cc index a535aba3..0c9d0406 100644 --- a/src/asio_server_response_impl.cc +++ b/src/asio_server_response_impl.cc @@ -72,12 +72,7 @@ void response_impl::call_on_close(uint32_t error_code) { void response_impl::cancel(uint32_t error_code) { auto handler = strm_->handler(); - handler->stream_error(strm_->get_stream_id(), error_code); - - if (!handler->inside_callback()) { - handler->initiate_write(); - } } void response_impl::start_response() { @@ -91,10 +86,6 @@ void response_impl::start_response() { handler->stream_error(strm_->get_stream_id(), NGHTTP2_INTERNAL_ERROR); return; } - - if (!handler->inside_callback()) { - handler->initiate_write(); - } } response *response_impl::push(boost::system::error_code &ec, std::string method, @@ -107,10 +98,6 @@ response *response_impl::push(boost::system::error_code &ec, std::string method, void response_impl::resume() { auto handler = strm_->handler(); handler->resume(*strm_); - - if (!handler->inside_callback()) { - handler->initiate_write(); - } } boost::asio::io_service &response_impl::io_service() {