我正在使用epoll来监听listen fd事件,在EPOLLIN事件发生后我调用accept来处理, 但总是EAGAIN错误。有人可以给我一些建议吗?谢谢!
[日志] 始终打印以下信息
INFO Jan 01 00:02:08:924 [385] poll_loop:epoll有1个事件
INFO Jan 01 00:02:08:925 [385] poll_loop:event fd 0,event type:1
INFO Jan 01 00:02:08:925 [385] handle_connect:fd 0,
ERROR Jan 01 00:02:08:925 [385] handle_connect:accept返回错误11资源暂时不可用...正在重试,请听fd:0。
[代码]
................
listenfd = listen_sock (port, &addrlen);
socket_nonblocking(listenfd);
g_epollFd = epoll_create(MAX_EVENTS);
register_read(listenfd);
.................
while (!config.quit)
{
int fds = epoll_wait(g_epollFd, g_Events, MAX_EVENTS, -1);
if (fds < 0)
{
log_message(LOG_CRIT, "epoll wait error %d %s, continue", errno, strerror(errno));
continue;
}
log_message(LOG_INFO, "epoll has %d event", fds);
for (i = 0; i < fds; ++i)
{
log_message(LOG_INFO, "event fd %d, event type:%d", g_Events[i].data.fd, g_Events[i].events);
if ((g_Events[i].events & EPOLLERR)
|| (g_Events[i].events & EPOLLHUP)
/*|| (g_Events[i].events & POLLNVAL) compile err*/)
{
log_message(LOG_INFO, "A disconnect occurs, [fd:%d]", g_Events[i].data.fd);
handle_disconnect(g_Events[i].data.fd);
continue;
}
if (g_Events[i].events & EPOLLIN)
{
if (listenfd == g_Events[i].data.fd)
{
handle_connect(listenfd, ptr);
}
else if (g_dnsfd == g_Events[i].data.fd)
{
dns_poll(g_dns);
}
else
{
handle_input(g_Events[i].data.fd);
}
}
...................
void handle_connect(int fd, struct child_s *ptr)
{
int connfd;
struct conn_s *connptr = 0;
char peer_ipaddr[IP_LENGTH];
socklen_t clilen = sizeof(struct sockaddr_in);
struct sockaddr_in cliaddr ;
log_message(LOG_INFO, "fd %d, ");
connfd = accept (fd, (struct sockaddr*)(&cliaddr), &clilen);
if (connfd < 0)
{
log_message (LOG_ERR, "accept returned an error %d %s ... retrying, listen fd:%d", errno, strerror (errno), fd);
return;
}
......................
#define REGISTER_EPOLL_EVENT(_fd, evt, op) \
do\
{\
struct epoll_event epv = {0, {0}}; \
epv.data.fd = _fd; \
epv.events = evt; /*EPOLLLT default*/\
\
if(epoll_ctl(g_epollFd, op, _fd, &epv) < 0) \
{\
/*if ((errno != EEXIST) && (errno != ENOENT))*/\
{\
log_message(LOG_ERR, "epool ctl set failed, fd %d, errno %d: %s", _fd, errno, strerror(errno)); \
return ;\
}\
}\
log_message(LOG_INFO, "epool ctl set sucess, fd %d", _fd);\
}while(0)
void register_read(int fd, int* evt)
{
if (0 == evt)
{
REGISTER_EPOLL_EVENT(fd, EPOLLIN, EPOLL_CTL_ADD);
}
else if (0 == *evt)
{
*evt |= EPOLLIN;
REGISTER_EPOLL_EVENT(fd, (*evt), EPOLL_CTL_ADD);
}
else if (!(*evt & EPOLLIN))
{
*evt |= EPOLLIN;
REGISTER_EPOLL_EVENT(fd, (*evt), EPOLL_CTL_MOD);
}
else
{
log_message("event %d for fd %d is already there", *evt, fd);
}
}
答案 0 :(得分:1)
这一行说明了一切:
INFO Jan 01 00:02:08:925 [385] poll_loop: event fd 0, event type:1
fd 0是STDIN,而不是你的侦听套接字。显然,你正在为epoll集添加“0”而不是你的监听套接字。
您没有显示调用epoll_ctl将listenFd添加到epoll集(g_epollFd)的位置。据推测,这就是上面的register_read()调用的全部内容。我希望它看起来像这样:
int result;
epoll_event readEvent = {};
readEvent.data.fd = listenFd;
readEvent.events = EPOLLIN;
result = epoll_ctl(g_epollFd, EPOLL_CTL_ADD, listenFd, &readEvent);
请注意,listenFd既指定为epoll_ctl的第三个参数,也指定为第四个参数的成员var(readEvent.data.fd)。
答案 1 :(得分:-1)
EAGAIN并非真正的错误。
请参阅: ERROR on accept: Resource temporarily unavailable
另外,如果您有兴趣进一步阅读,我强烈推荐这本书,以便对epoll网络编程有一个很好的概述。