예제 #1
0
BOOL CNetManager::WorkThread_ProcessEvent()
{
	EventNode _EventNode;
	while(TRUE)
	{
		if(!m_DispatchEventList.Pop(_EventNode))
		{
			continue;
		}

		CConnection *pConnection = (CConnection *)_EventNode.pPtr;
		if(pConnection == NULL)
		{
			if(_EventNode.dwEvent != 1)
			{
				CLog::GetInstancePtr()->AddLog("错误:取出一个空事件!");
			}

			continue;
		}

		if(_EventNode.dwEvent == EVENT_READ)
		{
			if(!pConnection->HandleRecvEvent(0))
			{
				//基本表明连接己断开,可以关闭连接了。
				pConnection->Close(TRUE);
				CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection);
			}
			else
			{
				struct epoll_event EpollEvent;
				EpollEvent.data.ptr= pConnection;
				EpollEvent.events  = EPOLLIN|EPOLLET;

				epoll_ctl(m_hCompletePort, EPOLL_CTL_MOD, pConnection->GetSocket(),  &EpollEvent);
			}
		}
		else if(_EventNode.dwEvent == EVENT_WRITE)
		{

		}
	}

	return TRUE;
}
예제 #2
0
BOOL CNetManager::WorkThread_ProcessEvent()
{
	if(m_hCompletePort == INVALID_HANDLE_VALUE)
	{
		CLog::GetInstancePtr()->AddLog("创建事件网络事件处理线程失败, 完成端口还未创建!");
		ASSERT_FAIELD;
		return FALSE;
	}

	DWORD dwNumOfByte = 0;
	DWORD dwCompleteKey = 0;
	LPOVERLAPPED lpOverlapped = NULL;
	DWORD dwWaitTime = 1000;
	BOOL  bRetValue = FALSE;

	while(!m_bCloseEvent)
	{
		bRetValue = GetQueuedCompletionStatus(m_hCompletePort, &dwNumOfByte, &dwCompleteKey, &lpOverlapped, dwWaitTime);
		if(!bRetValue)
		{
			if(lpOverlapped == NULL)
			{ 
				if(ERROR_ABANDONED_WAIT_0 == CommonSocket::GetSocketLastError())
				{
					CLog::GetInstancePtr()->AddLog("完成端口被外部关闭!");
					return FALSE;
				}

				if(CommonSocket::GetSocketLastError() == WAIT_TIMEOUT)
				{
					//CLog::GetInstancePtr()->AddLog("完成端口等待超时出错,严重!");
					//return FALSE;
					continue;
				}
			}
		}

		NetIoOperatorData *pIoPeratorData = (NetIoOperatorData*)lpOverlapped;
		switch( pIoPeratorData->dwCmdType )
		{
		case NET_CMD_RECV:
			{
				CConnection *pConnection = (CConnection *)dwCompleteKey;
				if(pConnection != NULL)
				{
					if(dwNumOfByte == 0)
					{
						//说明对方己经关闭
						CLog::GetInstancePtr()->AddLog("完成端口收到数据为0, 对方己经关闭连接!");
						pConnection->Close(TRUE);
						CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection);
					}
					else
					{
						if(!pConnection->HandleRecvEvent(dwNumOfByte))
						{
							//收数据失败,基本就是连接己断开
							pConnection->Close(TRUE);
							CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection);
						}
					}
				}
			}
			break;
		case NET_CMD_SEND:
			{
				pIoPeratorData->pDataBuffer->Release();
			}
			break;
		case NET_CMD_CONNECT:
			{
				CConnection *pConnection = (CConnection *)dwCompleteKey;
				if(pConnection != NULL)
				{
					if(bRetValue)
					{
						CLog::GetInstancePtr()->AddLog("连接其它服务器成功!");

						pConnection->SetConnectionOK(TRUE);

						if(SendIdentifyInfo(pConnection->GetSocket()))
						{
							if(!pConnection->DoReceive())
							{
								pConnection->Close(FALSE);

								CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection);
							}
						}
					}
					else
					{
						CLog::GetInstancePtr()->AddLog("连接其它服务器失败!");

						pConnection->Close(FALSE);

						pConnection->SetConnectionOK(FALSE);

						CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection);
					}
				}
			}
			break;
		default:
			{

			}
			break;
		}
	}

	return TRUE;
}