SOCKET CChannelInfo::GetSocket(SOCKET_TYPE enSocketType)
{
    CConnection* pConnection = GetConnection(enSocketType);
    if (NULL == pConnection)
    {
        return INVALID_SOCKET;
    }

    return pConnection->GetSocket();
}
Exemple #2
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;
}
Exemple #3
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;
}
Exemple #4
0
BOOL    CNetManager::WorkThread_SendData()
{
	while(!m_bCloseSend)
	{
		SendDataNode _SendNode;

		if(!m_SendDataList.Pop(_SendNode))
		{
			continue;
		}

		IDataBuffer *pDataBuffer = (IDataBuffer *)_SendNode.pPtr;
		if(pDataBuffer == NULL)
		{
			if(_SendNode.u64ConnID != 1)
			{
				CLog::GetInstancePtr()->AddLog("发送线程:空消息 ,准备退出发送线程!");
			}

			continue;
		}

		SOCKET hSocket = INVALID_SOCKET;
		CConnection *pConnection = NULL;
		if(_SendNode.bIsConnID)
		{
			pConnection = CConnectionMgr::GetInstancePtr()->GetConnectionByConnID(_SendNode.u64ConnID);
			if(pConnection == NULL)
			{
				CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 无效的连接ID!");

				pDataBuffer->Release();

				continue;
			}

			hSocket = pConnection->GetSocket();
		}
		else
		{
			hSocket = _SendNode.u64ConnID;
		}

		if(hSocket == INVALID_SOCKET)
		{
			CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 无效的套接字!");

			pDataBuffer->Release();

			continue;
		}

		INT32 nRet = send(hSocket, pDataBuffer->GetData(),pDataBuffer->GetDataLenth(), 0);
		if(nRet < 0)
		{
			int nErr = CommonSocket::GetSocketLastError();

			CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 原因:%s!", CommonSocket::GetLastErrorStr(nErr).c_str());
		}
		else if(nRet < pDataBuffer->GetDataLenth())
		{
			if(pConnection != NULL)
			{
				pConnection->Close(TRUE);

				CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection);
			}

			CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 缓冲区满了!");
		}

		pDataBuffer->Release();
	}

	return TRUE;
}
Exemple #5
0
BOOL    CNetManager::WorkThread_SendData()
{
	while(!m_bCloseSend)
	{
		SendDataNode _SendNode;
		
		if(!m_SendDataList.Pop(_SendNode))
		{
			continue;
		}
	
		IDataBuffer *pDataBuffer = (IDataBuffer *)_SendNode.pPtr;
		if(pDataBuffer == NULL)
		{
			if(_SendNode.u64ConnID != 0)
			{
				CLog::GetInstancePtr()->AddLog("发送线程:发送失败 ,一个空包!");
			}

			continue;
		}

		SOCKET hSocket = INVALID_SOCKET;
		CConnection *pConnection = NULL;
		if(_SendNode.bIsConnID)
		{
			pConnection = CConnectionMgr::GetInstancePtr()->GetConnectionByConnID(_SendNode.u64ConnID);
			if(pConnection == NULL)
			{
				CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 无效的连接ID!");

				pDataBuffer->Release();

				continue;
			}

			hSocket = pConnection->GetSocket();
		}
		else
		{
			hSocket = (SOCKET)_SendNode.u64ConnID;
		}

		if(hSocket == INVALID_SOCKET)
		{
			CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 无效的套接字!");

			pDataBuffer->Release();

			continue;
		}

		WSABUF  DataBuf;
		DataBuf.len = pDataBuffer->GetDataLenth();
		DataBuf.buf = pDataBuffer->GetData();

		NetIoOperatorData *pOperatorData = (NetIoOperatorData *)pDataBuffer->GetBufferPos(pDataBuffer->GetDataLenth()+1);
		if(pOperatorData == NULL)
		{
			pDataBuffer->Release();
			continue;
		}

		pOperatorData->Clear();
		pOperatorData->dwCmdType   = NET_CMD_SEND;
		pOperatorData->pDataBuffer = pDataBuffer;

		DWORD dwSendBytes;
		int nRet = WSASend(hSocket, &DataBuf, 1, &dwSendBytes, 0, (LPOVERLAPPED)pOperatorData, NULL);
		if(nRet == 0)
		{
			if(dwSendBytes == 0)
			{
				if(pConnection != NULL)
				{
					CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 未能发送出数据,所以主动关闭连接!");

					pConnection->Close(TRUE);

					CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection);

					pDataBuffer->Release();
				}
			}
		}
		else if( nRet == -1 )
		{
			UINT32 errCode = CommonSocket::GetSocketLastError();
			if(errCode != ERROR_IO_PENDING)
			{
				if(pConnection != NULL)
				{
					pConnection->Close(TRUE);

					CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection);
				}

				pDataBuffer->Release();

				CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 连接关闭原因:%s!", CommonSocket::GetLastErrorStr(errCode).c_str());
			}
		}
	}

	return TRUE;
}
Exemple #6
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;
}