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

发布时间:2021-06-06

整理的一个简单的完成端口类

uf,0,MAX_BUF);
memset(&(pIo->ol),0,sizeof(WSAOVERLAPPED));
pIo->wsabuf.buf = pIo->buf;
pIo->wsabuf.len = MAX_BUF;
pIo->IoOperation = IO_READ;
ret = WSARecv(pHd->IoSocket,&(pIo->wsabuf),1,&dwRecv,&flag,&(pIo->ol),NULL);

if(SOCKET_ERROR == ret)
{
if(WSA_IO_PENDING != WSAGetLastError())
{
Disconnect(pHd->IoSocket);
InsertIoMem(pIo);
return;
}
}
}

//处理发送事件
void NetCompletionPort::SendEvent(LPPER_IO_CONTEXT pIo,LPPER_HANDLE_CONTEXT pHd,DWORD dwByte)
{
if(dwByte < 0)
{
InsertIoMem(pIo);
Disconnect(pHd->IoSocket);
}
else
{
InsertIoMem(pIo);
}
}

//处理接收事件
void NetCompletionPort::RecvEvent(LPPER_IO_CONTEXT pIo,LPPER_HANDLE_CONTEXT pHd,DWORD dwByte)
{
if(dwByte < 0)
{
InsertIoMem(pIo);
Disconnect(pHd->IoSocket);
}
else
{
RecvData(pIo->buf, GetClient(pHd->IoSocket));
DWORD flag = 0;
DWORD dwRecv = 0;
DWORD ret = 0;
memset(pIo->buf,0,MAX_BUF);
memset(&(pIo->ol),0,sizeof(WSAOVERLAPPED));
pIo->wsabuf.buf = pIo->buf;
pIo->wsabuf.len = MAX_BUF;
pIo->IoOperation = IO_READ;
ret = WSARecv(pHd->IoSocket,&(pIo->wsabuf),1,&dwRecv,&flag,&(pIo->ol),NULL);

if(SOCKET_ERROR == ret)
{
if(WSA_IO_PENDING != WSAGetLastError())
{
Disconnect(pHd->IoSocket);
InsertIoMem(pIo);
return;
}
}
}
}

//处理关闭事件
void NetCompletionPort::CloseEvent(LPPER_IO_CONTEXT pIo,LPPER_HANDLE_CONTEXT pHd)
{
Disconnect(pIo->sClient);
InsertIoMem(pIo);
}

//工作者线程函数
unsigned __stdcall NetCompletionPort::
(LPVOID param)
{
NetCompletionPort* p = (NetCompletionPort*)param;
HANDLE port = p->m_comPort;
DWORD dwByteSend;
DWORD dwByteRecv;
DWORD dwByteTrans;
bool flag;
DWORD ret;
BYTE opType;
LPPER_IO_CONTEXT pIoData = NULL;
LPPER_HANDLE_CONTEXT pHandleData = NULL;

while(p->m_isCreate)
{
pIoData = NULL;
pHandleData = NULL;
ret = GetQueuedCompletionStatus(port,&dwByteTrans,
(LPDWORD)&pHandleData,(LPOVERLAPPED*)&pIoData,1000);
if(FALSE == ret)
{
continue;
}
else if(pIoData == NULL)
{
return 0;
}
opType = pIoData->IoOperation;


if(opType != IO_ACCEPT && dwByteTrans == 0)
{
p->Disconnect(pHandleData->IoSocket);
p->InsertIoMem(pIoData);

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

精彩图片

热门精选

大家正在看

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

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

支付方式:

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

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