我在libevent epoll中读过代码,这里是代码:
if (what & (EPOLLHUP|EPOLLERR)) {
ev = EV_READ | EV_WRITE;
} else {
if (what & EPOLLIN)
ev |= EV_READ;
if (what & EPOLLOUT)
ev |= EV_WRITE;
if (what & EPOLLRDHUP)
ev |= EV_CLOSED;
}
根据我的理解,当EPOLLERR或EPOLLHUP发生时,应该关闭连接。但是在上面的代码中,当EPOLLHUP | EPOLLERR遇到时,事件掩码被设置为EV_READ | EV_WRITE。所以我的问题是:
提前致谢!
答案 0 :(得分:4)
是什么让EPOLLERR和EPOLLHUP发生?
man epoll_ctl
EPOLLERR
Error condition happened on the associated file descriptor.
epoll_wait(2) will always wait for this event; it is not
necessary to set it in events.
EPOLLHUP
Hang up happened on the associated file descriptor.
epoll_wait(2) will always wait for this event; it is not
necessary to set it in events.
当EPOLLERR和EPOLLHUP发生时,程序应该做什么 事件句柄功能?
在这种情况下,当libevent传递事件EV_READ | EV_WRITE
时,回调函数会调用e。 G。 recv()
,当对等体执行有序关闭(EPOLLHUP)时可能返回0,如果发生错误则返回-1(EPOLLERR);然后程序可能会清理连接,如果是客户端,可能会重新建立连接。