完成端口(IOCP)的实现(7)
发布时间:2021-06-06
发布时间:2021-06-06
整理的一个简单的完成端口类
//释放所有IO资源
{
free(*pos3);
pos3++;
}
m_pIoMemList->clear();
std::deque<LPPER_HANDLE_CONTEXT>::iterator pos4 = m_pHandleMemList->begin();
while(pos4 != m_pHandleMemList->end()) //释放所有句柄资源
{
free(*pos4);
pos4++;
}
m_pHandleMemList->clear();
WSACleanup();
}
//释放一个客户端并归还句柄资源
void NetCompletionPort::DeleteClient(NetClient* pClient)
{
if(pClient == NULL)
return;
EnterCriticalSection(&m_clientListSection);
std::map<int,NetClient*>::iterator pos = m_pClientList->find(pClient->m_socket);
if(pos != m_pClientList->end())
{
NetClient* pClient = pos->second;
pClient->Disconnect();
InsertHandleMem(pClient->m_pComKey);
delete pClient;
m_pClientList->erase(pClient->m_socket);
}
LeaveCriticalSection(&m_clientListSection);
}
//增加一个客户端
void NetCompletionPort::AddClient(NetClient* pClient)
{
if(pClient == NULL)
return;
EnterCriticalSection(&m_clientListSection);
m_pClientList->insert(std::make_pair(pClient->m_socket,pClient));
LeaveCriticalSection(&m_clientListSection);
}
//获取一个客户端
NetClient* NetCompletionPort::GetClient(int socket)
{
NetClient* pClient = NULL;
EnterCriticalSection(&m_clientListSection);
std::map<int,NetClient*>::iterator pos = m_pClientList->find(socket);
if(pos != m_pClientList->end())
pClient = pos->second;
LeaveCriticalSection(&m_clientListSection);
return pClient;
}
//增加一个连接请求
void NetCompletionPort::InsertConnect(LPPER_IO_CONTEXT pConnect)
{
if(pConnect != NULL)
{
EnterCriticalSection(&m_conSection);
m_pConnectList->insert(pConnect);
LeaveCriticalSection(&m_conSection);
}
}
//释放一个连接请求
void NetCompletionPort::ReleaseConnect(int socket)
{
EnterCriticalSection(&m_conSection);
std::set<LPPER_IO_CONTEXT>::iterator pos = m_pConnectList->begin();
while (pos != m_pConnectList->end())
{
if((*pos)->sClient == socket)
{
m_pConnectList->erase(pos);
break;
}
pos++;
}
LeaveCriticalSection(&m_conSection);
}
//获取一个IO资源
LPPER_IO_CONTEXT NetCompletionPort::GetIoMem()
{
LPPER_IO_CONTEXT pData = NULL;
EnterCriticalSection(&m_ioMenListSection);
if(m_pIoMemList->size() > 0)
{
pDa
ta = m_pIoMemList->front();
m_pIoMemList->pop_front();
}
else
{
pData = (LPPER_IO_CONTEXT)malloc(sizeof(PER_IO_CONTEXT));
上一篇:一年级上册语文生字自测练习组词