Example #1
0
BOOL CNetManager::WorkThread_Listen()
{
	while(TRUE)
	{
		sockaddr_in Con_Addr;
		socklen_t nLen = sizeof(Con_Addr);
		memset(&Con_Addr, 0, sizeof(Con_Addr));
		SOCKET hClientSocket = accept(m_hListenSocket, (sockaddr*)&Con_Addr, &nLen);
		if(hClientSocket == INVALID_SOCKET)
		{
			CLog::GetInstancePtr()->AddLog("accept 错误 原因:%s!", CommonSocket::GetLastErrorStr(CommonSocket::GetSocketLastError()).c_str());

			break;
		}

		CommonSocket::SetSocketUnblock(hClientSocket);

		CConnection *pConnection = AssociateCompletePort(hClientSocket);
		if(pConnection != NULL)
		{
			CLog::GetInstancePtr()->AddLog("成功收到新连接,发送身份信息, 并提交数据请求!");

			pConnection->SetConnectionOK(TRUE);

			SendIdentifyInfo(hClientSocket);

#ifdef WIN32
			if(!pConnection->DoReceive())
			{
				pConnection->Close(FALSE);

				CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection);
			}
#endif
		}
		else
		{
			CLog::GetInstancePtr()->AddLog("accept 错误 原因:%s!", CommonSocket::GetLastErrorStr(CommonSocket::GetSocketLastError()).c_str());
		}
	}

	return TRUE;
}
Example #2
0
BOOL CNetManager::WorkThread_DispathEvent()
{
	struct epoll_event EpollEvent[20];
	int nFd = 0;
	EventNode _EventNode;
	while(TRUE)
	{
		nFd = epoll_wait(m_hCompletePort, EpollEvent, 20, 1000);

		for(int i = 0; i < nFd; ++i)
		{
			CConnection *pConnection = (CConnection*)EpollEvent[i].data.ptr;
			if(pConnection == NULL)
			{
				continue ;
			}

			if((EpollEvent[i].events & EPOLLERR)||(EpollEvent[i].events & EPOLLHUP))
			{
				if(!pConnection->IsConnectionOK())
				{
					CLog::GetInstancePtr()->AddLog("---未连接socket收到这个消息----EPOLLERR------------!");
					continue;
				}
				
				CLog::GetInstancePtr()->AddLog("---己连接socket收到这个消息----EPOLLERR------------!");

				continue;
			}
			
			int nError;
			socklen_t len;

			if(getsockopt(pConnection->GetSocket(), SOL_SOCKET, SO_ERROR, (char*)&nError, &len) < 0)
			{
				CLog::GetInstancePtr()->AddLog("-------getsockopt Error:%d--------成功----!", nError);
				continue;
			}

			if(EpollEvent[i].events & EPOLLIN)
			{
				if(nError ==0)
				{
					_EventNode.dwEvent = EVENT_READ;

					_EventNode.pPtr = EpollEvent[i].data.ptr;

					m_DispatchEventList.Push(_EventNode);

					CLog::GetInstancePtr()->AddLog("-------EPOLLIN--------成功----!");
				}
				else
				{
					CLog::GetInstancePtr()->AddLog("-------EPOLLIN---------失败---!");
				}
			}
			
			if(EpollEvent[i].events & EPOLLOUT)
			{
				if(nError == 0)
				{
					if(!pConnection->IsConnectionOK())
					{
						pConnection->SetConnectionOK(TRUE);

						SendIdentifyInfo(pConnection->GetSocket());

						CLog::GetInstancePtr()->AddLog("-------EPOLLOUT-----成功---发送身份信息----!");
					}

					CLog::GetInstancePtr()->AddLog("-------EPOLLOUT-----成功-------!");
				}
				else
				{
					CLog::GetInstancePtr()->AddLog("-------EPOLLOUT----失败-------!");
				}
			}
		}
	}

	return TRUE;
}
Example #3
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;
}