手机版

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

发布时间: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));

完成端口(IOCP)的实现(7).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
×
二维码
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)