asio: Rename *_reader as *_generator and read_cb as generator_cb

This commit is contained in:
Tatsuhiro Tsujikawa 2015-03-06 01:47:55 +09:00
parent 529fc937dc
commit 992c14e354
14 changed files with 46 additions and 45 deletions

View File

@ -96,7 +96,7 @@ int main(int argc, char *argv[]) {
header_value{http_date(stbuf.st_mtime)}); header_value{http_date(stbuf.st_mtime)});
} }
res.write_head(200, std::move(header)); res.write_head(200, std::move(header));
res.end(file_reader_from_fd(fd)); res.end(file_generator_from_fd(fd));
}); });
server.listen("*", port); server.listen("*", port);

View File

@ -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, generator_cb::result_type request_impl::call_on_read(uint8_t *buf,
uint32_t *data_flags) { std::size_t len,
if (read_cb_) { uint32_t *data_flags) {
return read_cb_(buf, len, data_flags); if (generator_cb_) {
return generator_cb_(buf, len, data_flags);
} }
*data_flags |= NGHTTP2_DATA_FLAG_EOF; *data_flags |= NGHTTP2_DATA_FLAG_EOF;

View File

@ -54,9 +54,9 @@ public:
void on_close(close_cb cb); void on_close(close_cb cb);
void call_on_close(uint32_t error_code); void call_on_close(uint32_t error_code);
void on_read(read_cb cb); void on_read(generator_cb cb);
read_cb::result_type call_on_read(uint8_t *buf, std::size_t len, generator_cb::result_type call_on_read(uint8_t *buf, std::size_t len,
uint32_t *data_flags); uint32_t *data_flags);
void resume(); void resume();
@ -78,7 +78,7 @@ private:
response_cb response_cb_; response_cb response_cb_;
request_cb push_request_cb_; request_cb push_request_cb_;
close_cb close_cb_; close_cb close_cb_;
read_cb read_cb_; generator_cb generator_cb_;
class stream *strm_; class stream *strm_;
uri_ref uri_; uri_ref uri_;
std::string method_; std::string method_;

View File

@ -64,20 +64,20 @@ boost::asio::io_service &session::io_service() const {
const request *session::submit(boost::system::error_code &ec, const request *session::submit(boost::system::error_code &ec,
const std::string &method, const std::string &method,
const std::string &uri, header_map h) const { 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 request *session::submit(boost::system::error_code &ec,
const std::string &method, const std::string &method,
const std::string &uri, std::string data, const std::string &uri, std::string data,
header_map h) const { 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)); std::move(h));
} }
const request *session::submit(boost::system::error_code &ec, const request *session::submit(boost::system::error_code &ec,
const std::string &method, const std::string &method,
const std::string &uri, read_cb cb, const std::string &uri, generator_cb cb,
header_map h) const { header_map h) const {
return impl_->submit(ec, method, uri, std::move(cb), std::move(h)); return impl_->submit(ec, method, uri, std::move(cb), std::move(h));
} }

View File

