nghttpd: Use nghttp2_session_mem_recv and nghttp2_session_mem_send

This commit is contained in:
Tatsuhiro Tsujikawa 2014-02-19 23:27:21 +09:00
parent 1e1e77ad5e
commit bbc09b005b
1 changed files with 43 additions and 80 deletions

View File

@ -312,41 +312,57 @@ int Http2Handler::setup_bev()
int Http2Handler::on_read() int Http2Handler::on_read()
{ {
int rv = 0; int rv;
if((rv = nghttp2_session_recv(session_)) < 0) { auto input = bufferevent_get_input(bev_);
if(rv != NGHTTP2_ERR_EOF) { auto inputlen = evbuffer_get_length(input);
std::cerr << "nghttp2_session_recv() returned error: " auto mem = evbuffer_pullup(input, -1);
rv = nghttp2_session_mem_recv(session_, mem, inputlen);
if(rv < 0) {
std::cerr << "nghttp2_session_mem_recv() returned error: "
<< nghttp2_strerror(rv) << std::endl; << nghttp2_strerror(rv) << std::endl;
return -1;
} }
} else if((rv = nghttp2_session_send(session_)) < 0) {
std::cerr << "nghttp2_session_send() returned error: " evbuffer_drain(input, rv);
<< nghttp2_strerror(rv) << std::endl;
} return on_write();
if(rv == 0) {
if(nghttp2_session_want_read(session_) == 0 &&
nghttp2_session_want_write(session_) == 0 &&
evbuffer_get_length(bufferevent_get_output(bev_)) == 0) {
rv = -1;
}
}
return rv;
} }
int Http2Handler::on_write() int Http2Handler::on_write()
{ {
int rv = 0; int rv;
if((rv = nghttp2_session_send(session_)) < 0) { auto output = bufferevent_get_output(bev_);
std::cerr << "nghttp2_session_send() returned error: "
<< nghttp2_strerror(rv) << std::endl; for(;;) {
if(evbuffer_get_length(output) >
sessions_->get_config()->output_upper_thres) {
break;
}
const uint8_t *data;
auto datalen = nghttp2_session_mem_send(session_, &data);
if(datalen < 0) {
std::cerr << "nghttp2_session_mem_send() returned error: "
<< nghttp2_strerror(datalen) << std::endl;
return -1;
}
if(datalen == 0) {
break;
}
rv = evbuffer_add(output, data, datalen);
if(rv != 0) {
std::cerr << "evbuffer_add() failed" << std::endl;
return -1;
}
} }
if(rv == 0) {
if(nghttp2_session_want_read(session_) == 0 && if(nghttp2_session_want_read(session_) == 0 &&
nghttp2_session_want_write(session_) == 0 && nghttp2_session_want_write(session_) == 0 &&
evbuffer_get_length(bufferevent_get_output(bev_)) == 0) { evbuffer_get_length(output) == 0) {
rv = -1; return -1;
} }
} return 0;
return rv;
} }
namespace { namespace {
@ -420,38 +436,6 @@ int Http2Handler::verify_npn_result()
return -1; return -1;
} }
int Http2Handler::sendcb(const uint8_t *data, size_t len)
{
int rv;
auto output = bufferevent_get_output(bev_);
// Check buffer length and return WOULDBLOCK if it is large enough.
if(evbuffer_get_length(output) >
sessions_->get_config()->output_upper_thres) {
return NGHTTP2_ERR_WOULDBLOCK;
}
rv = evbuffer_add(output, data, len);
if(rv == -1) {
std::cerr << "evbuffer_add() failed" << std::endl;
return NGHTTP2_ERR_CALLBACK_FAILURE;
} else {
return len;
}
}
int Http2Handler::recvcb(uint8_t *buf, size_t len)
{
auto input = bufferevent_get_input(bev_);
int nread = evbuffer_remove(input, buf, len);
if(nread == -1) {
return NGHTTP2_ERR_CALLBACK_FAILURE;
} else if(nread == 0) {
return NGHTTP2_ERR_WOULDBLOCK;
} else {
return nread;
}
}
int Http2Handler::submit_file_response(const std::string& status, int Http2Handler::submit_file_response(const std::string& status,
int32_t stream_id, int32_t stream_id,
time_t last_modified, time_t last_modified,
@ -593,25 +577,6 @@ void Http2Handler::terminate_session(nghttp2_error_code error_code)
nghttp2_session_terminate_session(session_, error_code); nghttp2_session_terminate_session(session_, error_code);
} }
namespace {
ssize_t hd_send_callback(nghttp2_session *session,
const uint8_t *data, size_t len, int flags,
void *user_data)
{
auto hd = static_cast<Http2Handler*>(user_data);
return hd->sendcb(data, len);
}
} // namespace
namespace {
ssize_t hd_recv_callback(nghttp2_session *session,
uint8_t *data, size_t len, int flags, void *user_data)
{
auto hd = static_cast<Http2Handler*>(user_data);
return hd->recvcb(data, len);
}
} // namespace
ssize_t file_read_callback ssize_t file_read_callback
(nghttp2_session *session, int32_t stream_id, (nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t length, int *eof, uint8_t *buf, size_t length, int *eof,
@ -964,8 +929,6 @@ namespace {
void fill_callback(nghttp2_session_callbacks& callbacks, const Config *config) void fill_callback(nghttp2_session_callbacks& callbacks, const Config *config)
{ {
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.send_callback = hd_send_callback;
callbacks.recv_callback = hd_recv_callback;
callbacks.on_stream_close_callback = on_stream_close_callback; callbacks.on_stream_close_callback = on_stream_close_callback;
callbacks.on_frame_recv_callback = hd_on_frame_recv_callback; callbacks.on_frame_recv_callback = hd_on_frame_recv_callback;
callbacks.before_frame_send_callback = hd_before_frame_send_callback; callbacks.before_frame_send_callback = hd_before_frame_send_callback;