手机版

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

发布时间:2021-06-06   来源:未知    
字号:

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

emListSection;
LPPER_IO_CONTEXT GetIoMem();
LPPER_HANDLE_CONTEXT GetHandleMem();
void InsertIoMem(LPPER_IO_CONTEXT pMem);
void InsertHandleMem(LPPER_HANDLE_CONTEXT pMem);

bool PostAcceptEx();

protected:
public:
NetCompletionPort();
virtual ~NetCompletionPort();
bool StartServer(int port);
void StopServer();

virtual bool Disconnect(int socket);
void SendPacket(int socket,void * pData,bool encrypt = TRUE);
virtual void EncryptPacket(void * pData,int & length,BYTE type,bool encrypt = TRUE);
virtual void RecvData(void * pData,NetClient* pClient);

void AcceptEvent(LPPER_IO_CONTEXT pIo,LPPER_HANDLE_CONTEXT pHd,DWORD dwByte);
void SendEvent(LPPER_IO_CONTEXT pIo,LPPER_HANDLE_CONTEXT pHd,DWORD dwByte);
void RecvEvent(LPPER_IO_CONTEXT pIo,LPPER_HANDLE_CONTEXT pHd,DWORD dwByte);
void CloseEvent(LPPER_IO_CONTEXT pIo,LPPER_HANDLE_CONTEXT pHd);

static unsigned __stdcall WorkThreadFun(LPVOID param);
static unsigned __stdcall AcceptThreadFun(LPVOID param);

};

#pragma pack (pop)

#endif
#ifndef _COMPLETIONPORT_H
#define _COMPLETIONPORT_H

#include <winsock2.h>
#include <mswsock.h>
#include <process.h>
#include <vector>
#include <map>
#include <deque>
#include <set>

#define MAX_MEM_COUNT 1000
#define MAX_BUF 1024
#define RECV_COUNT 5

#pragma pack (push)
#pragma pack (1)

enum
{
IO_ACCEPT,
IO_READ,
IO_WRITE,
IO_END,
};

enum
{
IO_SEND,
IO_RECV
};


//单IO数据
typedef struct _PER_IO_CONTEXT
{
WSAOVERLAPPED ol;
char buf[MAX_BUF];
WSABUF wsabuf;
SOCKET sClient;
DWORD id;
BYTE IoOperation;
}PER_IO_CONTEXT,*LPPER_IO_CONTEXT;

//完成键(单句炳数据)
typedef struct _PER_HANDLE_CONTEXT
{
SOCKET IoSocket;

}PER_HANDLE_CONTEXT,*LPPER_HANDLE_CONTEXT;

static GUID g_GUIDAcceptEx = WSAID_ACCEPTEX;
static GUID g_GUIDGetAcceptExSockaddrs = WSAID_GETACCEPTEXSOCKADDRS;

//工作线程类
class WorkThread
{
private:
HANDLE m_hThread;
unsigned int m_nThreadID;
unsigned int (__stdcall*pnThreadFun)(LPVOID param);
void* m_pParam;
public:
WorkThread();
WorkThread(unsigned int (__stdcall*pnThreadFun)(LPVOID param),void* param);
~WorkThread();
bool Init();
void Start();
void Stop();
void SendMessage(UINT Msg, WPARAM wParam, LPARAM lParam);
void SetPriority(int priority);
};

//客户端链接类
class NetClient
{
public:
SOCKET m_socket;
SOCKADDR_IN m_address;
LPPER_HANDLE_CONTEXT m_pComKey;
NetClient();
NetClient(int socket);
~NetClient();
void SendPacket(void* pData,bool encrypt = TRUE);
void Disconnect();
};

//完成端口类
class NetCompletionPort
{
private:
LPFN_ACCEPTEX m_pAcc
eptEx;
LPFN_GETACCEPTEXSOCKADDRS m_pGetAcceptExSockaddrs;
HANDLE m_comPort;
int m_port;
SOCKET m_listenSocket;
HANDLE m_hEventListen;
b

完成端口(IOCP)的实现(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
×
二维码
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)