完成端口(IOCP)的实现(6)
发布时间:2021-06-06
发布时间: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())
上一篇:一年级上册语文生字自测练习组词