如何处理EPOLLERR和EPOLLHUP?

Cha*_*429 6 linux network-programming

我阅读了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;
        }
Run Code Online (Sandbox Code Playgroud)

据我了解,当发生EPOLLERR或EPOLLHUP时,应关闭连接。但是在上面的代码中,当遇到EPOLLHUP | EPOLLERR时,事件掩码设置为EV_READ | EV_WRITE。所以我的问题是:

  • 是什么使EPOLLERR和EPOLLHUP发生?
  • 当发生EPOLLERR和EPOLLHUP时,程序应在事件句柄功能中做什么?并请详细解释其背后的原因。

提前致谢!

Arm*_*ali 5

是什么使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.
Run Code Online (Sandbox Code Playgroud)

当发生EPOLLERR和EPOLLHUP时,程序应在事件句柄功能中做什么?

EV_READ | EV_WRITE在这种情况下,当libevent传递事件时,回调函数将调用例如recv(),当对等方执行有序关闭(EPOLLHUP)时,回调函数可能会返回0,如果发生错误则返回-1(EPOLLERR);然后程序可能会清理连接,如果是客户端,则可能重新建立连接。