From 1cf932a784d192c877d8e50a32ef3361821284d9 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Wed, 21 Sep 2016 20:41:42 +0800 Subject: [PATCH] introduce CONTROL_HEADER_SIZE & CONTROL_HEADER_LENGTH_OFFSET Signed-off-by: Lai Jiangshan --- src/api.h | 10 ++++++++++ src/init.c | 10 +++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/api.h b/src/api.h index 3040693..1b415cd 100644 --- a/src/api.h +++ b/src/api.h @@ -3,6 +3,7 @@ #define APIVERSION 4242 +// control command id enum { GETVERSION, STARTPOD, @@ -29,6 +30,15 @@ enum { REMOVECONTAINER, }; +/* + * control message format + * | ctrl id | length | payload (length-8) | + * | . . . . | . . . . | . . . . . . . . . . . . | + * 0 4 8 length + */ +#define CONTROL_HEADER_SIZE 8 +#define CONTROL_HEADER_LENGTH_OFFSET 4 + /* * stream message format * | stream sequence | length | payload (length-12) | diff --git a/src/init.c b/src/init.c index d5b421d..dfe3a13 100644 --- a/src/init.c +++ b/src/init.c @@ -1179,15 +1179,15 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len) static int hyper_channel_read(struct hyper_event *he, int efd) { struct hyper_buf *buf = &he->rbuf; - uint32_t len = he->ops->len_offset + 4; + uint32_t len; uint8_t data[4]; int size; int ret; fprintf(stdout, "%s\n", __func__); - if (buf->get < len) { - size = nonblock_read(he->fd, buf->data + buf->get, len - buf->get); + if (buf->get < CONTROL_HEADER_SIZE) { + size = nonblock_read(he->fd, buf->data + buf->get, CONTROL_HEADER_SIZE - buf->get); if (size < 0) { return size; } @@ -1197,12 +1197,12 @@ static int hyper_channel_read(struct hyper_event *he, int efd) hyper_send_msg(he->fd, NEXT, 4, data); } buf->get += size; - if (buf->get < len) { + if (buf->get < CONTROL_HEADER_SIZE) { return 0; } } - hyper_getmsg_len(he, &len); + len = hyper_get_be32(buf->data + CONTROL_HEADER_LENGTH_OFFSET); fprintf(stdout, "get length %" PRIu32"\n", len); // test it with '>=' to leave at least one byte in hyper_channel_handle(), // so that hyper_channel_handle() can convert the data to c-string inplace.