- 论坛徽章:
- 324
|
下载了,大致看了看,虽然没什么文档,注释也很少,不过还是比较清晰易懂的。
数据好像只在内存,没有持久化机制。- void mx_process_command(mx_connection_t *conn)
- {
- char *bline, *eline;
- mx_token_t tokens[MX_MAX_TOKENS];
- int tokens_count;
- mx_command_t *cmd;
- int i, movcnt;
- again:
- bline = conn->recvpos;
- eline = memchr(bline, '\n', conn->recvlast - bline);
- if (!eline)
- return;
- conn->recvpos = eline + 1;
- if (eline - bline > 1 && *(eline - 1) == '\r')
- eline--;
- *eline = '\0';
- memset(tokens, 0, sizeof(tokens)); // 这句应该不需要,反正我是不喜欢
-
- tokens_count = mx_tokenize_command(bline, tokens, MX_MAX_TOKENS);
- if (tokens_count == 0) {
- mx_send_reply(conn, "-ERR command invaild");
- return;
- }
-
- cmd = mx_find_command(tokens[0].value, tokens[0].length);
- if (!cmd->name || cmd->argcnt != tokens_count) {
- mx_send_reply(conn, "-ERR command invaild");
- return;
- }
-
- cmd->handler(conn, tokens, tokens_count);
-
- if (conn->recvpos < conn->recvlast) {
- movcnt = conn->recvlast - conn->recvpos;
- memcpy(conn->recvbuf, conn->recvpos, movcnt); // 这里可能有重叠,应该用memmove
- conn->recvpos = conn->recvbuf;
- conn->recvlast = conn->recvbuf + movcnt;
- goto again;
- } else { /* all buffer process finish */
- conn->recvpos = conn->recvbuf;
- conn->recvlast = conn->recvbuf;
- }
- return;
- }
复制代码 |
|