doc: Update tutorials

This commit is contained in:
Tatsuhiro Tsujikawa 2014-08-23 17:55:17 +09:00
parent 53ee21caa9
commit 3daa6f2c30
2 changed files with 71 additions and 48 deletions

View File

@ -156,15 +156,30 @@ finished successfully. We first initialize nghttp2 session object in
static void initialize_nghttp2_session(http2_session_data *session_data) static void initialize_nghttp2_session(http2_session_data *session_data)
{ {
nghttp2_session_callbacks callbacks = {0}; nghttp2_session_callbacks *callbacks;
callbacks.send_callback = send_callback; nghttp2_session_callbacks_new(&callbacks);
callbacks.on_frame_recv_callback = on_frame_recv_callback;
callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback; nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
callbacks.on_stream_close_callback = on_stream_close_callback;
callbacks.on_header_callback = on_header_callback; nghttp2_session_callbacks_set_on_frame_recv_callback
callbacks.on_begin_headers_callback = on_begin_headers_callback; (callbacks, on_frame_recv_callback);
nghttp2_session_client_new(&session_data->session, &callbacks, session_data);
nghttp2_session_callbacks_set_on_data_chunk_recv_callback
(callbacks, on_data_chunk_recv_callback);
nghttp2_session_callbacks_set_on_stream_close_callback
(callbacks, on_stream_close_callback);
nghttp2_session_callbacks_set_on_header_callback
(callbacks, on_header_callback);
nghttp2_session_callbacks_set_on_begin_headers_callback
(callbacks, on_begin_headers_callback);
nghttp2_session_client_new(&session_data->session, callbacks, session_data);
nghttp2_session_callbacks_del(callbacks);
} }
Since we are creating client, we use `nghttp2_session_client_new()` to Since we are creating client, we use `nghttp2_session_client_new()` to
@ -291,11 +306,9 @@ frames. The ``session_send()`` function is defined as follows::
} }
The `nghttp2_session_send()` function serializes the frame into wire The `nghttp2_session_send()` function serializes the frame into wire
format and call :member:`nghttp2_session_callbacks.send_callback` with format and call ``send_callback()`` function of type
it. We set ``send_callback()`` function to :type:`nghttp2_send_callback`. The ``send_callback()`` is defined as
:member:`nghttp2_session_callbacks.send_callback` in follows::
``initialize_nghttp2_session()`` function described earlier. It is
defined as follows::
static ssize_t send_callback(nghttp2_session *session, static ssize_t send_callback(nghttp2_session *session,
const uint8_t *data, size_t length, const uint8_t *data, size_t length,
@ -311,15 +324,14 @@ Since we use bufferevent to abstract network I/O, we just write the
data to the bufferevent object. Note that `nghttp2_session_send()` data to the bufferevent object. Note that `nghttp2_session_send()`
continues to write all frames queued so far. If we were writing the continues to write all frames queued so far. If we were writing the
data to the non-blocking socket directly using ``write()`` system call data to the non-blocking socket directly using ``write()`` system call
in the :member:`nghttp2_session_callbacks.send_callback`, we will in the ``send_callback()``, we will surely get ``EAGAIN`` or
surely get ``EAGAIN`` or ``EWOULDBLOCK`` since the socket has limited ``EWOULDBLOCK`` since the socket has limited send buffer. If that
send buffer. If that happens, we can return happens, we can return :macro:`NGHTTP2_ERR_WOULDBLOCK` to signal the
:macro:`NGHTTP2_ERR_WOULDBLOCK` to signal the nghttp2 library to stop nghttp2 library to stop sending further data. But writing to the
sending further data. But writing to the bufferevent, we have to bufferevent, we have to regulate the amount data to be buffered by
regulate the amount data to be buffered by ourselves to avoid possible ourselves to avoid possible huge memory consumption. In this example
huge memory consumption. In this example client, we do not limit client, we do not limit anything. To see how to regulate the amount of
anything. To see how to regulate the amount of buffered data, see the buffered data, see the ``send_callback()`` in the server tutorial.
``send_callback()`` in the server tutorial.
The third bufferevent callback is ``writecb()``, which is invoked when The third bufferevent callback is ``writecb()``, which is invoked when
all data written in the bufferevent output buffer have been sent:: all data written in the bufferevent output buffer have been sent::

View File

@ -229,14 +229,27 @@ We initialize a nghttp2 session object which is done in
static void initialize_nghttp2_session(http2_session_data *session_data) static void initialize_nghttp2_session(http2_session_data *session_data)
{ {
nghttp2_session_callbacks callbacks = {0}; nghttp2_session_callbacks *callbacks;
callbacks.send_callback = send_callback; nghttp2_session_callbacks_new(&callbacks);
callbacks.on_frame_recv_callback = on_frame_recv_callback;
callbacks.on_stream_close_callback = on_stream_close_callback; nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
callbacks.on_header_callback = on_header_callback;
callbacks.on_begin_headers_callback = on_begin_headers_callback; nghttp2_session_callbacks_set_on_frame_recv_callback
nghttp2_session_server_new(&session_data->session, &callbacks, session_data); (callbacks, on_frame_recv_callback);
nghttp2_session_callbacks_set_on_stream_close_callback
(callbacks, on_stream_close_callback);
nghttp2_session_callbacks_set_on_header_callback
(callbacks, on_header_callback);
nghttp2_session_callbacks_set_on_begin_headers_callback
(callbacks, on_begin_headers_callback);
nghttp2_session_server_new(&session_data->session, callbacks, session_data);
nghttp2_session_callbacks_del(callbacks);
} }
Since we are creating a server, the nghttp2 session object is created using Since we are creating a server, the nghttp2 session object is created using
@ -317,11 +330,9 @@ frames. The ``session_send()`` function is defined as follows::
} }
The `nghttp2_session_send()` function serializes the frame into wire The `nghttp2_session_send()` function serializes the frame into wire
format and calls :member:`nghttp2_session_callbacks.send_callback` with format and calls ``send_callback()`` of type
it. We set the ``send_callback()`` function to :type:`nghttp2_send_callback`. The ``send_callback()`` is defined as
:member:`nghttp2_session_callbacks.send_callback` in follows::
``initialize_nghttp2_session()`` function described earlier. It is
defined as follows::
static ssize_t send_callback(nghttp2_session *session, static ssize_t send_callback(nghttp2_session *session,
const uint8_t *data, size_t length, const uint8_t *data, size_t length,
@ -338,20 +349,20 @@ defined as follows::
return length; return length;
} }
Since we use bufferevent to abstract network I/O, we just write the data to Since we use bufferevent to abstract network I/O, we just write the
the bufferevent object. Note that `nghttp2_session_send()` continues to write data to the bufferevent object. Note that `nghttp2_session_send()`
all frames queued so far. If we were writing the data to a non-blocking socket continues to write all frames queued so far. If we were writing the
directly using ``write()`` system call in the data to a non-blocking socket directly using ``write()`` system call
:member:`nghttp2_session_callbacks.send_callback`, we would surely get in the ``send_callback()``, we would surely get ``EAGAIN`` or
``EAGAIN`` or ``EWOULDBLOCK`` back since the socket has limited send ``EWOULDBLOCK`` back since the socket has limited send buffer. If that
buffer. If that happens, we can return :macro:`NGHTTP2_ERR_WOULDBLOCK` to happens, we can return :macro:`NGHTTP2_ERR_WOULDBLOCK` to signal the
signal the nghttp2 library to stop sending further data. But when writing to nghttp2 library to stop sending further data. But when writing to the
the bufferevent, we have to regulate the amount data to get buffered ourselves bufferevent, we have to regulate the amount data to get buffered
to avoid using huge amounts of memory. To achieve this, we check the size of ourselves to avoid using huge amounts of memory. To achieve this, we
the output buffer and if it reaches more than or equal to check the size of the output buffer and if it reaches more than or
``OUTPUT_WOULDBLOCK_THRESHOLD`` bytes, we stop writing data and return equal to ``OUTPUT_WOULDBLOCK_THRESHOLD`` bytes, we stop writing data
:macro:`NGHTTP2_ERR_WOULDBLOCK` to tell the library to stop calling and return :macro:`NGHTTP2_ERR_WOULDBLOCK` to tell the library to stop
send_callback. calling send_callback.
The next bufferevent callback is ``readcb()``, which is invoked when The next bufferevent callback is ``readcb()``, which is invoked when
data is available to read in the bufferevent input buffer:: data is available to read in the bufferevent input buffer::