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