linux下的epoll

epoll只有一种设计模式

1)收集需要处理的fd

2)判断fd,如果来源于监听端口,进行accept,如果是不是,那就是或者可读,或者可写,然后根据fd的读写类型进行读写处理。


while (TRUE)

{
//等待EPOLL事件的发生,相当于监听,至于相关的端口,需要在初始化EPOLL的时候绑定。
int nfds = epoll_wait (m_epoll_fd, m_events, MAX_EVENTS, EPOLL_TIME_OUT);
if (nfds <= 0)
   continue;
for (int i=0; i<nfds; i++)
{
   try
   {
     //如果新监测到一个用户连接到绑定的端口,建立新的连接。
    if (m_events[i].data.fd == m_listen_fd)。
    {
     OnAcceptEpoll ();
    }
    else if (m_events[i].events & EPOLLIN)//如果是已经连接的用户,并且收到数据,那么进行读入。
    {
     OnReadEpoll (i);
    }else if(m_events[i].events & EPOLLOUT){
    OnWriteEpoll (i);//查看当前的活动连接是否有需要写出的数据。
    }


   }
   catch (int)
   {
    PRINTF ("CATCH捕获错误\n");
    continue;
   }
}

}

猜你喜欢

转载自blog.csdn.net/luwei9233/article/details/66983117