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)});
}
res.write_head(200, std::move(header));
res.end(file_reader_from_fd(fd));
res.end(file_generator_from_fd(fd));
});
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,
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;

View File

@ -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_;

View File

@ -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));
}

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 std::string &method,
const std::string &uri, read_cb cb,
const std::string &uri, generator_cb cb,
header_map h) {
ec.clear();

View File

@ -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;

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());
}
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),
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<F, T...>> defer_shared(F &&f, 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);
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)
;

View File

@ -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 <typename InputIt>
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(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)); }

View File

@ -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;

View File

@ -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_;

View File

@ -100,16 +100,16 @@ typedef std::function<void(uint32_t)> 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

View File

@ -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<session_impl> impl_;

View File

@ -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