【C/C】用 epoll 写一个 Reactor连接对象、回调和状态机1. Reactor 解决了什么问题裸epoll版本里主循环通常会写成这样if(events[i].data.fdsockfd){accept(...);}else{recv(...);send(...);}这种写法适合演示 API但业务一复杂主循环会越来越臃肿。比如 HTTP 要分“响应头”和“响应体”WebSocket 要分“握手阶段”和“帧数据阶段”长响应还要处理一次send()没写完的情况。Reactor 模式的核心思路是主循环只负责等事件和分发事件真正的业务处理放到回调函数里。本项目的reactor.c已经体现了这个结构epoll_wait()等待事件。监听 fd 触发accept_cb()。客户端 fd 读事件触发recv_cb()。客户端 fd 写事件触发send_cb()。每个连接的数据缓冲、写偏移、状态都放在connections[fd]里。2. connection把连接上下文集中管理server.h里的struct connection是整个 Reactor 的核心数据结构#defineBUFFER_SIZE1024typedefint(*callback_t)(intfd);structconnection{intfd;charrbuffer[BUFFER_SIZE];intrlength;charwbuffer[BUFFER_SIZE];intwlength;intwoffset;callback_tsend_callback;union{callback_trecv_callback;callback_taccept_callback;}rcallback;FILE*fp;longfile_size;longfile_offset;charpayload[BUFFER_SIZE];intpayload_length;intstate;};这里有几个字段很关键rbuffer/rlength保存本次读到的数据。wbuffer/wlength/woffset保存待发送数据和当前发送偏移。recv_callback/send_callback把事件和处理函数绑定起来。state给 HTTP 或 WebSocket 这种分阶段协议使用。fp/file_offset/file_size用于 HTTP 大文件响应分块发送。项目里直接用connections[fd]作为连接表这样通过 fd 可以 O(1) 找到连接上下文。3. 事件注册epoll_ctl 封装成 set_eventreactor.c把epoll_ctl()封装成了set_event()intset_event(intfd,uint32_tevents,intopt){structepoll_eventev;ev.eventsevents;ev.data.fdfd;if(epoll_ctl(epoll_fd,opt,fd,ev)0){perror(epoll_ctl);close(fd);return-1;}return0;}这样添加、修改、删除事件都可以复用同一个函数set_event(client_fd,EPOLLIN,EPOLL_CTL_ADD);set_event(fd,EPOLLOUT,EPOLL_CTL_MOD);set_event(fd,EPOLLIN,EPOLL_CTL_DEL);在 Reactor 中事件不是一次性写死的。比如读到请求后业务生成了响应数据就应该把连接从“监听可读”切换到“监听可写”。4. event_register绑定 fd、事件和回调新连接建立后项目通过event_register()初始化连接上下文intevent_register(intfd,uint32_tevents,callback_trecv_callback,callback_tsend_callback){if(set_event(fd,events,EPOLL_CTL_ADD)0){return-1;}connections[fd].fdfd;connections[fd].rcallback.recv_callbackrecv_cb;connections[fd].send_callbacksend_cb;memset(connections[fd].rbuffer,0,BUFFER_SIZE);connections[fd].rlength0;memset(connections[fd].wbuffer,0,BUFFER_SIZE);connections[fd].wlength0;connections[fd].woffset0;connections[fd].fpNULL;connections[fd].file_offset0;connections[fd].file_size0;connections[fd].payload_length0;connections[fd].state0;if(eventsEPOLLIN){connections[fd].rcallback.recv_callbackrecv_callback;}if(eventsEPOLLOUT){connections[fd].send_callbacksend_callback;}return0;}这段代码做了三件事把 fd 加入 epoll。初始化连接的读写缓存和状态。绑定读写回调函数。5. 主循环只做事件分发Reactor 的主循环不再直接写业务逻辑而是判断 fd 类型和事件类型然后调用对应回调while(1){intnepoll_wait(epoll_fd,events,MAX_EVENTS,-1);if(n0){perror(epoll_wait);break;}for(inti0;in;i){intfdevents[i].data.fd;if(find_server_fd(fd)!-1){connections[fd].rcallback.accept_callback(fd);}else{if(events[i].eventsEPOLLIN){connections[fd].rcallback.recv_callback(fd);}if(events[i].eventsEPOLLOUT){connections[fd].send_callback(fd);}}}}这种结构的好处是清晰事件循环是事件循环协议处理是协议处理两者不混在一起。6. recv_cb 和 send_cb读写事件如何切换读事件回调把数据读入rbuffer然后交给业务函数处理。当前项目里接入的是 WebSocketintrecv_cb(intfd){ssize_tbytes_readrecv(fd,connections[fd].rbuffer,BUFFER_SIZE,0);if(bytes_read0){set_event(fd,EPOLLIN,EPOLL_CTL_DEL);close(fd);return-1;}connections[fd].rlengthbytes_read;websocket_request(connections[fd]);set_event(fd,EPOLLOUT,EPOLL_CTL_MOD);return0;}当业务处理后需要响应客户端就把事件改成EPOLLOUT。写事件回调负责把wbuffer中的数据写出去ssize_tbytes_sentsend(fd,connections[fd].wbufferconnections[fd].woffset,connections[fd].wlength-connections[fd].woffset,MSG_NOSIGNAL);connections[fd].woffsetbytes_sent;if(connections[fd].woffsetconnections[fd].wlength){connections[fd].woffset0;connections[fd].wlength0;}这里的woffset很重要。真实网络里一次send()不一定能把所有数据写完必须记录已经写了多少。7. 状态机示例HTTP 图片响应webserver.c展示了另一个典型业务HTTP 返回一张c1000k.jpg。它把响应拆成两个阶段if(conn-state0){conn-fpfopen(c1000k.jpg,r);fseek(conn-fp,0,SEEK_END);conn-file_sizeftell(conn-fp);fseek(conn-fp,0,SEEK_SET);conn-file_offset0;intnsprintf(conn-wbuffer,HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\nContent-Length: %ld\r\n\r\n,conn-file_size);conn-wlengthn;conn-state1;}elseif(conn-state1){intnfread(conn-wbuffer,1,BUFFER_SIZE,conn-fp);conn-wlengthn;conn-file_offsetn;}state 0时准备响应头state 1时分块读取图片内容。这个例子说明 Reactor 不是只能处理简单 echo它能自然承载“多次读写才能完成”的协议。8. 编译运行当前reactor.c中接入的是 WebSocket 业务gcc reactor.c websocket.c-owebsocket-lssl-lcrypto./websocket服务端默认监听 8080Server is listening on port 8080如果你要把 HTTP 业务也接进 Reactor可以把recv_cb()/send_cb()中的业务函数从websocket_request()/websocket_response()替换或抽象成可配置回调再链接webserver.c。9. 小结Reactor 的核心不是某个 API而是一种代码组织方式epoll负责发现事件。Reactor 主循环负责分发事件。callback 负责处理事件。connection保存每个连接的上下文。state负责表达协议阶段。学习链接: https://github.com/0voice