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

发布时间:2021-06-06

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

ool m_isCreate;

std::vector<WorkThread*>* m_pWorkThreadList;
WorkThread* m_pThreadAccept;

bool InitWinSock();
bool InitListenSocket();
bool InitListenEvent();
bool InitWorkThread();

std::map<int,NetClient*>* m_pClientList;
CRITICAL_SECTION m_clientListSection;
void DeleteClient(NetClient* pClient);
void AddClient(NetClient* pClient);
NetClient* GetClient(int socket);

std::set<LPPER_IO_CONTEXT>* m_pConnectList;
CRITICAL_SECTION m_conSection;
void InsertConnect(LPPER_IO_CONTEXT pConnect);
void ReleaseConnect(int socket);

std::deque<LPPER_IO_CONTEXT>* m_pIoMemList;
std::deque<LPPER_HANDLE_CONTEXT>* m_pHandleMemList;
CRITICAL_SECTION m_ioMenListSection;
CRITICAL_SECTION m_hdMemListSection;
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



view plaincopy to clipboardprint?#include "stdafx.h"
#include "NetCompletionPort.h"
#include <ws2tcpip.h>

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////WorkThrerd

NetCompletionPort* g_pHostServer = NULL;

WorkThread::WorkThread()
{
m_hThread = NULL;
m_nThreadID = 0;
pnThreadFun = NULL;
m_pParam = NULL;
}

WorkThread::WorkThread(unsigned int (__stdcall*pnThreadFun)(LPVOID param),void* param)
{
m_hThread = NULL;
m_nThreadID = 0;
this->pnThreadFun = pnThreadFun;
m_pParam = param;
}

WorkThread::~WorkThread()
{
}

bool WorkThread::Init()
{
m_hThread = (HANDLE)_beginthreadex(NULL,0,pnThreadFun,m_pParam,CREATE_SUSPENDED,&m_nThreadID);//CREATE_SUSPENDED
if(m_hThread)
return TRUE;
return FALSE;
}

void WorkThread::Start()
{
if(m_hThrea
d)
ResumeThread(m_hThread);
}

void WorkThread::Stop()
{
if (m_hThread)
{
DWORD code;
int count = 0;
while(TR

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

精彩图片

热门精选

大家正在看

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

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

支付方式:

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

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