Beispiel #1
0
BOOL CIocpServer::Start(LPCTSTR pszBindAddress, USHORT usPort)
{
	if(!CheckParams() || !CheckStarting())
		return FALSE;

	//创建监听套接字开始监听端口
	if(CreateListenSocket(pszBindAddress, usPort))
	{
		if(CreateCompletePort())
		{
			if(CreateWorkerThreads())
			{
				if(StartAcceptThread())
				{
					m_enState = SS_STARTED;
					return TRUE;
				}
			}
		}
	}


	Stop();

	return FALSE;
}
Beispiel #2
0
BOOL CNetManager::Start( IDataHandler *pBufferHandler )
{
	if(pBufferHandler == NULL)
	{
		ASSERT_FAIELD;

		return FALSE;
	}

	m_pBufferHandler = pBufferHandler;

	if(!InitNetwork())
	{
		ASSERT_FAIELD;
		CLog::GetInstancePtr()->AddLog("初始化网络失败!!");
		return FALSE;
	}

	if(!CreateCompletePort())
	{
		ASSERT_FAIELD;
		CLog::GetInstancePtr()->AddLog("创建完成端口或Epoll失败!!");
		return FALSE;
	}

	if(!CreateEventThread(0))
	{
		ASSERT_FAIELD;
		CLog::GetInstancePtr()->AddLog("创建网络事件处理线程失败!!");
		return FALSE;
	}

	if(!CreateDispatchThread())
	{
		ASSERT_FAIELD;
		CLog::GetInstancePtr()->AddLog("创建事件分发线程失败!!");
		return FALSE;
	}

	if(!CreateSendDataThread())
	{
		ASSERT_FAIELD;
		CLog::GetInstancePtr()->AddLog("创建数据发送线程失败!!");
		return FALSE;
	}

	if(!StartListen())
	{
		ASSERT_FAIELD;
		CLog::GetInstancePtr()->AddLog("开启监听失败!!");
		return FALSE;
	}

	return TRUE;
}
Beispiel #3
0
BOOL CTcpAgent::Start(LPCTSTR pszBindAddress, BOOL bAsyncConnect)
{
	if(!CheckParams() || !CheckStarting())
		return FALSE;

	if(ParseBindAddress(pszBindAddress, bAsyncConnect))
		if(CreateCompletePort())
			if(CreateWorkerThreads())
			{
				m_bAsyncConnect	= bAsyncConnect;
				m_enState		= SS_STARTED;

				return TRUE;
			}

	Stop();

	return FALSE;
}
Beispiel #4
0
BOOL CTcpServer::Start(LPCTSTR lpszBindAddress, USHORT usPort)
{
	if(!CheckParams() || !CheckStarting())
		return FALSE;

	PrepareStart();

	if(CreateListenSocket(lpszBindAddress, usPort))
		if(CreateCompletePort())
			if(CreateWorkerThreads())
				if(StartAccept())
				{
					m_enState = SS_STARTED;
					return TRUE;
				}

	EXECUTE_RESTORE_ERROR(Stop());

	return FALSE;
}
Beispiel #5
0
BOOL CTcpServer::Start(LPCTSTR lpszBindAddress, USHORT usPort)
{
	if(!CheckParams() || !CheckStarting())
		return FALSE;

	PrepareStart();

	if(CreateListenSocket(lpszBindAddress, usPort))
		if(CreateCompletePort())
			if(CreateWorkerThreads())
				if(StartAccept())
				{
					m_enState = SS_STARTED;
					return TRUE;
				}

	DWORD dwCode = ::GetLastError();

	Stop();

	::SetLastError(dwCode);

	return FALSE;
}
Beispiel #6
0
BOOL CTcpIocpServer::Create(uint16_t nSvrPort, ITcpServerEvent* pSvrEvent, 
	ENUM_ENCRYPT_TYPE enType)
{
	// 参数检查
	ASSERT(pSvrEvent);
	ASSERT(nSvrPort != 0);
	if(NULL == pSvrEvent || 0 == nSvrPort)
		return FALSE;

	// 重复创建检查
	if(INVALID_SOCKET != m_hListenSocket)
		return FALSE;

	m_pTcpEvent = pSvrEvent;
	SetEncryptType(enType);

	BOOL bResult = FALSE;
	do 
	{
		// 创建完成端口
		m_hIocp = CreateCompletePort();
		if(INVALID_HANDLE_VALUE == m_hIocp)
			break;

		// 创建SOCKET句柄
		m_hListenSocket = CreateSocket(nSvrPort);
		if(INVALID_SOCKET == m_hListenSocket)
			break;

		// 完成端口与SOCKET绑定
		m_oListenContext.m_hSocket = m_hListenSocket;
		if (!CreateIoCompletionPort((HANDLE)m_hListenSocket, m_hIocp, 
			(ULONG_PTR)&m_oListenContext, 0))
		{
			TraceLogError("CTcpIocpServer::Create 与完成端口关联失败 ERROR=%d\n", 
				WSAGetLastError());
			break;
		}

		// 绑定端口
		sockaddr_in addr;
		addr.sin_family = AF_INET;
		addr.sin_addr.s_addr = INADDR_ANY;
		addr.sin_port = htons(nSvrPort);
		if (SOCKET_ERROR == bind(m_hListenSocket, (LPSOCKADDR)&addr, sizeof(addr)))
		{
			TraceLogError("CTcpIocpServer::Create 绑定端口失败 PORT=%d ERROR=%d\n", 
				nSvrPort, WSAGetLastError());
			break;
		}

		// 开始监听
		if(listen(m_hListenSocket, DEFAULT_LISTEN_BACK_LOG) == SOCKET_ERROR)
		{
			TraceLogError("CTcpIocpServer::Create 监听失败 ERROR=%d!\n", 
				WSAGetLastError());
			break;
		}

		// 获得AcceptEx相关函数指针
		m_lpfnAcceptEx = GetAcceptExPtr(m_hListenSocket);
		m_lpfnGetAcceptExSockAddrs = GetAcceptExSockAddrs(m_hListenSocket);
		if(NULL == m_lpfnAcceptEx || NULL == m_lpfnGetAcceptExSockAddrs)
			break;

		// 创建ACCEPT事件
		m_hAcceptEvent = CreateAcceptEvent(m_hListenSocket);
		if(INVALID_HANDLE_VALUE == m_hAcceptEvent)
		{
			break;
		}

		// 投递ACCEPTEX
		for (int i = 0; i < 100 ; i++)
		{
			OVERLAPPEDPLUS* pOverlap = MallocOverlap(IO_ACCEPT);
			if(NULL != pOverlap)
			{
				if(!AddAcceptEx(pOverlap))
				{
					FreeOverlap(pOverlap);
				}
			}
		}

		// 创建检查线程
		if(!m_CheckThread.StartThread(this))
			break;

		bResult = TRUE;
	} while (FALSE);

	if(!bResult)
	{
		Destroy();
	}
	return bResult;
}