@ -364,7 +364,7 @@ std::unique_ptr<stream> session_impl::create_stream() {
const request *session_impl::submit(boost::system::error_code &ec, const request *session_impl::submit(boost::system::error_code &ec,
const std::string &method, const std::string &method,
const std::string &uri, read_cb cb, const std::string &uri, generator_cb cb,
header_map h) { header_map h) {
ec.clear(); ec.clear();

View File

@ -63,7 +63,7 @@ public:
const request *submit(boost::system::error_code &ec, const request *submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri, 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 void start_connect(tcp::resolver::iterator endpoint_it) = 0;
virtual tcp::socket &socket() = 0; virtual tcp::socket &socket() = 0;

View File

@ -47,7 +47,7 @@ boost::system::error_code make_error_code(nghttp2_error ev) {
return boost::system::error_code(static_cast<int>(ev), nghttp2_category()); return boost::system::error_code(static_cast<int>(ev), nghttp2_category());
} }
read_cb string_reader(std::string data) { generator_cb string_generator(std::string data) {
auto strio = std::make_shared<std::pair<std::string, size_t>>(std::move(data), auto strio = std::make_shared<std::pair<std::string, size_t>>(std::move(data),
data.size()); data.size());
return [strio](uint8_t *buf, size_t len, uint32_t *data_flags) { 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, return [](uint8_t *buf, size_t len,
uint32_t *data_flags) { return NGHTTP2_ERR_DEFERRED; }; uint32_t *data_flags) { return NGHTTP2_ERR_DEFERRED; };
} }
@ -74,20 +74,20 @@ std::shared_ptr<Defer<F, T...>> defer_shared(F &&f, T &&... t) {
std::forward<T>(t)...); std::forward<T>(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); auto fd = open(path.c_str(), O_RDONLY);
if (fd == -1) { 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); auto d = defer_shared(close, fd);
return [fd, d](uint8_t *buf, size_t len, uint32_t *data_flags) return [fd, d](uint8_t *buf, size_t len, uint32_t *data_flags)
-> read_cb::result_type { -> generator_cb::result_type {
ssize_t n; ssize_t n;
while ((n = read(fd, buf, len)) == -1 && errno == EINTR) while ((n = read(fd, buf, len)) == -1 && errno == EINTR)
; ;

View File

@ -39,10 +39,10 @@ namespace asio_http2 {
boost::system::error_code make_error_code(nghttp2_error ev); 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 // Returns generator_cb, which just returns NGHTTP2_ERR_DEFERRED
read_cb deferred_reader(); generator_cb deferred_generator();
template <typename InputIt> template <typename InputIt>
void split_path(uri_ref &dst, InputIt first, InputIt last) { void split_path(uri_ref &dst, InputIt first, InputIt last) {

View File

@ -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(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)); } void response::on_close(close_cb cb) const { impl_->on_close(std::move(cb)); }

View File

@ -33,7 +33,7 @@ namespace asio_http2 {
namespace server { namespace server {
response_impl::response_impl() 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), state_(response_state::INITIAL), pushed_(false),
push_promise_sent_(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) { 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) { if (state_ == response_state::BODY_STARTED) {
return; return;
} }
read_cb_ = std::move(cb); generator_cb_ = std::move(cb);
if (state_ == response_state::INITIAL) { if (state_ == response_state::INITIAL) {
write_head(status_code_); write_head(status_code_);
} else { } 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(); auto handler = strm_->handler();
handler->resume(*strm_); 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; } void response_impl::stream(class stream *s) { strm_ = s; }
read_cb::result_type response_impl::call_read(uint8_t *data, std::size_t len, generator_cb::result_type
uint32_t *data_flags) { response_impl::call_read(uint8_t *data, std::size_t len, uint32_t *data_flags) {
if (read_cb_) { if (generator_cb_) {
return read_cb_(data, len, data_flags); return generator_cb_(data, len, data_flags);
} }
*data_flags |= NGHTTP2_DATA_FLAG_EOF; *data_flags |= NGHTTP2_DATA_FLAG_EOF;

View File

@ -48,7 +48,7 @@ public:
response_impl(); response_impl();
void write_head(unsigned int status_code, header_map h = {}); void write_head(unsigned int status_code, header_map h = {});
void end(std::string data = ""); void end(std::string data = "");
void end(read_cb cb); void end(generator_cb cb);
void on_close(close_cb cb); void on_close(close_cb cb);
void resume(); void resume();
@ -66,14 +66,14 @@ public:
void pushed(bool f); void pushed(bool f);
void push_promise_sent(); void push_promise_sent();
void stream(class stream *s); void stream(class stream *s);
read_cb::result_type call_read(uint8_t *data, std::size_t len, generator_cb::result_type call_read(uint8_t *data, std::size_t len,
uint32_t *data_flags); uint32_t *data_flags);
void call_on_close(uint32_t error_code); void call_on_close(uint32_t error_code);
private: private:
class stream *strm_; class stream *strm_;
header_map header_; header_map header_;
read_cb read_cb_; generator_cb generator_cb_;
close_cb close_cb_; close_cb close_cb_;
unsigned int status_code_; unsigned int status_code_;
response_state state_; response_state state_;

View File

@ -100,16 +100,16 @@ typedef std::function<void(uint32_t)> close_cb;
// of the error and request/response must be closed, return // of the error and request/response must be closed, return
// NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE. // NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE.
typedef std::function< 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 // Convenient function to create function to read file denoted by
// |path|. This can be passed to response::end(). // |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 // descriptor. The passed descriptor will be closed when returned
// function object is destroyed. // 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 // Validates path so that it does not contain directory traversal
// vector. Returns true if path is safe. The |path| must start with // vector. Returns true if path is safe. The |path| must start with

View File

@ -165,7 +165,7 @@ public:
// nullptr and |ec| contains error message. // nullptr and |ec| contains error message.
const request *submit(boost::system::error_code &ec, const request *submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri, const std::string &method, const std::string &uri,
read_cb cb, header_map h = {}) const; generator_cb cb, header_map h = {}) const;
private: private:
std::unique_ptr<session_impl> impl_; std::unique_ptr<session_impl> impl_;

View File

@ -79,7 +79,7 @@ public:
// Sets callback as a generator of the response body. No further // Sets callback as a generator of the response body. No further
// call of end() is allowed. // 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 // Sets callback which is invoked when this request and response are
// finished. After the invocation of this callback, the application // finished. After the invocation of this callback, the application