整理的一个简单的完成端口类
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);