完成端口(IOCP)的实现(11)
发布时间:2021-06-06
发布时间:2021-06-06
整理的一个简单的完成端口类
continue;
}
if(opType == IO_ACCEPT)
{
//释放接点
p->ReleaseConnect((DWORD)pIoData->sClient);
if(SOCKET_ERROR == setsockopt(p->m_listenSocket,SOL_SOCKET,SO_UPDATE_ACCEPT_CONTEXT,
(char*)&(pIoData->sClient),sizeof(pIoData->sClient)))
{
::closesocket(pIoData->sClient);
p->InsertIoMem(pIoData);
continue;
}
else
{
//分配单句柄数据
LPPER_HANDLE_CONTEXT pNeW = p->GetHandleMem();
pNeW->IoSocket = pIoData->sClient;
if(!CreateIoCompletionPort((HANDLE)pIoData->sClient,p->m_comPort,(DWORD)pNeW,0))
{
::closesocket(pIoData->sClient);
p->InsertIoMem(pIoData);
p->InsertHandleMem(pNeW);
continue;
}
p->AcceptEvent(pIoData,pNeW,dwByteTrans);
}
continue;
}
else if(opType == IO_READ)
{
p->RecvEvent(pIoData,pHandleData,dwByteTrans);
continue;
}
else if(opType == IO_WRITE)
{
p->SendEvent(pIoData,pHandleData,dwByteTrans);
continue;
}
else
{
p->Disconnect(pHandleData->IoSocket);
p->InsertIoMem(pIoData);
continue;
}
}
return 1;
}
//accept线程函数,管理连接,关闭长时间没有收发动作的连接
unsigned __stdcall NetCompletionPort::AcceptThreadFun(LPVOID param)
{
NetCompletionPort* p=(NetCompletionPort*)param;
DWORD ret=0;
while(p->m_isCreate)
{
ret=::WaitForSingleObject(p->m_hEventListen,1000);
if(WAIT_FAILED == ret)
{
return 0;
}
else if(WAIT_TIMEOUT == ret)
{
int optValue = 0;
int len = sizeof(optValue);
int ret = 0;
int count = 0;
EnterCriticalSection(&p->m_conSection);
std::set<LPPER_IO_CONTEXT>::iterator pos = p->m_pConnectList->begin();
while(pos != p->m_pConnectList->end()) //处理没有数据收发的链接
{
ret = ::getsockopt((*pos)->sClient,SOL_SOCKET,SO_CONNECT_TIME,(char*)&optValue,&len);
if(ret == SOCKE
T_ERROR)
{
++pos;
continue;
}
else if(optValue != -1 && optValue >= 10)
{
::closesocket((*pos)->sClient);
上一篇:一年级上册语文生字自测练习组词