예제 #1
0
void EventLoop::runOnce(bool isImmediately)
{
    int retCount = epoll_wait(_epoll, _events, MAX_EPOLL_WAIT,  isImmediately ? 0 : _timer.getNextExpireTime());
    if (retCount == -1)
    {
        if (errno != EINTR)
        {
            LCW("EventLoop::runOnce[this0x" << this << "]  epoll_wait err!  errno=" << strerror(errno) << logSection());
            return; //! error
        }
        return;
    }

    //check timer
    {
        _timer.checkTimer();
        if (retCount == 0) return;//timeout
    }


    for (int i=0; i<retCount; i++)
    {
        EventData * pEvent = (EventData *)_events[i].data.ptr;
        //tagHandle  type
        if (pEvent->_type == EventData::REG_ZSUMMER)
        {
            {
                char buf[200];
                while (recv(pEvent->_fd, buf, 200, 0) > 0);
            }

            MessageStack msgs;
            _stackMessagesLock.lock();
            msgs.swap(_stackMessages);
            _stackMessagesLock.unlock();

            for (auto pfunc : msgs)
            {
                _OnPostHandler * p = (_OnPostHandler*)pfunc;
                try
                {
                    (*p)();
                }
                catch (std::runtime_error e)
                {
                    LCW("OnPostHandler have runtime_error exception. err=" << e.what());
                }
                catch (...)
                {
                    LCW("OnPostHandler have unknown exception.");
                }
                delete p;
            }
        }
        else if (pEvent->_type == EventData::REG_TCP_ACCEPT)
        {
            if (pEvent->_tcpacceptPtr)
            {
                pEvent->_tcpacceptPtr->onEPOLLMessage(_events[i].events);
            }
        }
        else if (pEvent->_type == EventData::REG_TCP_SOCKET)
        {
            if (pEvent->_tcpSocketPtr)
            {
                pEvent->_tcpSocketPtr->onEPOLLMessage(_events[i].events);
            }
        }
        else if (pEvent->_type == EventData::REG_UDP_SOCKET)
        {
            if (pEvent->_udpsocketPtr)
            {
                pEvent->_udpsocketPtr->onEPOLLMessage(_events[i].events);
            }
        }
        else
        {
            LCE("EventLoop::runOnce[this0x" << this << "] check register event type failed !!  type=" << pEvent->_type << logSection());
        }
            
    }
}
예제 #2
0
void EventLoop::runOnce(bool isImmediately)
{
	int retCount = epoll_wait(_epoll, _events, 1000,  isImmediately ? 0 : _timer.getNextExpireTime());
	if (retCount == -1)
	{
		if (errno != EINTR)
		{
			LCW("EventLoop::runOnce[this0x" << this << "]  epoll_wait err!  errno=" << strerror(errno) << logSection());
			return; //! error
		}
		return;
	}

	//check timer
	{
		_timer.checkTimer();
		if (retCount == 0) return;//timeout
	}


	for (int i=0; i<retCount; i++)
	{
		int eventflag = _events[i].events;
		tagRegister * pReg = (tagRegister *)_events[i].data.ptr;
		//tagHandle  type
		if (pReg->_type == tagRegister::REG_ZSUMMER)
		{
			char buf[1000];
			while (recv(pReg->_fd, buf, 1000, 0) > 0);

			MessageStack msgs;
			_stackMessagesLock.lock();
			msgs.swap(_stackMessages);
			_stackMessagesLock.unlock();

			for (auto pfunc : msgs)
			{
				_OnPostHandler * p = (_OnPostHandler*)pfunc;
				try
				{
					(*p)();
				}
				catch (std::runtime_error e)
				{
					LCW("OnPostHandler have runtime_error exception. err=" << e.what());
				}
				catch (...)
				{
					LCW("OnPostHandler have unknown exception.");
				}
				delete p;
			}
		}
		else if (pReg->_type == tagRegister::REG_TCP_ACCEPT)
		{
			if (eventflag & EPOLLIN)
			{
				if (pReg->_tcpacceptPtr)
				{
					pReg->_tcpacceptPtr->onEPOLLMessage(true);
				}
			}
			else if (eventflag & EPOLLERR || eventflag & EPOLLHUP)
			{
				if (pReg->_tcpacceptPtr)
				{
					pReg->_tcpacceptPtr->onEPOLLMessage(false);
				}
			}
		}
		else if (pReg->_type == tagRegister::REG_TCP_SOCKET)
		{
			if (eventflag & EPOLLERR || eventflag & EPOLLHUP)
			{
				if (pReg->_tcpSocketConnectPtr)
				{
					pReg->_tcpSocketConnectPtr->onEPOLLMessage(EPOLLOUT, true);
				}
				else if (pReg->_tcpSocketRecvPtr)
				{
					pReg->_tcpSocketRecvPtr->onEPOLLMessage(EPOLLIN, true);
				}
				else if (pReg->_tcpSocketSendPtr)
				{
					pReg->_tcpSocketSendPtr->onEPOLLMessage(EPOLLOUT, true);
				}
			}
			else if (eventflag & EPOLLIN)
			{
				if (pReg->_tcpSocketRecvPtr)
				{
					pReg->_tcpSocketRecvPtr->onEPOLLMessage(EPOLLIN, false);
				}
			}
			else if (eventflag & EPOLLOUT)
			{
				if (pReg->_tcpSocketConnectPtr)
				{
					pReg->_tcpSocketConnectPtr->onEPOLLMessage(EPOLLOUT, false);
				}
				else if (pReg->_tcpSocketSendPtr)
				{
					pReg->_tcpSocketSendPtr->onEPOLLMessage(EPOLLOUT, false);
				}
			}
		}
		else if (pReg->_type == tagRegister::REG_UDP_SOCKET)
		{
			if (pReg->_udpsocketPtr)
			{
				pReg->_udpsocketPtr->onEPOLLMessage(pReg->_type, eventflag);
			}
		}
		else
		{
			LCE("EventLoop::runOnce[this0x" << this << "] check register event type failed !!  type=" << pReg->_type << logSection());
		}
			
	}
}