예제 #1
0
FdEvent* FdEventsKqueuer::addFdEvent(int fd, eventCallback cb)
{
    assert(fd);
    assert(!getChannel(fd));
    FdEvent *fde = FdEvent::newFdEvent(fd);
    fde->events_ = (FDEVENT_IN | FDEVENT_HUP | FDEVENT_ERR);
    fde->callback_ = cb;
    addFdEvent(fde);
    return fde;
}
예제 #2
0
파일: CEvent.cpp 프로젝트: laolinzai/lce
int CEvent::init(uint32_t dwMaxFdNum /* = 10000 */)
{
	if(m_bInit)
	{
		return 0;
	}
		

	m_dwMaxFdNum = dwMaxFdNum;

    m_stEPollState.iEPollFd = epoll_create ( dwMaxFdNum );

    if( m_stEPollState.iEPollFd < 0 )
    {
        snprintf(m_szErrMsg,sizeof(m_szErrMsg),"%s,%d CEvent::init epoll_create error",__FILE__,__LINE__);
        return -1;
    }

    m_stFdEvents =new SFdEvent[dwMaxFdNum];

    for(int i=0;i< (int)dwMaxFdNum;i++)
    {
        m_stFdEvents[i].iEventType=EV_DONE;
        m_stFdEvents[i].pClientRData = NULL;
        m_stFdEvents[i].pClientWData = NULL;
    }

    if( pipe(m_iMsgFd) != 0)
    {
        snprintf(m_szErrMsg,sizeof(m_szErrMsg),"%s,%d CEvent::init create pipe error",__FILE__,__LINE__);
        return -1;
    }

    //set nonblock
    int iOpts;
    iOpts=fcntl(m_iMsgFd[0],F_GETFL);
    if(iOpts<0)
    {
        snprintf(m_szErrMsg,sizeof(m_szErrMsg),"%s,%d CEvent::init fcntl error",__FILE__,__LINE__);
        return -1;
    }
    iOpts = iOpts|O_NONBLOCK;

    if(fcntl(m_iMsgFd[0],F_SETFL,iOpts)<0)
    {
        snprintf(m_szErrMsg,sizeof(m_szErrMsg),"%s,%d CEvent::init fcntl error",__FILE__,__LINE__);
        return -1;
    }

    addFdEvent(m_iMsgFd[0],CEvent::EV_READ,NULL,NULL);//自定义事件通知

	m_bInit = true;
    return 0;
}