简体   繁体   English

通过epoll处理侦听套接字

[英]Dealing with listening socket by epoll

All below is from man epoll page: 以下全部来自man epoll页面:

The function do_use_fd() uses the new ready file descriptor until EAGAIN is returned by either read(2) or write(2). 函数do_use_fd()使用新的就绪文件描述符,直到read(2)或write(2)返回EAGAIN。

Code example for ET triggered : ET触发的代码示例:

   for(;;) {
       nfds = epoll_wait(kdpfd, events, maxevents, -1);

       for(n = 0; n < nfds; ++n) {
           if(events[n].data.fd == listener) {
               client = accept(listener, (struct sockaddr *) &local,
                               &addrlen);
               if(client < 0){
                   perror("accept");
                   continue;
               }
               setnonblocking(client);
               ev.events = EPOLLIN | EPOLLET;
               ev.events = EPOLLIN | EPOLLET;
               ev.data.fd = client;
               if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, client, &ev) < 0) {
                   fprintf(stderr, "epoll set insertion error: fd=%d\n",
                           client);
                   return -1;
               }
           }
           else
               do_use_fd(events[n].data.fd);
       }
   }

So for read/write operation,we should do it by looping until a EAGAIN is received;but why it's not the case for accept ? 因此,对于read/write操作,我们应该通过循环来实现它,直到收到EAGAIN ;但为什么不accept的情况呢?

IMO the above code will miss some requests when there're multiple client sockets waiting to be accepted, as it accepts only 1 client socket, we should also wrap it in a loop until EAGAIN received. IMO上面的代码会在有多个客户端套接字等待接受时错过一些请求,因为它只接受1个客户端套接字,我们也应该将它包装在一个循环中,直到收到EAGAIN

Or maybe is there something I'm missing? 或者也许是我缺少的东西?

Look at how the listener socket is added to the epollfd : 看看如何将监听器套接字添加到epollfd

ev.events = EPOLLIN;       // this is the crucial bit
ev.data.fd = listen_sock;

It's not added in edge-triggered, it's added in level-triggered. 它不是在边缘触发中添加的,而是在水平触发中添加的。 So no need for a loop until EAGAIN on that one. 所以在EAGAIN之前不需要循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM