From 3562427afc4f706d3f2bfbded1919e347380d75a Mon Sep 17 00:00:00 2001 From: kedixa <1204837541@qq.com> Date: Mon, 22 Aug 2022 21:53:06 +0800 Subject: [PATCH] simplify code --- src/protocol/redis_parser.c | 66 +++++++++++-------------------------- src/protocol/redis_parser.h | 1 - 2 files changed, 19 insertions(+), 48 deletions(-) diff --git a/src/protocol/redis_parser.c b/src/protocol/redis_parser.c index a0f0d953..31c22749 100644 --- a/src/protocol/redis_parser.c +++ b/src/protocol/redis_parser.c @@ -309,7 +309,6 @@ void redis_parser_init(redis_parser_t *parser) INIT_LIST_HEAD(&parser->read_list); parser->msgidx = 0; parser->cmd = '\0'; - parser->is_inline = 0; parser->nchar = 0; parser->findidx = 0; } @@ -387,6 +386,7 @@ static int __redis_split_inline_command(redis_parser_t *parser) if (arr_size == 0) { parser->msgsize = 0; + parser->msgidx = 0; return 0; } @@ -419,63 +419,17 @@ static int __redis_split_inline_command(redis_parser_t *parser) return 1; } -static int __redis_append_inline_command(const void *buf, size_t *size, - redis_parser_t *parser) -{ - const char *msg = (const char *)buf; - size_t sz = 0; - - while (sz < *size && msg[sz] != '\n') - sz++; - - if (parser->msgsize + sz > parser->bufsize) - { - size_t new_size = MAX(REDIS_MSGBUF_INIT_SIZE, 2 * parser->bufsize); - void *new_base; - - while (new_size < parser->msgsize + sz) - new_size *= 2; - - new_base = realloc(parser->msgbuf, new_size); - if (!new_base) - return -1; - - parser->msgbuf = new_base; - parser->bufsize = new_size; - } - - memcpy((char *)parser->msgbuf + parser->msgsize, buf, sz); - parser->msgsize += sz; - - if (sz == *size) - return 0; - - *size = sz + 1; - - msg = (const char *)parser->msgbuf; - if (parser->msgsize > 0 && msg[parser->msgsize - 1] == '\r') - parser->msgsize--; - - return __redis_split_inline_command(parser); -} - int redis_parser_append_message(const void *buf, size_t *size, redis_parser_t *parser) { size_t msgsize_bak = parser->msgsize; - if (msgsize_bak == 0 && *size > 0 && *(char *)buf != '*') - parser->is_inline = 1; - if (parser->status == REDIS_PARSE_END) { *size = 0; return 1; } - if (parser->is_inline) - return __redis_append_inline_command(buf, size, parser); - if (parser->msgsize + *size > parser->bufsize) { size_t new_size = MAX(REDIS_MSGBUF_INIT_SIZE, 2 * parser->bufsize); @@ -495,6 +449,24 @@ int redis_parser_append_message(const void *buf, size_t *size, memcpy((char *)parser->msgbuf + parser->msgsize, buf, *size); parser->msgsize += *size; + if (parser->msgsize && *(const char *)parser->msgbuf != '*') + { + while (parser->msgidx < parser->msgsize && + *((const char *)parser->msgbuf + parser->msgidx) != '\n') + { + parser->msgidx++; + } + + if (parser->msgidx == parser->msgsize) + return 0; + + parser->msgidx++; + parser->msgsize = parser->msgidx; + *size = parser->msgsize - msgsize_bak; + + return __redis_split_inline_command(parser); + } + do { int ret = __redis_parser_forward(parser); diff --git a/src/protocol/redis_parser.h b/src/protocol/redis_parser.h index 5806005c..dcb6cf49 100644 --- a/src/protocol/redis_parser.h +++ b/src/protocol/redis_parser.h @@ -55,7 +55,6 @@ typedef struct __redis_parser int nleft; int nchar; char cmd; - char is_inline; redis_reply_t reply; } redis_parser_t;