我正在使用epoll获取有关传入数据的通知。这并不难,因为返回的所有事件都epoll_wait()表明我可以从epoll_event.data.fd(套接字描述符)读取数据。
但现在我想获得两种类型的通知:接收和发送(套接字可用于发送)。但我不能这样做,因为:
epoll_event.events返回epoll_wait()的与我传入的相同epoll_ctl()。所以它包含EPOLLIN和EPOLLOUT在我的情况下。epoll(作为 EPOLLIN 和作为 EPOLLOUT 事件),我会得到一个EEXIST.select()每次收到通知时,如何在不手动调用的情况下解决此问题?
man epoll_wait明确指出“事件成员将包含返回的事件位字段。”。因此,如果你得到EPOLLIN | EPOLLOUT的epoll_event.events,那么你的插座必须准备好读取和写入。
如果您只想在套接字更改 state时收到通知,请EPOLLET用于边缘触发操作。
使用 添加描述符时epoll_ctl,请将events掩码设置为EPOLLIN | EPOLLOUT。
当您通过 then 收到通知时,epoll_wait您将循环返回的通知检查EPOLLIN和EPOLLOUT。
伪代码:
int index, count;
count = epoll_wait(epfd, epoll_event, MAX_EVENTS, -1);
for (index = 0; index < count; ++index) {
if (epoll_event[index].events & EPOLLIN) {
// Ready for read
}
if (epoll_event[index].events & EPOLLOUT) {
// Ready for write
}
}
Run Code Online (Sandbox Code Playgroud)
有些人仅EPOLLOUT在发送缓冲区中有数据时才设置该位。我没有包含任何错误检查。