高周波做网站,金融投资风险公司网站源码,提升网站建设品质信息,企业展厅设计网文件描述符处理与回调函数
一、主要概念
反应堆模型#xff1a;一种处理系统事件或网络事件的模型#xff0c;当文件描述符被激活时#xff0c;可以检测到文件描述符#xff1a;在操作系统中#xff0c;用于标识打开的文件、套接字等的一种数据类型 处理激活的文件描述符…文件描述符处理与回调函数
一、主要概念
反应堆模型一种处理系统事件或网络事件的模型当文件描述符被激活时可以检测到文件描述符在操作系统中用于标识打开的文件、套接字等的一种数据类型 处理激活的文件描述符的函数当文件描述符被激活时需要有一个函数来处理这些事件dispatch函数用于分发或处理不同类型事件的函数channel结构体存储与文件描述符相关的事件处理动作的结构体回调函数在初始化channel对象时指定的读回调和写回调用于处理不同类型的事件select函数用于检测多个文件描述符的状态看是否有数据可读或可写fd_set集合用于存储文件描述符的集合通过宏函数FD_ISSET判断文件描述符是否被触发
二、处理流程
当反应堆模型启动检测到被激活的文件描述符调用dispatch函数得到文件描述符fd根据fd从channelMap中取出对应的channel判断是读事件还是写事件调用对应的回调函数处理事件在select函数中通过fd_set集合判断是否有数据可读或可写调用eventActivate函数处理事件在epoll函数中通过epoll_wait进行检测遍历返回的events数组调用eventActivate函数处理事件在poll函数中通过poll进行检测遍历返回的事件列表,调用eventActivate函数处理事件
三、注意事项
在调用回调函数时需要传入注册时指定的参数在使用回调函数时需要注意处理函数的参数和返回值
四、概括
本文主要介绍了在EventLoop中处理被激活的文件描述符的事件和回调机制当反应堆模型启动时可以检测到被激活的文件描述符并使用dispatch函数获取文件描述符。(EventLoop初始化和启动)根据文件描述符从ChannelMap中取出对应的channel判断是读事件还是写事件并调用相应的回调函数处理
核心观点:
反应堆模型启动后可以检测到被激活的文件描述符使用dispatch函数获取文件描述符并根据文件描述符从ChannelMap中取出对应的channel根据channel判断是读事件还是写事件并调用相应的回调函数处理在select函数中通过fd_set集合判断是否有数据可读或可写调用eventActivate函数处理事件在epoll函数中通过epoll_wait进行检测遍历返回的events数组调用eventActivate函数处理事件在poll函数中通过poll进行检测遍历返回的事件列表,调用eventActivate函数处理事件
回顾ChannelMap 模块的实现和Channel 模块的实现 Channel.h
// 定义函数指针
typedef int(*handleFunc)(void* arg);// 定义文件描述符的读写事件
enum FDEvent {TimeOut 0x01;ReadEvent 0x02;WriteEvent 0x04;
};struct Channel {// 文件描述符int fd;// 事件int events;// 回调函数handleFunc readCallback;// 读回调handleFunc writeCallback;// 写回调// 回调函数的参数void* arg;
}; 在EventLoop中处理被激活的文件描述符的事件
EpollLoop.h
// 处理被激活的文件描述符fd
int eventActivate(struct EventLoop* evLoop,int fd,int event); EpollLoop.c
// 处理被激活的文件描述符fd
int eventActivate(struct EventLoop* evLoop,int fd,int event) {if(fd 0 || evLoop NULL) {return -1;}// 取出channelstruct Channel* channel evLoop-channelMap-list[fd];assert(channel-fd fd);if(event ReadEvent channel-readCallback) {channel-readCallback(channel-arg);}if(event WriteEvent channel-writeCallback) {channel-writeCallback(channel-arg);}return 0;
}
回顾Dispatcher模块,Dispatcher模块的实现思路和定义 补充代码
SelectDispatcher.c
static int selectDispatch(struct EventLoop* evLoop,int timeout) {struct SelectData* data (struct SelectData*)evLoop-dispatcherData;struct timeval val;val.tv_sec timeout;val.tv_usec 0;fd_set rdtmp data-readSet;fd_set wrtmp data-writeSet;int count select(Max,rdtmp,wrtmp,NULL,val);if(count -1) {perror(select);exit(0);}for(int i0;iMax;i) { if(FD_ISSET(i,rdtmp)) {// 已续写...eventActivate(evLoop,i,ReadEvent);}if(FD_ISSET(i,wrtmp)) {// 已续写...eventActivate(evLoop,i,WriteEvent);}}return 0;
}
PollDispatcher.c
static int pollDispatch(struct EventLoop* evLoop,int timeout) {struct PollData* data (struct PollData*)evLoop-dispatcherData;int count poll(data-fds,data-maxfd 1,timeout * 1000);if(count -1) {perror(poll);exit(0);}for(int i0;idata-maxfd;i) {if(data-fds[i].fd -1) {continue;}if(data-fds[i].revents POLLIN) {// 已续写...eventActivate(evLoop,data-fds[i].fd,ReadEvent);}if(data-fds[i].revents POLLOUT) {// 已续写...eventActivate(evLoop,data-fds[i].fd,WriteEvent);}}return 0;
}
EpollDispatcher.c
static int epollDispatch(struct EventLoop* evLoop,int timeout) {struct EpollData* data (struct EpollData*)evLoop-dispatcherData;int count epoll_wait(data-epfd,data-events,Max,timeout * 1000);for(int i0;icount;i) {int events data-events[i].events;int fd data-events[i].data.fd;if(events EPOLLERR || events EPOLLHUP) {// 对方断开了连接删除 fd// epollRemove(evLoop-channels[fd],evLoop);continue;}if(events EPOLLIN) {// 已续写...eventActivate(evLoop,fd,ReadEvent);}if(events EPOLLOUT) {// 已续写...eventActivate(evLoop,fd,WriteEvent);}}return 0;
}
五、回顾select函数、poll函数、epoll_wait函数(CodeGeex生成) select函数
Linux 中的 select 函数主要用于等待多个文件描述符(fd)就绪,从而可以一次性处理多个文件描述符。select 函数会检查指定的文件描述符是否就绪,如果其中任何一个是就绪的,则 select 函数返回。select 函数的原型为:
#include sys/select.h
int select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,const struct timeval *timeout);其中,nfds 表示要检查的文件描述符数量,readfds、writefds 和 exceptfds 分别表示要检查的读、写和异常文件描述符集合,timeout 表示等待的时间限制。
例如,下面代码等待两个文件描述符(fd)就绪,最多等待 5 秒钟:
int fd1 open(file1, O_RDONLY);
int fd2 open(file2, O_RDONLY);fd_set readfds;
FD_ZERO(readfds);
FD_SET(fd1, readfds);
FD_SET(fd2, readfds);struct timeval timeout;
timeout.tv_sec 5;
timeout.tv_usec 0;int ret select(2, readfds, NULL, NULL, timeout);if (ret 0) {if (FD_ISSET(fd1, readfds)) {// fd1 is ready to read}if (FD_ISSET(fd2, readfds)) {// fd2 is ready to read}
}poll函数
Linux 中的 poll 函数与 select 函数类似,用于等待多个文件描述符就绪,但是 poll 函数返回的是一个事件列表,而不是像 select 函数那样返回就绪的文件描述符数量。poll 函数的原型为:
#include poll.h
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
其中,fds 是一个 pollfd 结构数组,每个结构中包含一个文件描述符和一个事件掩码,timeout 表示等待的时间限制。
例如,下面代码使用 poll 函数等待两个文件描述符(FD)就绪,最多等待 5 秒钟:
int fd1 open(file1, O_RDONLY);
int fd2 open(file2, O_RDONLY);struct pollfd fds[2];
fds[0].fd fd1;
fds[0].events POLLIN;
fds[1].fd fd2;
fds[1].events POLLIN;int timeout 5000; // 5 秒int ret poll(fds, 2, timeout);if (ret 0) {if (fds[0].revents POLLIN) {// fd1 is ready to read}if (fds[1].revents POLLIN) {// fd2 is ready to read}
}如果 poll 函数返回 0,则表示超时,如果返回值大于 0,则表示有文件描述符就绪。与 select 函数不同的是,poll 函数返回的是一个事件列表,而不是具体的文件描述符。
epoll函数
Linux 中的 epoll 函数是 Linux 2.6.8 引入的一个新的 I/O 模型,它提供了高效的 I/O 事件通知机制,能够处理大量文件描述符,并且具有低延迟和高吞吐量的优点。
epoll 函数的原型为:
#include sys/epoll.h
int epoll_create(int size);
int epoll_ctl(int epoll_fd, int op, int fd, int event);
int epoll_wait(int epoll_fd, struct epoll_event *events, int maxevents, int timeout);
其中,epoll_create 函数用于创建一个 epoll 实例,size 参数指定要创建的 epoll 实例可以容纳的最大文件描述符数量。epoll_ctl 函数用于控制 epoll 实例,op 参数指定要进行的操作(例如,添加、修改或删除文件描述符),fd 参数指定要操作的文件描述符,event 参数指定要设置的事件掩码。epoll_wait 函数用于等待 epoll 实例中的文件描述符就绪,events 参数指向一个 epoll_event 结构数组,用于存储就绪的文件描述符和事件信息,maxevents 参数指定最多等待的文件描述符数量,timeout 参数指定等待的时间限制。
例如,下面代码使用 epoll 函数等待两个文件描述符(fd)就绪,最多等待 5 秒钟:
int fd1 open(file1, O_RDONLY);
int fd2 open(file2, O_RDONLY);int epoll_fd epoll_create(10);struct epoll_event event;
event.events EPOLLIN;
event.data.fd fd1;int ret epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd1, event);event.data.fd fd2;
ret epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd2, event);struct epoll_event events[2];
int timeout 5000; // 5 秒int num epoll_wait(epoll_fd, events, 2, timeout);if (num 0) {if (events[0].data.fd fd1 events[0].events EPOLLIN) {// fd1 is ready to read}if (events[1].data.fd fd2 events[1].events EPOLLIN) {// fd2 is ready to read}
}如果 epoll 函数返回 0,则表示超时,如果返回值大于 0,则表示有文件描述符就绪。与 select 和 poll 函数不同的是,epoll 函数能够处理大量的文件描述符,并且具有低延迟和高吞吐量的优点