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

发布时间:2021-06-06

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

rlt = TRUE;
}
LeaveCriticalSection(&m_clientListSection);
return rlt;
}

//向客户端发送数据
void NetCompletionPort::SendPacket(int socket,void * pData,bool encrypt)
{
DWORD SendBytes,Flags;
Flags = 0;
int length = 0;
EncryptPacket(pData, length, IO_SEND, encrypt); //加密数据
if(length > MAX_BUF)
return;
LPPER_IO_CONTEXT pIoMem = GetIoMem(); //申请一个IO资源
pIoMem->IoOperation = IO_WRITE;
pIoMem->sClient = socket;
pIoMem->id = socket;
memset(pIoMem->buf,0,MAX_BUF);
pIoMem->wsabuf.buf = pIoMem->buf;
pIoMem->wsabuf.len = MAX_BUF;
memset(&(pIoMem->ol),0,sizeof(WSAOVERLAPPED));
memcpy(pIoMem->buf,pData,length);

WSASend(socket,&pIoMem->wsabuf,1,&SendBytes,Flags,&(pIoMem->ol),NULL);

EncryptPacket(pData,length,IO_SEND,encrypt); //???为什么还要执行
}

//加密数据
void NetCompletionPort::EncryptPacket(void * pData,int & length,BYTE type,bool encrypt)
{
}

//接收数据
void NetCompletionPort::RecvData(void * pData,NetClient* pClient)
{
}

//处理接受连接事件
void NetCompletionPort::AcceptEvent(LPPER_IO_CONTEXT pIo, LPPER_HANDLE_CONTEXT pHd, DWORD dwByte)
{
DWORD flag = 0;
DWORD dwRecv = 0;
DWORD ret = 0;
NetClient* pClient = new NetClient(pHd->IoSocket);
pClient->m_pComKey = pHd;
AddClient(pClient); //增加一个客户端
int count = 0;

while(count < (RECV_COUNT - 1))
{
LPPER_IO_CONTEXT p = GetIoMem();
memset(p,0,sizeof(PER_IO_CONTEXT));
p->sClient = pIo->sClient;
p->id = pIo->id;
p->wsabuf.buf = p->buf;
p->wsabuf.len = MAX_BUF;
p->IoOperation = IO_READ;

ret = WSARecv(pHd->IoSocket,&(p->wsabuf),1,&dwRecv,&flag,&(p->ol),NULL); //接收连接数据

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

if(dwByte > 0)
{
SOCKADDR * lpLocalSockaddr = NULL,* lpRemoteSockaddr = NULL;
int LocalSockAddrLen = 0,RemoteSockaddrLen = 0;
m_pGetAcceptExSockaddrs(pIo->buf,pIo->wsabuf.len - ((sizeof(SOCKADDR_IN) + 16)*2),
sizeof(SOCKADDR_IN) + 16,sizeof(SOCKADDR_IN) + 16,&lpLocalSockaddr,&LocalSockAddrLen,
&lpRemoteSockaddr,&RemoteSockaddrLen);
if(lpRemoteSockaddr != N
ULL)
memcpy(&pClient->m_address,lpRemoteSockaddr,sizeof(SOCKADDR_IN));

RecvData(pIo->buf, pClient);
}

memset(pIo->b

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

精彩图片

热门精选

大家正在看

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

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

支付方式:

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

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