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

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

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

精彩图片

热门精选

大家正在看

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

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

支付方式:

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

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