本文整理汇总了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;
}
示例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;
}