class CIOCPServer
{
CString m_sConnectString;
public:
CDatabase m_Database;
/*讀起用戶信息到內存*/
CIOCPServer();
virtual ~CIOCPServer();
//2005.6.2
BOOL StartServer();
BOOL StopServer();
BOOL InitSocket(SOCKET_STYPE stype,int port, int num = -1);
//綁定在端口上工作線程
static DWord WINAPI CompletionWorkerThread( void * lpParam);
private:
BOOL ProcessLoadMsg(LPCLIENTCONTEXT lpContext);
private:
//根據消息overlapped的類型,處理消息
BOOL ProcessIOMessage(SOCKET_STYPE stype,IOType opCode, LPCLIENTCONTEXT lpContext , DWord dwiOSize);
//在端口上產生線程,並創建完成端口
void CreateWorkerThread();
//關閉完成端口
void CloseCompletionPort();
//分配連接overlappedplus
LPOVERLAPPEDPLUS AllocateOverlappedPlus(IOType ioType);
//分配連接進入的客戶的相關信息
LPCLIENTCONTEXT AllocateContext();
//釋放overlappedplus
void FreeOverlappedPlus(LPOVERLAPPEDPLUS lpOlp);
private:
HANDLE m_hIocp; //完成端口句柄
DWord m_dwThreads; //線程數
};
CPP方式:
#include "IOCPServer.h"
#include "common.h"
CIOCPServer::CIOCPServer()
{
//socket初始化
WSADATA wsd;
WORD wVersionRequested = MAKEWord(2, 2);
int nResult = WSAStartup(wVersionRequested, &wsd);
if (nResult == SOCKET_ERROR)
{
WSACleanup();
}
if (LOBYTE(wsd.wVersion) != 2 || HIBYTE(wsd.wVersion) != 2)
{
WSACleanup();
}
}
CIOCPServer::~CIOCPServer()
{
WSACleanup();
}
//分配連接overlappedplus
LPOVERLAPPEDPLUS CIOCPServer::AllocateOverlappedPlus(IOType ioType)
{
OVERLAPPEDPLUS* pOlp = NULL;
pOlp = new OVERLAPPEDPLUS;
ZeroMemory(pOlp, sizeof(OVERLAPPEDPLUS));
pOlp->opCode = ioType;
return pOlp;
}
//分配連接進入的客戶的相關信息
LPCLIENTCONTEXT CIOCPServer::AllocateContext()
{
LPCLIENTCONTEXT lpContext = NULL;