如何处理EPOLLERR和EPOLLHUP?

我读了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发生时,连接应该被closures。 但是在上面的代码中,当EPOLLHUP | EPOLLERR遇到时,事件掩码被设置为EV_READ | EV_WRITE。 所以我的问题是:

  • 什么使EPOLLERR和EPOLLHUP发生?
  • 当EPOLLERR和EPOLLHUP发生时,程序在事件处理函数中应该做什么? 请详细解释其背后的原因。

提前致谢!

什么使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 EV_READ | EV_WRITE在这种情况下,回调函数调用例如recv() ,当对等体执行有序关闭(EPOLLHUP)时可能返回0,如果发生错误则返回-1(EPOLLERR)。 程序可能会清理连接,如果是客户端,可能会重新建立连接。