mirror of
https://github.com/nghttp2/nghttp2.git
synced 2025-12-07 10:38:53 +08:00
Added callback functions for DATA frames. Fixed unpacking length field.
This commit is contained in:
@@ -185,12 +185,35 @@ typedef void (*spdylay_on_invalid_ctrl_recv_callback)
|
||||
typedef void (*spdylay_on_ping_recv_callback)
|
||||
(spdylay_session *session, const struct timespec *rtt, void *user_data);
|
||||
|
||||
/*
|
||||
* Callback function invoked when data chunk of DATA frame is
|
||||
* received. |stream_id| is the stream ID of this DATA frame belongs
|
||||
* to. |flags| is the flags of DATA frame which this data chunk is
|
||||
* contained. flags & SPDYLAY_FLAG_FIN does not necessarily mean this
|
||||
* chunk of data is the last one in the stream. You should use
|
||||
* spdylay_on_data_recv_callback to know all data frame is received
|
||||
* whose flags contains SPDYLAY_FLAG_FIN.
|
||||
*/
|
||||
typedef void (*spdylay_on_data_chunk_recv_callback)
|
||||
(spdylay_session *session, int32_t stream_id, uint8_t flags,
|
||||
const uint8_t *data, size_t len, void *user_data);
|
||||
|
||||
/*
|
||||
* Callback function invoked when DATA frame is received. The actual
|
||||
* data it contains are received by spdylay_on_data_recv_callback.
|
||||
*/
|
||||
typedef void (*spdylay_on_data_recv_callback)
|
||||
(spdylay_session *session, int32_t stream_id, uint8_t flags, int32_t length,
|
||||
void *user_data);
|
||||
|
||||
typedef struct {
|
||||
spdylay_send_callback send_callback;
|
||||
spdylay_recv_callback recv_callback;
|
||||
spdylay_on_ctrl_recv_callback on_ctrl_recv_callback;
|
||||
spdylay_on_invalid_ctrl_recv_callback on_invalid_ctrl_recv_callback;
|
||||
spdylay_on_ping_recv_callback on_ping_recv_callback;
|
||||
spdylay_on_data_chunk_recv_callback on_data_chunk_recv_callback;
|
||||
spdylay_on_data_recv_callback on_data_recv_callback;
|
||||
} spdylay_session_callbacks;
|
||||
|
||||
int spdylay_session_client_new(spdylay_session **session_ptr,
|
||||
|
||||
@@ -57,9 +57,9 @@ static void spdylay_frame_unpack_ctrl_hd(spdylay_ctrl_hd *hd,
|
||||
const uint8_t* buf)
|
||||
{
|
||||
hd->version = spdylay_get_uint16(buf) & SPDYLAY_VERSION_MASK;
|
||||
hd->type = spdylay_get_uint16(buf+2);
|
||||
hd->type = spdylay_get_uint16(&buf[2]);
|
||||
hd->flags = buf[4];
|
||||
hd->length = spdylay_get_uint32(buf+5) & SPDYLAY_LENGTH_MASK;
|
||||
hd->length = spdylay_get_uint32(&buf[4]) & SPDYLAY_LENGTH_MASK;
|
||||
}
|
||||
|
||||
static ssize_t spdylay_frame_alloc_pack_nv(uint8_t **buf_ptr,
|
||||
|
||||
@@ -717,6 +717,7 @@ int spdylay_session_on_rst_stream_received(spdylay_session *session,
|
||||
spdylay_frame *frame)
|
||||
{
|
||||
spdylay_session_close_stream(session, frame->rst_stream.stream_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int spdylay_session_on_ping_received(spdylay_session *session,
|
||||
@@ -881,10 +882,18 @@ int spdylay_session_process_ctrl_frame(spdylay_session *session)
|
||||
|
||||
int spdylay_session_process_data_frame(spdylay_session *session)
|
||||
{
|
||||
/* printf("Received data frame, stream_id %d, %zu bytes, fin=%d\n", */
|
||||
/* spdylay_get_uint32(session->iframe.headbuf), */
|
||||
/* session->iframe.len, */
|
||||
/* session->iframe.headbuf[4] & SPDYLAY_FLAG_FIN); */
|
||||
if(session->callbacks.on_data_recv_callback) {
|
||||
int32_t stream_id;
|
||||
uint8_t flags;
|
||||
int32_t length;
|
||||
stream_id = spdylay_get_uint32(session->iframe.headbuf) &
|
||||
SPDYLAY_STREAM_ID_MASK;
|
||||
flags = session->iframe.headbuf[4];
|
||||
length = spdylay_get_uint32(&session->iframe.headbuf[4]) &
|
||||
SPDYLAY_LENGTH_MASK;
|
||||
session->callbacks.on_data_recv_callback
|
||||
(session, stream_id, flags, length, session->user_data);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -909,7 +918,8 @@ int spdylay_session_recv(spdylay_session *session)
|
||||
}
|
||||
}
|
||||
session->iframe.state = SPDYLAY_RECV_PAYLOAD;
|
||||
payloadlen = spdylay_get_uint32(&session->ibuf.mark[4]) & 0xffffff;
|
||||
payloadlen = spdylay_get_uint32(&session->ibuf.mark[4]) &
|
||||
SPDYLAY_LENGTH_MASK;
|
||||
memcpy(session->iframe.headbuf, session->ibuf.mark, SPDYLAY_HEAD_LEN);
|
||||
session->ibuf.mark += SPDYLAY_HEAD_LEN;
|
||||
if(spdylay_frame_is_ctrl_frame(session->iframe.headbuf[0])) {
|
||||
@@ -921,11 +931,6 @@ int spdylay_session_recv(spdylay_session *session)
|
||||
}
|
||||
session->iframe.off = 0;
|
||||
} else {
|
||||
int32_t stream_id;
|
||||
/* data frame */
|
||||
/* For data frame, We dont' buffer data. Instead, just pass
|
||||
received data to callback function. */
|
||||
stream_id = spdylay_get_uint32(session->iframe.headbuf) & 0x7fffffff;
|
||||
/* TODO validate stream id here */
|
||||
session->iframe.len = payloadlen;
|
||||
session->iframe.off = 0;
|
||||
@@ -947,8 +952,21 @@ int spdylay_session_recv(spdylay_session *session)
|
||||
}
|
||||
bufavail = spdylay_inbound_buffer_avail(&session->ibuf);
|
||||
readlen = bufavail < rempayloadlen ? bufavail : rempayloadlen;
|
||||
if(session->iframe.buf != NULL) {
|
||||
if(spdylay_frame_is_ctrl_frame(session->iframe.headbuf[0])) {
|
||||
memcpy(session->iframe.buf, session->ibuf.mark, readlen);
|
||||
} else if(session->callbacks.on_data_chunk_recv_callback) {
|
||||
int32_t stream_id;
|
||||
uint8_t flags;
|
||||
/* For data frame, We don't buffer data. Instead, just pass
|
||||
received data to callback function. */
|
||||
stream_id = spdylay_get_uint32(session->iframe.headbuf) &
|
||||
SPDYLAY_STREAM_ID_MASK;
|
||||
flags = session->iframe.headbuf[4];
|
||||
session->callbacks.on_data_chunk_recv_callback(session, stream_id,
|
||||
flags,
|
||||
session->ibuf.mark,
|
||||
readlen,
|
||||
session->user_data);
|
||||
}
|
||||
session->iframe.off += readlen;
|
||||
session->ibuf.mark += readlen;
|
||||
|
||||
Reference in New Issue
Block a user