完成端口(IOCP)的实现(6)

发布时间:2021-06-06

整理的一个简单的完成端口类


if(SOCKET_ERROR == WSAIoctl(m_listenSocket,SIO_GET_EXTENSION_FUNCTION_POINTER, //获取扩展函数调用
&g_GUIDGetAcceptExSockaddrs,sizeof(g_GUIDGetAcceptExSockaddrs),&m_pGetAcceptExSockaddrs,sizeof(m_pGetAcceptExSockaddrs),
&dwTemp,NULL,NULL))
{
m_pGetAcceptExSockaddrs = NULL;
return FALSE;
}

return TRUE;
}

//设置监听端口的FD_ACCEPT事件
bool NetCompletionPort::InitListenEvent()
{
m_hEventListen = CreateEvent(NULL,FALSE,FALSE,NULL);

if(NULL == m_hEventListen)
return FALSE;

if(SOCKET_ERROR == ::WSAEventSelect(m_listenSocket,m_hEventListen,FD_ACCEPT))
return FALSE;

return TRUE;
}

//创建Accept处理线程
bool NetCompletionPort::InitWorkThread()
{
m_pThreadAccept = new WorkThread(AcceptThreadFun,this);
if( !m_pThreadAccept->Init() )
{
delete m_pThreadAccept;
return FALSE;
}
return TRUE;
}

//停止完成端口服务
void NetCompletionPort::StopServer()
{
m_isCreate = FALSE;

if(m_comPort)
{
PostQueuedCompletionStatus(m_comPort,0,0,NULL); //注意:对应n个线程应该有n的post
CloseHandle(m_comPort);
m_comPort = NULL;
}

if(m_pThreadAccept != NULL) //停止accept线程
{
m_pThreadAccept->Stop();
delete m_pThreadAccept;
m_pThreadAccept = NULL;
}

std::vector<WorkThread*>::iterator pos = m_pWorkThreadList->begin();

while (pos != m_pWorkThreadList->end()) //停止所有工作线程
{
(*pos)->Stop();
delete (*pos);
pos++;
}

m_pWorkThreadList->clear();

if(m_listenSocket != NULL) //关闭监听端口
{
closesocket(m_listenSocket);
m_listenSocket = NULL;
}

if(m_hEventListen != NULL)
{
CloseHandle(m_hEventListen);
m_hEventListen = NULL;
}

std::map<int,NetClient*>::iterator pos1 = m_pClientList->begin();
while(pos1 != m_pClientList->end()) //断开所有客户端连接
{
NetClient* pClient = pos1->second;
pClient->Disconnect();
InsertHandleMem(pClient->m_pComKey);
delete pClient;
pos1++;
}

m_pClientList->clear();

std::set<LPPER_IO_CONTEXT>::iterator pos2 = m_pConnectList->begin();
while(pos2 != m_pConnectList->end()) //释放所有连接请求IO资源
{
free(*pos);
pos2++;
}

m_pConnectList
->clear();

std::deque<LPPER_IO_CONTEXT>::iterator pos3 = m_pIoMemList->begin();
while(pos3 != m_pIoMemList->end())

完成端口(IOCP)的实现(6).doc 将本文的Word文档下载到电脑

精彩图片

热门精选

大家正在看

× 游客快捷下载通道(下载后可以自由复制和排版)

限时特价:7 元/份 原价:20元

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219