nghttpx: Use nghttp2_session_mem_recv instead of nghttp2_session_recv

This commit is contained in:
Tatsuhiro Tsujikawa 2014-01-18 16:50:52 +09:00
parent 50dd7ada50
commit 0872ce27f4
2 changed files with 45 additions and 71 deletions

View File

@ -711,24 +711,6 @@ ssize_t send_callback(nghttp2_session *session,
}
} // namespace
namespace {
ssize_t recv_callback(nghttp2_session *session,
uint8_t *data, size_t len, int flags, void *user_data)
{
auto http2session = reinterpret_cast<Http2Session*>(user_data);
auto bev = http2session->get_bev();
auto input = bufferevent_get_input(bev);
int nread = evbuffer_remove(input, data, len);
if(nread == -1) {
return NGHTTP2_ERR_CALLBACK_FAILURE;
} else if(nread == 0) {
return NGHTTP2_ERR_WOULDBLOCK;
} else {
return nread;
}
}
} // namespace
namespace {
int on_stream_close_callback
(nghttp2_session *session, int32_t stream_id, nghttp2_error_code error_code,
@ -1215,7 +1197,6 @@ int Http2Session::on_connect()
nghttp2_session_callbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.send_callback = send_callback;
callbacks.recv_callback = recv_callback;
callbacks.on_stream_close_callback = on_stream_close_callback;
callbacks.on_frame_recv_callback = on_frame_recv_callback;
callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback;
@ -1290,27 +1271,33 @@ int Http2Session::on_connect()
int Http2Session::on_read()
{
int rv = 0;
if((rv = nghttp2_session_recv(session_)) < 0) {
if(rv != NGHTTP2_ERR_EOF) {
SSLOG(ERROR, this) << "nghttp2_session_recv() returned error: "
<< nghttp2_strerror(rv);
}
} else if((rv = nghttp2_session_send(session_)) < 0) {
ssize_t rv = 0;
auto input = bufferevent_get_input(bev_);
auto inputlen = evbuffer_get_length(input);
auto mem = evbuffer_pullup(input, -1);
rv = nghttp2_session_mem_recv(session_, mem, inputlen);
if(rv < 0) {
SSLOG(ERROR, this) << "nghttp2_session_recv() returned error: "
<< nghttp2_strerror(rv);
return -1;
}
evbuffer_drain(input, rv);
rv = nghttp2_session_send(session_);
if(rv < 0) {
SSLOG(ERROR, this) << "nghttp2_session_send() returned error: "
<< nghttp2_strerror(rv);
return -1;
}
if(rv == 0) {
if(nghttp2_session_want_read(session_) == 0 &&
nghttp2_session_want_write(session_) == 0 &&
evbuffer_get_length(bufferevent_get_output(bev_)) == 0) {
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, this) << "No more read/write for this session";
}
rv = -1;
if(nghttp2_session_want_read(session_) == 0 &&
nghttp2_session_want_write(session_) == 0 &&
evbuffer_get_length(bufferevent_get_output(bev_)) == 0) {
if(LOG_ENABLED(INFO)) {
SSLOG(INFO, this) << "No more read/write for this session";
}
return -1;
}
return rv;
return 0;
}
int Http2Session::on_write()

View File

@ -73,25 +73,6 @@ ssize_t send_callback(nghttp2_session *session,
}
} // namespace
namespace {
ssize_t recv_callback(nghttp2_session *session,
uint8_t *data, size_t len, int flags, void *user_data)
{
auto upstream = reinterpret_cast<Http2Upstream*>(user_data);
auto handler = upstream->get_client_handler();
auto bev = handler->get_bev();
auto input = bufferevent_get_input(bev);
int nread = evbuffer_remove(input, data, len);
if(nread == -1) {
return NGHTTP2_ERR_CALLBACK_FAILURE;
} else if(nread == 0) {
return NGHTTP2_ERR_WOULDBLOCK;
} else {
return nread;
}
}
} // namespace
namespace {
int on_stream_close_callback
(nghttp2_session *session, int32_t stream_id, nghttp2_error_code error_code,
@ -533,7 +514,6 @@ Http2Upstream::Http2Upstream(ClientHandler *handler)
nghttp2_session_callbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.send_callback = send_callback;
callbacks.recv_callback = recv_callback;
callbacks.on_stream_close_callback = on_stream_close_callback;
callbacks.on_frame_recv_callback = on_frame_recv_callback;
callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback;
@ -593,27 +573,34 @@ Http2Upstream::~Http2Upstream()
int Http2Upstream::on_read()
{
int rv = 0;
if((rv = nghttp2_session_recv(session_)) < 0) {
if(rv != NGHTTP2_ERR_EOF) {
ULOG(ERROR, this) << "nghttp2_session_recv() returned error: "
<< nghttp2_strerror(rv);
}
} else if((rv = nghttp2_session_send(session_)) < 0) {
ssize_t rv = 0;
auto bev = handler_->get_bev();
auto input = bufferevent_get_input(bev);
auto inputlen = evbuffer_get_length(input);
auto mem = evbuffer_pullup(input, -1);
rv = nghttp2_session_mem_recv(session_, mem, inputlen);
if(rv < 0) {
ULOG(ERROR, this) << "nghttp2_session_recv() returned error: "
<< nghttp2_strerror(rv);
return -1;
}
evbuffer_drain(input, rv);
rv = nghttp2_session_send(session_);
if(rv < 0) {
ULOG(ERROR, this) << "nghttp2_session_send() returned error: "
<< nghttp2_strerror(rv);
return -1;
}
if(rv == 0) {
if(nghttp2_session_want_read(session_) == 0 &&
nghttp2_session_want_write(session_) == 0 &&
evbuffer_get_length(bufferevent_get_output(handler_->get_bev())) == 0) {
if(LOG_ENABLED(INFO)) {
ULOG(INFO, this) << "No more read/write for this HTTP2 session";
}
rv = -1;
if(nghttp2_session_want_read(session_) == 0 &&
nghttp2_session_want_write(session_) == 0 &&
evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
if(LOG_ENABLED(INFO)) {
ULOG(INFO, this) << "No more read/write for this HTTP2 session";
}
rv = -1;
}
return rv;
return 0;
}
int Http2Upstream::on_write()