最近在做一个视频中转服务器,使用到了epoll ET模型,在网上找了一大把资料,以为稳妥妥的,到实际应用时,才发现很多小细节得注意。
首先介绍下ET模型下几个常用事件:
1、EPOLLET,指定ET模型,EPOLL默认是LT模型;
2、EPOLLIN, 有新数据;
3、EPOLLOUT, 由不可写变为可写;
4、EPOLLRDHUP,对方主动断开连接。
man了下epoll,看到:When used as an edge-triggered interface, for performance reasons, it is possible to add the file descriptor inside the epoll interface (EPOLL_CTL_ADD) once by specifying (EPOLLIN|EPOLLOUT). This allows you to avoid continuously switching between EPOLLIN and EPOLLOUT calling epoll_ctl(2) with EPOLL_CTL_MOD. 果断注册EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP 这几个事件,然后,阿西巴每次EPOLLIN事件都会带着有EPOLLOUT,百思不得其解,google一下,发现好多老哥都有类似的问题,无奈之下,采用LT里处理OUT事件的方法,当写到EAGAIN才注册EPOLLOUT,用完再MOD掉它。