通过epoll处理监听socket

发布于 2024-11-16 12:13:52 字数 1338 浏览 0 评论 0原文

以下所有内容均来自 man epoll 页面:

函数 do_use_fd() 使用新的 就绪文件描述符 直到 EAGAIN 由 read(2) 或 write(2) 返回。

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);
       }
   }

因此,对于读/写操作,我们应该通过循环直到收到EAGAIN来完成;但为什么不是接受的情况如何?

IMO,当有多个客户端套接字等待接受时,上面的代码将错过一些请求,因为它只接受 1 客户端套接字,我们还应该将其包装在一个循环中,直到 EAGAIN收到。

或者也许我缺少什么?

All below is from man epoll page:

The function do_use_fd() uses the new
ready file descriptor
until EAGAIN is returned by either read(2) or write(2).

Code example for ET triggered :

   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?

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.

Or maybe is there something I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿童谣 2024-11-23 12:13:53

看看epollfd中监听器socket是如何添加的:

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

它不是添加在edge-triggered中,而是添加在level-triggered中。因此,在 EAGAIN 之前不需要循环。

Look at how the listener socket is added to the 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文