当前位置: 首页>>代码示例>>C++>>正文


C++ CBaseSocket::GetSocket方法代码示例

本文整理汇总了C++中CBaseSocket::GetSocket方法的典型用法代码示例。如果您正苦于以下问题:C++ CBaseSocket::GetSocket方法的具体用法?C++ CBaseSocket::GetSocket怎么用?C++ CBaseSocket::GetSocket使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CBaseSocket的用法示例。


在下文中一共展示了CBaseSocket::GetSocket方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: AddClientChannel


//.........这里部分代码省略.........
	}
	else
	{
		pBaseSocket = new CLinuxUdpSocket();

	}
#endif

	if ( NULL == pBaseSocket )
	{
		// 没申请到内存
		return ERROR_NET_UNKNOWN;
	}

	pBaseSocket->SetBlockMode( m_clsThreadDealNetEvent.GetBlockMode() );
	m_clsClientChannel.SetLocalIPPort(pszBindIP, localport);
	m_clsClientChannel.SetReMoteIPPort(pszhost,unDesPort);


	//创建通道
	if (m_clsClientChannel.CreateChannel(pBaseSocket) != ERROR_BASE_SUCCESS)
	{
		//创建 通道失败
		pclsSocketChnl->CloseChannel();
		delete pclsSocketChnl;
		pclsSocketChnl = NULL;

		delete pBaseSocket;
		pBaseSocket = NULL;	

		char szRemoteIP[16]={0};
		UINT16 uiRemotePort = 0;
		char szLocalIP[16]={0};
		UINT16 uiLocalPort = 0;
		m_clsClientChannel.GetLocalIPPort(szLocalIP,uiLocalPort);
		m_clsClientChannel.GetReMoteIPPort(szRemoteIP,uiRemotePort);
		CTRLPRINTF( g_clsLogPtr,"创建通道失败传入通道参数,pszhost:%s, unDesPort:%u, pszBindIP:%s, localport:%u \n",pszhost, unDesPort,pszBindIP,localport );
		CTRLPRINTF( g_clsLogPtr,"创建通道失败客户端通道参数,szRemoteIP:%s, uiRemotePort:%u, szLocalIP:%s, uiLocalPort:%u \n",szRemoteIP, uiRemotePort,szLocalIP,uiLocalPort );
		return ERROR_NET_CREATE_CHANNEL_FAIL;
	}

	sockaddr_in struLocalAddr;
#if _WIN32
	INT32   iLocalAddrLen = sizeof(sockaddr_in);
#endif
#if _LINUX
	socklen_t   iLocalAddrLen = sizeof(sockaddr_in);
#endif
	getsockname( pBaseSocket->GetSocket(), (sockaddr*)&struLocalAddr, &iLocalAddrLen );
	pclsSocketChnl->SetLocalIPPort(inet_ntoa(struLocalAddr.sin_addr), (UINT16) ntohs(struLocalAddr.sin_port));

	//保存CBaseSocket
	pclsSocketChnl->SetCbaseSocketPoint(pBaseSocket);

	//将socketchannel存入vector容器
	m_clsThreadDealNetEvent.SaveSocketChannel(pclsSocketChnl);

	pclsSocketChnl->SetLogInstancePtr(m_clsThreadDealNetEvent.GetLogInstancePtr());

	CTRLPRINTF(m_clsThreadDealNetEvent.GetLogInstancePtr(), "通道%p创建成功, new socket %d, szRemoteIP:%s, \
		uiRemotePort:%u, szLocalIP:%s, uiLocalPort:%u\n ",pclsSocketChnl,pBaseSocket->GetSocket(),pszhost,unDesPort,
		inet_ntoa(struLocalAddr.sin_addr), (UINT16) ntohs(struLocalAddr.sin_port));

#if OPERATING_SYSTEM

	// select 什么都不需要做

#elif _WIN32

	//加入完成端口
	HANDLE hHandle = m_clsThreadDealNetEvent.AddToIoCompletionPort((HANDLE)pBaseSocket->GetSocket(), (DWORD)pclsSocketChnl->GetIOCPHandleData());
	if (hHandle == NULL)
	{
		pclsSocketChnl->CloseChannel();
		delete pclsSocketChnl;
		pclsSocketChnl = NULL;

		delete pBaseSocket;
		pBaseSocket = NULL;	
		CTRLPRINTF(m_clsThreadDealNetEvent.GetLogInstancePtr(), "通道%p加入完成端口失败, socket %d\n ",pclsSocketChnl,pBaseSocket->GetSocket());
		return ERROR_NET_CREATE_CHANNEL_FAIL;
	}
	//投掷第一次接收数据请求
	pclsSocketChnl->RecvData();

