完成端口(IOCP)的实现(9)
发布时间:2021-06-06
发布时间: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
上一篇:一年级上册语文生字自测练习组词