コード例 #1
0
ファイル: epoll_impl.cpp プロジェクト: hahaya/zsummer_11x
void CZSummerImpl::RunOnce()
{
	int retCount = epoll_wait(m_epoll, m_events, 1000,   m_timer.GetNextExpireTime());
	if (retCount == -1)
	{
		if (errno != EINTR)
		{
			LCD(" epoll_wait err!  errno=" <<strerror(errno));
			return; //! error
		}
		return;
	}

	//check timer
	{
		m_timer.CheckTimer();
		if (retCount == 0) return;//timeout
	}


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

			MsgVct msgs;
			m_msglock.lock();
			msgs.swap(m_msgs);
			m_msglock.unlock();

			for (auto iter = msgs.begin(); iter != msgs.end(); ++iter)
			{
				_OnPostHandler * p = (_OnPostHandler*)(*iter);
				(*p)();
				delete p;
			}
		}
		else if (pReg->_type == tagRegister::REG_TCP_ACCEPT)
		{
			CTcpAcceptImpl *pKey = (CTcpAcceptImpl *) pReg->_ptr;
			if (eventflag & EPOLLIN)
			{
				pKey->OnEPOLLMessage(true);
			}
			if (eventflag & EPOLLERR || eventflag & EPOLLHUP)
			{
				pKey->OnEPOLLMessage(false);
			}
		}
		else if (pReg->_type == tagRegister::REG_TCP_SOCKET)
		{
			CTcpSocketImpl *pKey = (CTcpSocketImpl *) pReg->_ptr;
			pKey->OnEPOLLMessage(pReg->_type, eventflag);
		}
		else if (pReg->_type == tagRegister::REG_UDP_SOCKET)
		{
			CUdpSocketImpl *pKey = (CUdpSocketImpl *) pReg->_ptr;
			pKey->OnEPOLLMessage(pReg->_type, eventflag);
		}
		else
		{
			LCE("check register event type failed !!  type=" << pReg->_type);
		}
			
	}
}
コード例 #2
0
ファイル: epoll.cpp プロジェクト: steven-liao/zsummer
void CIOServer::Run()
{
	epoll_event events[10000];
	int retCount = 0;
	bool bRuning = true;
	unsigned int timerTime = GetTimeMillisecond();
	while (true)
	{
		retCount = epoll_wait(m_epoll, events, 10000,  1000);
		if (retCount == -1)
		{
			if (errno != EINTR)
			{
				LCD("ERR: epoll_wait err!  errno=" <<strerror(errno));
				break;
			}
			continue;
		}
		//check timer
		{
			unsigned int curTime = GetTimeMillisecond();
			if (curTime - timerTime > 1000)
			{
				timerTime = curTime;
				m_cb->OnTimer();
			}
			if (retCount == 0) continue;//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_THREAD)
			{
				char buf[1000];
				while (recv(pReg->_socket, buf, 1000, 0) > 0){}

				MsgVct msgs;
				m_msglock.Lock();
				msgs = m_msgs;
				m_msgs.clear();
				m_msglock.UnLock();

				for (MsgVct::iterator iter = msgs.begin(); iter != msgs.end(); ++iter)
				{
					if (iter->first == PCK_EPOLL_EXIT)
					{
						LCD("INFO: epoll recv exit event");
						bRuning = false;
						continue;
					}
					else if (iter->first == PCK_ACCEPT_CLOSE)
					{
						CTcpAccept * p = (CTcpAccept *) iter->second;
						p->OnPostClose();
					}
					else if (iter->first == PCK_SOCKET_CLOSE)
					{
						CTcpSocket *p = (CTcpSocket*)iter->second;
						p->OnPostClose();
					}
					else if (iter->first == PCK_USER_DATA)
					{
						m_cb->OnMsg(iter->second);
					}
				}
			}
			else if (pReg->_type == tagRegister::REG_ACCEPT)
			{
				CTcpAccept *pKey = (CTcpAccept *) pReg->_ptr;
				if (eventflag & EPOLLIN)
				{
					pKey->OnEPOLLMessage(true);
				}
				if (eventflag & EPOLLERR || eventflag & EPOLLHUP)
				{
					pKey->OnEPOLLMessage(false);
				}
			}
			else
			{
				if    (pReg->_type != tagRegister::REG_RECV 
					&& pReg->_type != tagRegister::REG_SEND
					&& pReg->_type != tagRegister::REG_CONNECT)
				{
					LCE("check register event type failed !!  type=" << pReg->_type);
					continue;
				}

				CTcpSocket *pKey = (CTcpSocket *) pReg->_ptr;
				pKey->OnEPOLLMessage(pReg->_type, eventflag);
			}
		}

		if (!bRuning)
		{
			break;
		}
	}

	LCD("INFO: epoll loop exit");
}