1. 三个struct结构体
1. 事件结构体,每个fd对应一个event
struct ntyevent {
int fd; //每个event对应一个fd
int events;
void *arg;
int (*callback)(int fd, int events, void *arg); //回调函数
int status;
char buffer[BUFFER_LENGTH]; 读/写buffer
int length;
long last_active;
};
2. 采用一个数组挂一个链表的形式保存event块
struct eventblock {
struct eventblock *next;
struct ntyevent *events;//链表长度小于1024
};
3. reactor结构,一个reactor管理所有event
struct ntyreactor {
int epfd;
int blkcnt;
struct eventblock *evblk; //fd --> 100w
};
2. 主函数流程
1. 初始化监听socket
int sockfd = init_sock(port); // socket, bind ,listenfd
2. 创建reactor并初始化
struct ntyreactor *reactor = (struct ntyreactor*)malloc(sizeof(struct ntyreactor));
n

本文介绍了C++实现的Reactor事件驱动框架,包括三个关键结构体:事件结构体、事件块和Reactor结构。主函数流程涉及监听socket初始化、Reactor创建、事件处理循环以及资源释放。同时详细阐述了accept、recv和send的回调函数实现。
最低0.47元/天 解锁文章
5万+

被折叠的 条评论
为什么被折叠?



