Ejemplo n.º 1
0
TInt32 CConnectionMgr::StopListening(void *pListeningPointer)
{
    CListener *pListener = m_pListeners;
    CListener *pLast=NULL;
    while(pListener)
    {
        if (pListener == (pListeningPointer))
        {
            if(pLast)
            {
                pLast->AttachToList(pListener->GetNext());
                delete pListener;
                return SUCCESS;
            }
            else
            {
                m_pListeners = pListener->GetNext();
                delete pListener;
                return SUCCESS;
            }
        }
        else
        {
            pLast = pListener;
            pListener = pListener->GetNext();
        }
    }
    return OUT_OF_RANGE;
}
Ejemplo n.º 2
0
TInt32 CConnectionMgr::Run(TUInt32 runCnt)
{
    TInt32 usedCnt = 0;
    CListener *pList = m_pListeners;
    m_nTimeNow = time(NULL);
    while (pList)
    {
        usedCnt += pList->Run(runCnt-usedCnt);
        if (usedCnt >= runCnt)
        {
            return usedCnt;
        }
        pList = pList->GetNext();
    }
    usedCnt += m_connector.Run((runCnt-usedCnt));
    TIOEvent *pEvent = m_netEventQueues.GetNetEvent();
    
    while(pEvent)
    {
        TIOEvent event = *pEvent;
        m_netEventQueues.ConfirmHandleNetEvent(pEvent);
        CConnection *pConnection = m_conncectionPool.GetConectionByIdx(pEvent->m_connectionIdx);
        if (pConnection)
        {
            //先确认处理了消息,这样网络层如果有新事件,也会发事件通知.
            pConnection->OnAppRecved();
            //由应用层调用,如果返回-1,则表示需要释放连接,把链接close ,并且放回connectionPool
            TInt32 ret = pConnection->AppRoutine(m_pBuff,m_buffSize);
            //不能先处理,再确认,因为,可能在处理完了,即上句执行完了,网络层又有了新事件,就会丢失.
            //pConnection->OnAppRecved();
            pConnection->OnAppHandled();

            if (ret < SUCCESS)
            {
                //pConnection->CloseConnection();
                m_conncectionPool.ReleaseItem(pConnection);
            }
        }
        ++ usedCnt;
        if(usedCnt > runCnt)
        {
            break;
        } 
        pEvent = m_netEventQueues.GetNetEvent();
    }
    return usedCnt;
}