epoll:失去一些EPOLLOUT事件?

时间:2012-08-02 23:31:31

标签: c++ c asyncsocket epoll epollet

这就是我的服务器的样子:

-WorkerThread(S):

  • 调用epoll_wait,接受连接,设置fd nonblocking(EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP)
  • 在EPOLLIN事件上调用recv直到EAGAIN并将所有数据推送到全局RecvBuffer(pthread_mutex已同步)
  • on EPOLLOUT事件:访问全局SendBuffer,如果要为当前就绪fd发送数据,请执行此操作(在循环中直到EAGAIN或直到发送所有数据;发送整个数据包时,从SendBuffer弹出)

-IOThread(S)

  • 从全局RecvBuffer获取数据,处理它们
  • 首先尝试立即拨打电话发送回复。如果不是所有数据都被发送,则将其余数据推送到全局SendBuffer以从WorkerThread发送)

问题是,该服务器不会发送所有排队的数据(它们保留在SendBuffer中),并且“未发送”数据量会随着客户端数量的增加而增加。 为了测试我只使用1个workerthread和1个iothread,但如果我使用更多,它似乎没有任何区别。 使用pthread_mutex保护访问全局缓冲区。 此外,我的响应数据大小为130k字节(至少需要3次发送调用才能发送此数据量)。另一方面是使用阻塞套接字的Windows客户端。

非常感谢! MJ

编辑:

是的,默认情况下我正在等待EPOLLOUT事件,即使我没有任何东西可以发送。为了实现简单性和手册页指南,我这样做了。此外,我的未知就是这样:

即使我“错过”EPOLLOUT事件当时我不想发送任何东西也没问题,因为当我想发送数据时,我会调用send,直到EAGAIN和EPOLLOUT将来会被触发(并且它是最多的)时间)

现在我修改了代码以在IN / OUT事件之间切换:

接受:

event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
epoll_ctl (pNetServer->m_EventFD, EPOLL_CTL_ADD, infd, &event);

发送所有数据后:

event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
epoll_ctl (pNetServer->m_EventFD, EPOLL_CTL_MOD, events[i].data.fd, &event);

当我通过在IOThread中调用send来到达EAGAIN时:

event.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
epoll_ctl (pNetServer->m_EventFD, EPOLL_CTL_MOD, events[i].data.fd, &event);

..我得到了同样的行为。此外,我尝试删除EPOLLET标志,没有任何改变

一个问题:带有EPOLL_CTL_MOD标志的epoll_ctl是否会替换事件成员,或者只是将其与给定参数进行OR运算?

EDIT3:更新了IOThread功能以连续发送,直到所有数据都已发送,或者直到EAGAIN。 即使我发送了所有数据,我也尝试发送,但大部分时间我都在非套接字上获得errno 88套接字操作

EDIT4:我修复了我的'发送代码'中的一些错误,所以我现在没有收到任何排队的数据..但是,我没有收到尽可能多的数据:))最多的“错过”(未收到) )当发送完成后,当客户端调用recv时,我得到的数据随着客户端的数量而增长。当我在客户端发送和recv呼叫之间放置2秒延迟(阻塞呼叫)时,我在服务器上丢失了很少的数据,这取决于我运行的客户端数量(客户端测试代码包括简单的for循环,1次发送和1次recv调用) 再次尝试使用和不使用ET模式。下面是更新的WorkerThread函数,它负责接收数据。 @ Admins / Mods也许我现在应该开新话题,因为问题有点不同?

