resp协议实现和使用hiredis进行测试

This commit is contained in:
1iaan
2026-01-20 11:51:38 +00:00
parent bb2c4275cb
commit f031e107b5
15 changed files with 1010 additions and 498 deletions

View File

@@ -238,43 +238,62 @@ int send_cb(int fd) {
#endif
int count = 0;
struct conn *c = &conn_list[fd];
int sent_total = 0;
#if 0
if (conn_list[fd].status == 1) {
//printf("SEND: %s\n", conn_list[fd].wbuffer);
count = send(fd, conn_list[fd].wbuffer, conn_list[fd].wlength, 0);
set_event(fd, EPOLLOUT, 0);
} else if (conn_list[fd].status == 2) {
set_event(fd, EPOLLOUT, 0);
} else if (conn_list[fd].status == 0) {
pthread_mutex_lock(&c->g_sync_lock);
if (conn_list[fd].wlength != 0) {
count = send(fd, conn_list[fd].wbuffer, conn_list[fd].wlength, 0);
}
set_event(fd, EPOLLIN, 0);
while (c->wlength > 0) {
ssize_t n = send(fd, c->wbuffer, (size_t)c->wlength, MSG_NOSIGNAL);
if (n > 0) {
sent_total += (int)n;
if (n == c->wlength) {
/* 全部发完 */
c->wlength = 0;
break;
}
/* 只发了一部分:把剩余数据搬到 buffer 头部 */
int left = c->wlength - (int)n;
memmove(c->wbuffer, c->wbuffer + n, (size_t)left);
c->wlength = left;
/* 不要在这里死循环占用 CPU交给下一次 EPOLLOUT */
break;
}
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* 暂时发不出去,等下一次可写事件 */
pthread_mutex_unlock(&c->g_sync_lock);
set_event(fd, EPOLLOUT, 0);
return sent_total;
}
/* 对端断开 / 其他错误 */
int e = errno;
pthread_mutex_unlock(&c->g_sync_lock);
printf("send fd=%d errno=%d %s\n", fd, e, strerror(e));
close(fd);
epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
return 0;
}
break;
}
#else
// printf("wlength: %d\n", conn_list[fd].wlength);
pthread_mutex_unlock(&c->g_sync_lock);
pthread_mutex_lock(&conn_list[fd].g_sync_lock);
if (conn_list[fd].wlength != 0) {
// for(int i = 0;i < conn_list[fd].wlength; ++i){
// printf("%02x", conn_list[fd].wbuffer[i]);
// }
// printf("\n");
count = send(fd, conn_list[fd].wbuffer, conn_list[fd].wlength, 0);
conn_list[fd].wlength = 0;
}
pthread_mutex_unlock(&conn_list[fd].g_sync_lock);
set_event(fd, EPOLLIN, 0);
if (c->wlength > 0) {
/* 还有没发完,继续监听可写 */
set_event(fd, EPOLLOUT, 0);
} else {
/* 发完了,回到读 */
set_event(fd, EPOLLIN, 0);
}
#endif
//set_event(fd, EPOLLOUT, 0);
return count;
return sent_total;
}
// wakup fd