#else
	//linux

	//加入epoll
	m_clsThreadDealNetEvent.EpollCtrl( EPOLL_CTL_ADD,pBaseSocket->GetSocket(),pclsSocketChnl->GetEpollEvent());

	pclsSocketChnl->AddRefCount();

#endif
	//转换
	*pSocketChnl = (ISocketChannel*)pclsSocketChnl;


	return ERROR_BASE_SUCCESS;
}
开发者ID:chcoffice,项目名称:Bull,代码行数:101,代码来源:NetService.cpp

示例2: AddServerChannel


//.........这里部分代码省略.........

		delete pBaseSocket;
		pBaseSocket = NULL;

		return ERROR_NET_CREATE_CHANNEL_FAIL;
	}


	//保存CBaseSocket
	pclsSocketChnl->SetCbaseSocketPoint(pBaseSocket);

	//将socketchannel存入vector容器
    m_clsThreadDealNetEvent.SaveSocketChannel(pclsSocketChnl);

	pclsSocketChnl->SetLogInstancePtr(m_clsThreadDealNetEvent.GetLogInstancePtr());

#if OPERATING_SYSTEM

	if (eProtocolType == NET_PROTOCOL_TCP)
	{
		pstruThreadPoolTask	 pTask = (pstruThreadPoolTask) malloc(sizeof(struThreadTask));//在任务结束时或程序退出时的在线程池的Uninitialize()中释放

		if ( NULL == pTask )
		{
			return ERROR_NET_UNKNOWN;
		}

		memset(pTask,0x0,sizeof(struThreadTask));
		pTask->pFunction = &CCommunicationManager::Listen;
		pTask->enumEvent = LISTEN;
		pTask->pObject = (CCommunicationManager*)&m_clsThreadDealNetEvent;
		pTask->pObject2 = pclsSocketChnl;
		m_clsThreadDealNetEvent.m_clsThreadPool.AssignTask(pTask);
		m_clsThreadDealNetEvent.m_clsThreadPool.WakeUpThread();

	}
	

#elif _WIN32
	
	if (eProtocolType == NET_PROTOCOL_UDP)
	{
		//加入完成端口
		m_clsThreadDealNetEvent.AddToIoCompletionPort((HANDLE)pBaseSocket->GetSocket(), (DWORD)pclsSocketChnl->GetIOCPHandleData());
		//发起接收
		pclsSocketChnl->RecvData();
	}
	else
	{
		//m_clsThreadDealNetEvent.m_clsThreadPool.AddThread(1);//增加一个线程去完成下面创建的任务,即监听

		pstruThreadPoolTask	 pTask = (pstruThreadPoolTask) malloc(sizeof(struThreadTask));//在任务结束时或程序退出时的在线程池的Uninitialize()中释放

		if ( NULL == pTask )
		{
			return ERROR_NET_UNKNOWN;
		}

		memset(pTask,0x0,sizeof(struThreadTask));
		pTask->pFunction = &CCommunicationManager::Listen;
		pTask->enumEvent = LISTEN;
		pTask->pObject = (CCommunicationManager*)&m_clsThreadDealNetEvent;
		pTask->pObject2 = pclsSocketChnl;
		m_clsThreadDealNetEvent.m_clsThreadPool.AssignTask(pTask);
		m_clsThreadDealNetEvent.m_clsThreadPool.WakeUpThread();
	}
	

#else	//linux
	
	if (eProtocolType == NET_PROTOCOL_UDP)
	{
		//加入EPOLL
		m_clsThreadDealNetEvent.EpollCtrl( EPOLL_CTL_ADD,pBaseSocket->GetSocket(),pclsSocketChnl->GetEpollEvent());

	}
	else
	{

		pstruThreadPoolTask	 pTask = (pstruThreadPoolTask) malloc(sizeof(struThreadTask));//在任务结束时或程序退出时的在线程池的Uninitialize()中释放

		if ( NULL == pTask )
		{
			return ERROR_NET_UNKNOWN;
		}

		memset(pTask,0x0,sizeof(struThreadTask));
		pTask->pFunction = &CCommunicationManager::Listen;
		pTask->enumEvent = LISTEN;
		pTask->pObject = (CCommunicationManager*)&m_clsThreadDealNetEvent;
		pTask->pObject2 = pclsSocketChnl;
		m_clsThreadDealNetEvent.m_clsThreadPool.AssignTask(pTask);
	}
#endif

	//转换
	*pSocketChnl = (ISocketChannel*)pclsSocketChnl;

	return ERROR_BASE_SUCCESS;
}
开发者ID:chcoffice,项目名称:Bull,代码行数:101,代码来源:NetService.cpp


注:本文中的CBaseSocket::GetSocket方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。