From 992c14e3547c971c72763e0e83286c544174c4ab Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 6 Mar 2015 01:47:55 +0900 Subject: [PATCH] asio: Rename *_reader as *_generator and read_cb as generator_cb --- examples/asio-sv2.cc | 2 +- src/asio_client_request_impl.cc | 11 ++++++----- src/asio_client_request_impl.h | 8 ++++---- src/asio_client_session.cc | 6 +++--- src/asio_client_session_impl.cc | 2 +- src/asio_client_session_impl.h | 2 +- src/asio_common.cc | 14 +++++++------- src/asio_common.h | 6 +++--- src/asio_server_response.cc | 2 +- src/asio_server_response_impl.cc | 18 +++++++++--------- src/asio_server_response_impl.h | 8 ++++---- src/includes/nghttp2/asio_http2.h | 8 ++++---- src/includes/nghttp2/asio_http2_client.h | 2 +- src/includes/nghttp2/asio_http2_server.h | 2 +- 14 files changed, 46 insertions(+), 45 deletions(-) diff --git a/examples/asio-sv2.cc b/examples/asio-sv2.cc index 2fba5bbf..63eb1800 100644 --- a/examples/asio-sv2.cc +++ b/examples/asio-sv2.cc @@ -96,7 +96,7 @@ int main(int argc, char *argv[]) { header_value{http_date(stbuf.st_mtime)}); } res.write_head(200, std::move(header)); - res.end(file_reader_from_fd(fd)); + res.end(file_generator_from_fd(fd)); }); server.listen("*", port); diff --git a/src/asio_client_request_impl.cc b/src/asio_client_request_impl.cc index 4975cefd..584a1396 100644 --- a/src/asio_client_request_impl.cc +++ b/src/asio_client_request_impl.cc @@ -63,12 +63,13 @@ void request_impl::call_on_close(uint32_t error_code) { } } -void request_impl::on_read(read_cb cb) { read_cb_ = std::move(cb); } +void request_impl::on_read(generator_cb cb) { generator_cb_ = std::move(cb); } -read_cb::result_type request_impl::call_on_read(uint8_t *buf, std::size_t len, - uint32_t *data_flags) { - if (read_cb_) { - return read_cb_(buf, len, data_flags); +generator_cb::result_type request_impl::call_on_read(uint8_t *buf, + std::size_t len, + uint32_t *data_flags) { + if (generator_cb_) { + return generator_cb_(buf, len, data_flags); } *data_flags |= NGHTTP2_DATA_FLAG_EOF; diff --git a/src/asio_client_request_impl.h b/src/asio_client_request_impl.h index bd6b1541..f5d5249b 100644 --- a/src/asio_client_request_impl.h +++ b/src/asio_client_request_impl.h @@ -54,9 +54,9 @@ public: void on_close(close_cb cb); void call_on_close(uint32_t error_code); - void on_read(read_cb cb); - read_cb::result_type call_on_read(uint8_t *buf, std::size_t len, - uint32_t *data_flags); + void on_read(generator_cb cb); + generator_cb::result_type call_on_read(uint8_t *buf, std::size_t len, + uint32_t *data_flags); void resume(); @@ -78,7 +78,7 @@ private: response_cb response_cb_; request_cb push_request_cb_; close_cb close_cb_; - read_cb read_cb_; + generator_cb generator_cb_; class stream *strm_; uri_ref uri_; std::string method_; diff --git a/src/asio_client_session.cc b/src/asio_client_session.cc index 8c002e21..2e03f51e 100644 --- a/src/asio_client_session.cc +++ b/src/asio_client_session.cc @@ -64,20 +64,20 @@ boost::asio::io_service &session::io_service() const { const request *session::submit(boost::system::error_code &ec, const std::string &method, const std::string &uri, header_map h) const { - return impl_->submit(ec, method, uri, read_cb(), std::move(h)); + return impl_->submit(ec, method, uri, generator_cb(), std::move(h)); } const request *session::submit(boost::system::error_code &ec, const std::string &method, const std::string &uri, std::string data, header_map h) const { - return impl_->submit(ec, method, uri, string_reader(std::move(data)), + return impl_->submit(ec, method, uri, string_generator(std::move(data)), std::move(h)); } const request *session::submit(boost::system::error_code &ec, const std::string &method, - const std::string &uri, read_cb cb, + const std::string &uri, generator_cb cb, header_map h) const { return impl_->submit(ec, method, uri, std::move(cb), std::move(h)); } diff --git a/src/asio_client_session_impl.cc b/src/asio_client_session_impl.cc index ec976f97..c85be305 100644 --- a/src/asio_client_session_impl.cc +++ b/src/asio_client_session_impl.cc @@ -364,7 +364,7 @@ std::unique_ptr session_impl::create_stream() { const request *session_impl::submit(boost::system::error_code &ec, const std::string &method, - const std::string &uri, read_cb cb, + const std::string &uri, generator_cb cb, header_map h) { ec.clear(); diff --git a/src/asio_client_session_impl.h b/src/asio_client_session_impl.h index 045131aa..1cecedeb 100644 --- a/src/asio_client_session_impl.h +++ b/src/asio_client_session_impl.h @@ -63,7 +63,7 @@ public: const request *submit(boost::system::error_code &ec, const std::string &method, const std::string &uri, - read_cb cb, header_map h); + generator_cb cb, header_map h); virtual void start_connect(tcp::resolver::iterator endpoint_it) = 0; virtual tcp::socket &socket() = 0; diff --git a/src/asio_common.cc b/src/asio_common.cc index 3fec23e9..3025c493 100644 --- a/src/asio_common.cc +++ b/src/asio_common.cc @@ -47,7 +47,7 @@ boost::system::error_code make_error_code(nghttp2_error ev) { return boost::system::error_code(static_cast(ev), nghttp2_category()); } -read_cb string_reader(std::string data) { +generator_cb string_generator(std::string data) { auto strio = std::make_shared>(std::move(data), data.size()); return [strio](uint8_t *buf, size_t len, uint32_t *data_flags) { @@ -63,7 +63,7 @@ read_cb string_reader(std::string data) { }; } -read_cb deferred_reader() { +generator_cb deferred_generator() { return [](uint8_t *buf, size_t len, uint32_t *data_flags) { return NGHTTP2_ERR_DEFERRED; }; } @@ -74,20 +74,20 @@ std::shared_ptr> defer_shared(F &&f, T &&... t) { std::forward(t)...); } -read_cb file_reader(const std::string &path) { +generator_cb file_generator(const std::string &path) { auto fd = open(path.c_str(), O_RDONLY); if (fd == -1) { - return read_cb(); + return generator_cb(); } - return file_reader_from_fd(fd); + return file_generator_from_fd(fd); } -read_cb file_reader_from_fd(int fd) { +generator_cb file_generator_from_fd(int fd) { auto d = defer_shared(close, fd); return [fd, d](uint8_t *buf, size_t len, uint32_t *data_flags) - -> read_cb::result_type { + -> generator_cb::result_type { ssize_t n; while ((n = read(fd, buf, len)) == -1 && errno == EINTR) ; diff --git a/src/asio_common.h b/src/asio_common.h index 28a69a31..8f59a0d1 100644 --- a/src/asio_common.h +++ b/src/asio_common.h @@ -39,10 +39,10 @@ namespace asio_http2 { boost::system::error_code make_error_code(nghttp2_error ev); -read_cb string_reader(std::string data); +generator_cb string_generator(std::string data); -// Returns read_cb, which just returns NGHTTP2_ERR_DEFERRED -read_cb deferred_reader(); +// Returns generator_cb, which just returns NGHTTP2_ERR_DEFERRED +generator_cb deferred_generator(); template void split_path(uri_ref &dst, InputIt first, InputIt last) { diff --git a/src/asio_server_response.cc b/src/asio_server_response.cc index 3cb074de..6952b586 100644 --- a/src/asio_server_response.cc +++ b/src/asio_server_response.cc @@ -44,7 +44,7 @@ void response::write_head(unsigned int status_code, header_map h) const { void response::end(std::string data) const { impl_->end(std::move(data)); } -void response::end(read_cb cb) const { impl_->end(std::move(cb)); } +void response::end(generator_cb cb) const { impl_->end(std::move(cb)); } void response::on_close(close_cb cb) const { impl_->on_close(std::move(cb)); } diff --git a/src/asio_server_response_impl.cc b/src/asio_server_response_impl.cc index 205cac19..5661e610 100644 --- a/src/asio_server_response_impl.cc +++ b/src/asio_server_response_impl.cc @@ -33,7 +33,7 @@ namespace asio_http2 { namespace server { response_impl::response_impl() - : strm_(nullptr), read_cb_(deferred_reader()), status_code_(200), + : strm_(nullptr), generator_cb_(deferred_generator()), status_code_(200), state_(response_state::INITIAL), pushed_(false), push_promise_sent_(false) {} @@ -57,20 +57,20 @@ void response_impl::write_head(unsigned int status_code, header_map h) { } void response_impl::end(std::string data) { - end(string_reader(std::move(data))); + end(string_generator(std::move(data))); } -void response_impl::end(read_cb cb) { +void response_impl::end(generator_cb cb) { if (state_ == response_state::BODY_STARTED) { return; } - read_cb_ = std::move(cb); + generator_cb_ = std::move(cb); if (state_ == response_state::INITIAL) { write_head(status_code_); } else { - // read_cb is changed, start writing in case it is deferred. + // generator_cb is changed, start writing in case it is deferred. auto handler = strm_->handler(); handler->resume(*strm_); } @@ -133,10 +133,10 @@ const header_map &response_impl::header() const { return header_; } void response_impl::stream(class stream *s) { strm_ = s; } -read_cb::result_type response_impl::call_read(uint8_t *data, std::size_t len, - uint32_t *data_flags) { - if (read_cb_) { - return read_cb_(data, len, data_flags); +generator_cb::result_type +response_impl::call_read(uint8_t *data, std::size_t len, uint32_t *data_flags) { + if (generator_cb_) { + return generator_cb_(data, len, data_flags); } *data_flags |= NGHTTP2_DATA_FLAG_EOF; diff --git a/src/asio_server_response_impl.h b/src/asio_server_response_impl.h index fb3e3172..c85101a0 100644 --- a/src/asio_server_response_impl.h +++ b/src/asio_server_response_impl.h @@ -48,7 +48,7 @@ public: response_impl(); void write_head(unsigned int status_code, header_map h = {}); void end(std::string data = ""); - void end(read_cb cb); + void end(generator_cb cb); void on_close(close_cb cb); void resume(); @@ -66,14 +66,14 @@ public: void pushed(bool f); void push_promise_sent(); void stream(class stream *s); - read_cb::result_type call_read(uint8_t *data, std::size_t len, - uint32_t *data_flags); + generator_cb::result_type call_read(uint8_t *data, std::size_t len, + uint32_t *data_flags); void call_on_close(uint32_t error_code); private: class stream *strm_; header_map header_; - read_cb read_cb_; + generator_cb generator_cb_; close_cb close_cb_; unsigned int status_code_; response_state state_; diff --git a/src/includes/nghttp2/asio_http2.h b/src/includes/nghttp2/asio_http2.h index 635bb65f..670952f1 100644 --- a/src/includes/nghttp2/asio_http2.h +++ b/src/includes/nghttp2/asio_http2.h @@ -100,16 +100,16 @@ typedef std::function close_cb; // of the error and request/response must be closed, return // NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE. typedef std::function< - ssize_t(uint8_t *buf, std::size_t len, uint32_t *data_flags)> read_cb; + ssize_t(uint8_t *buf, std::size_t len, uint32_t *data_flags)> generator_cb; // Convenient function to create function to read file denoted by // |path|. This can be passed to response::end(). -read_cb file_reader(const std::string &path); +generator_cb file_generator(const std::string &path); -// Like file_reader(const std::string&), but it takes opened file +// Like file_generator(const std::string&), but it takes opened file // descriptor. The passed descriptor will be closed when returned // function object is destroyed. -read_cb file_reader_from_fd(int fd); +generator_cb file_generator_from_fd(int fd); // Validates path so that it does not contain directory traversal // vector. Returns true if path is safe. The |path| must start with diff --git a/src/includes/nghttp2/asio_http2_client.h b/src/includes/nghttp2/asio_http2_client.h index 76ef8c91..9d4750d3 100644 --- a/src/includes/nghttp2/asio_http2_client.h +++ b/src/includes/nghttp2/asio_http2_client.h @@ -165,7 +165,7 @@ public: // nullptr and |ec| contains error message. const request *submit(boost::system::error_code &ec, const std::string &method, const std::string &uri, - read_cb cb, header_map h = {}) const; + generator_cb cb, header_map h = {}) const; private: std::unique_ptr impl_; diff --git a/src/includes/nghttp2/asio_http2_server.h b/src/includes/nghttp2/asio_http2_server.h index 23649740..a3df3310 100644 --- a/src/includes/nghttp2/asio_http2_server.h +++ b/src/includes/nghttp2/asio_http2_server.h @@ -79,7 +79,7 @@ public: // Sets callback as a generator of the response body. No further // call of end() is allowed. - void end(read_cb cb) const; + void end(generator_cb cb) const; // Sets callback which is invoked when this request and response are // finished. After the invocation of this callback, the application