gpt4 book ai didi

linux - 如何处理EPOLLERR和EPOLLHUP?

转载 作者:IT王子 更新时间:2023-10-29 00:54:54 30 4
gpt4 key购买 nike

我看了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。所以我的问题是:

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

提前致谢!

最佳答案

What makes EPOLLERR and EPOLLHUP happen?

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.

When EPOLLERR and EPOLLHUP happen, what should the program do in the event handle function?

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

关于linux - 如何处理EPOLLERR和EPOLLHUP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24119072/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com