void CNetServer::WorkerThread(void* param)
{
    CNetServer* pNetServer =(CNetServer*)param;
    struct epoll_event event;
    struct epoll_event *events;
    int s = 0;

//  events = (epoll_event*)calloc (MAXEVENTS, sizeof event);

    while (1)
    {
        int n, i;

//      printf ("BLOCKING NOW! epoll_wait thread %d\n",pthread_self());
        n = pNetServer->m_epollCtrl.Wait(-1);
//      printf ("epoll_wait thread %d\n",pthread_self());
        pthread_mutex_lock(&g_mtx_WorkerThd);
        for (i = 0; i < n; i++)
        {
            if ((pNetServer->m_epollCtrl.Event(i)->events & EPOLLERR))
            {
                // An error has occured on this fd, or the socket is not ready for reading (why were we notified then?)

            //  g_SendBufferArray.RemoveAll( 0 );

                char szFileName[30] = {0};
                sprintf( (char*)szFileName,"fd_%d.txt",pNetServer->m_epollCtrl.Event(i)->data.fd );
                remove(szFileName);

            /*  printf( "\n\n\n");
                printf( "\tDATA LEFT COUNT:%d\n",g_SendBufferArray.size());
                for (int k=0;k<g_SendBufferArray.size();k++)
                    printf( "\tSD: %d DATA LEFT:%d\n",g_SendBufferArray[i]->sd,g_SendBufferArray[i]->nBytesSent );
*/

            //  fprintf (stderr, "epoll error\n");
            //  fflush(stdout);
                close (pNetServer->m_epollCtrl.Event(i)->data.fd);
                continue;
            }
            else if (pNetServer->m_ListenSocket == pNetServer->m_epollCtrl.Event(i)->data.fd)
            {
                // We have a notification on the listening socket, which   means one or more incoming connections. 
                while (1)
                {
                    struct sockaddr in_addr;
                    socklen_t in_len;
                    int infd;
                    char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];

                    in_len = sizeof in_addr;
                    infd = accept (pNetServer->m_ListenSocket, &in_addr, &in_len);
                    if (infd == -1)
                    {
                        if ((errno == EAGAIN) ||
                            (errno == EWOULDBLOCK))
                        {
                            // We have processed all incoming connections.
                            break;
                        }
                        else
                        {
                            perror ("accept");
                            break;
                        }
                    }

                    s = getnameinfo (&in_addr, in_len,
                        hbuf, sizeof hbuf,
                        sbuf, sizeof sbuf,
                        NI_NUMERICHOST | NI_NUMERICSERV);
                    if (s == 0)
                    {
                        printf("Accepted connection on descriptor %d "
                            "(host=%s, port=%s) thread %d\n", infd, hbuf, sbuf,pthread_self());
                    }

                    // Make the incoming socket non-blocking and add it to the list of fds to monitor.
                    CEpollCtrl::SetNonBlock(infd,true);
                    if ( !pNetServer->m_epollCtrl.Add( infd, EPOLLIN, NULL ))
                    {
                        perror ("epoll_ctl");
                        abort ();
                    }

                }
                continue;
            }
            if( (pNetServer->m_epollCtrl.Event(i)->events & EPOLLOUT) )
            {

                pNetServer->DoSend( pNetServer->m_epollCtrl.Event(i)->data.fd );
            }
            if( pNetServer->m_epollCtrl.Event(i)->events & EPOLLIN )
            {
                printf("EPOLLIN TRIGGERED FOR SD: %d\n",pNetServer->m_epollCtrl.Event(i)->data.fd);
                // We have data on the fd waiting to be read. 
                int done = 0;
                ssize_t count = 0;
                char buf[512];
                while (1)
                {
                    count = read (pNetServer->m_epollCtrl.Event(i)->data.fd, buf, sizeof buf);
                    printf("recv sd %d size %d thread %d\n",pNetServer->m_epollCtrl.Event(i)->data.fd,count,pthread_self());
                    if (count == -1)
                    {
                        // If errno == EAGAIN, that means we have read all data. So go back to the main loop.
                        if ( errno != EAGAIN )
                        {
                            perror ("read");
                            done = 1;
                        }
                        break;
                    }
                    else if (count == 0)
                    {
                        //connection is closed by peer.. do a cleanup and close
                        done = 1;
                        break;
                    }
                    else if (count > 0)
                    {
                        static int nDataCounter = 0;
                        nDataCounter+=count;
                        printf("RECVDDDDD %d\n",nDataCounter);
                        CNetServer::s_pRecvContainer->OnData( pNetServer->m_epollCtrl.Event(i)->data.fd, buf, count );
                    }
                }

                if (done)
                {
                    printf ("Closed connection on descriptor %d\n",pNetServer->m_epollCtrl.Event(i)->data.fd);
                    // Closing the descriptor will make epoll remove it from the set of descriptors which are monitored. 
                    close (pNetServer->m_epollCtrl.Event(i)->data.fd);
                }
            }

        }
//      

        pNetServer->IOThread( (void*)pNetServer );

        pthread_mutex_unlock(&g_mtx_WorkerThd);
    }

}

void CNetServer::IOThread(void* param)
{

    BYTEARRAY* pbPacket = new BYTEARRAY;
    int fd;
    struct epoll_event event;
    CNetServer* pNetServer =(CNetServer*)param;

    printf("IOThread startin' !\n");

    for (;;)
    {
        bool bGotIt = CNetServer::s_pRecvContainer->GetPacket( pbPacket, &fd );

        if( bGotIt )
        {

            //process packet here
            printf("Got 'em packet yo !\n");
            BYTE* re = new BYTE[128000];
            memset((void*)re,0xCC,128000);
            buffer_t* responsebuff = new buffer_t( fd, re, 128000 ) ;

            pthread_mutex_lock(&g_mtx_WorkerThd);

            while( 1 )
            {
                    int s;
                    int nSent = send( responsebuff->sd, ( responsebuff->pbBuffer + responsebuff->nBytesSent ),responsebuff->nSize - responsebuff->nBytesSent,0 );
                    printf ("IOT: Trying to send nSent: %d buffsize: %d \n",nSent,responsebuff->nSize - responsebuff->nBytesSent);

                    if (nSent == -1)
                    {

                        if (errno == EAGAIN || errno == EWOULDBLOCK )
                        {
                                g_vSendBufferArray.push_back( *responsebuff );
                                printf ("IOT: now waiting for EPOLLOUT\n");
                                event.data.fd = fd;
                                event.events = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP;
                                s = epoll_ctl (pNetServer->m_EventFD, EPOLL_CTL_MOD, fd, &event);
                                break;
                                if (s == -1)
                                {
                                    perror ("epoll_ctl");
                                    abort ();
                                }

                        }
                        else
                        {
                            printf( "%d\n",errno );
                            perror ("send");
                            break;
                        }
                        printf ("IOT: WOOOOT\n");
                        break;
                    }
                    else if (nSent == responsebuff->nSize - responsebuff->nBytesSent)
                    {
                        printf ("IOT:all is sent! wOOhOO\n");
                        responsebuff->sd = 0;
                        responsebuff->nBytesSent += nSent;
                        delete responsebuff;
                        break;
                    }
                    else if (nSent < responsebuff->nSize - responsebuff->nBytesSent)
                    {
                        printf ("IOT: partial send!\n");
                        responsebuff->nBytesSent += nSent;

                    }

            }
            delete [] re;

            pthread_mutex_unlock(&g_mtx_WorkerThd);

        }
    }

}

1 个答案:

答案 0 :(得分:2)

  1. 停止使用EPOLLET。这几乎是不可能的。

  2. 如果您无需发送任何内容,请不要询问EPOLLOUT事件。

  3. 如果要在连接上发送数据,请遵循以下逻辑:

    A)如果您的发送队列中已存在该连接的数据,则只需添加新数据。你完成了。

    )B)尝试立即发送数据。如果你全部发送,你就完成了。

    )C)将剩余数据保存在此连接的发送队列中。现在请求EPOLLOUT进行此连接。