bugfix: reactor网络模型的的半包解析错误问题。
全量持久化时清除增量持久化的记录。
This commit is contained in:
45
reactor.c
45
reactor.c
@@ -28,15 +28,15 @@
|
||||
#if ENABLE_KVSTORE
|
||||
|
||||
// typedef int (*msg_handler)(char *msg, int length, char *response);
|
||||
typedef int (*msg_handler)(char *request, int request_length, int *consumed_out, char *response, int *response_length);
|
||||
typedef int (*msg_handler)(char *request, int request_length, char *response, int *response_length);
|
||||
|
||||
|
||||
static msg_handler kvs_handler;
|
||||
|
||||
// 0 need more, -1 error, =1 suc
|
||||
int kvs_request(struct conn *c) {
|
||||
int consumed_out;
|
||||
int ret = kvs_handler(c->rbuffer, c->rlength, &consumed_out, c->wbuffer, &c->wlength);
|
||||
|
||||
int consumed_out = kvs_handler(c->rbuffer, c->rlength, c->wbuffer, &c->wlength);
|
||||
return consumed_out;
|
||||
}
|
||||
|
||||
int kvs_response(struct conn *c) {
|
||||
@@ -137,9 +137,17 @@ int accept_cb(int fd) {
|
||||
|
||||
|
||||
int recv_cb(int fd) {
|
||||
struct conn *c = &conn_list[fd];
|
||||
int avail = BUFFER_LENGTH - c->rlength;
|
||||
printf("avail: %d\n", avail);
|
||||
if (avail <= 0) {
|
||||
// 缓冲满了还没解析出来:协议异常或包过大
|
||||
close(fd);
|
||||
epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(conn_list[fd].rbuffer, 0, BUFFER_LENGTH );
|
||||
int count = recv(fd, conn_list[fd].rbuffer, BUFFER_LENGTH, 0);
|
||||
int count = recv(fd, c->rbuffer + c->rlength, avail, 0);
|
||||
if (count == 0) { // disconnect
|
||||
//printf("client disconnect: %d\n", fd);
|
||||
close(fd);
|
||||
@@ -157,7 +165,7 @@ int recv_cb(int fd) {
|
||||
}
|
||||
|
||||
|
||||
conn_list[fd].rlength = count;
|
||||
c->rlength += count;
|
||||
//printf("RECV: %s\n", conn_list[fd].rbuffer);
|
||||
|
||||
#if 0 // echo
|
||||
@@ -176,8 +184,27 @@ int recv_cb(int fd) {
|
||||
ws_request(&conn_list[fd]);
|
||||
|
||||
#elif ENABLE_KVSTORE
|
||||
int consumed = kvs_request(c);
|
||||
if(consumed < 0){
|
||||
close(fd);
|
||||
epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
kvs_request(&conn_list[fd]);
|
||||
// 清理 buffer
|
||||
if (consumed > 0 && consumed < c->rlength) {
|
||||
// 有剩余未处理数据,搬移到 buffer 头部
|
||||
int left = c->rlength - consumed;
|
||||
if (left > 0) memmove(c->rbuffer, c->rbuffer + consumed, left);
|
||||
c->rlength = left;
|
||||
if (c->wlength > 0) set_event(fd, EPOLLOUT, 0);
|
||||
return count;
|
||||
|
||||
}else{
|
||||
c->rlength = 0;
|
||||
if(c->wlength > 0) set_event(fd, EPOLLOUT, 0);
|
||||
return count;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -222,7 +249,7 @@ int send_cb(int fd) {
|
||||
set_event(fd, EPOLLIN, 0);
|
||||
}
|
||||
#else
|
||||
|
||||
// printf("wlength: %d\n", conn_list[fd].wlength);
|
||||
if (conn_list[fd].wlength != 0) {
|
||||
count = send(fd, conn_list[fd].wbuffer, conn_list[fd].wlength, 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user