www.pudn.com > ChatRoom.zip > IOCPServer.h, change:2011-07-29,size:1613b


#pragma once 
 
#include <WinSock2.h> 
 
#define MAX_BUF_SIZE	4096 
enum EIoType 
{ 
	TYPE_ACCEPT, 
	TYPE_READ, 
	TYPE_WRITE, 
	TYPE_CLOSE, 
	TYPE_UNKNOW 
}; 
 
class COverlappedEx 
{ 
public: 
	WSAOVERLAPPED m_overLapped; 
	EIoType m_nIoType; 
	char m_szBuf[MAX_BUF_SIZE]; 
 
	COverlappedEx(EIoType nIoType) 
	{ 
		m_nIoType = nIoType; 
		memset(&m_overLapped, 0, sizeof(WSAOVERLAPPED)); 
		memset(m_szBuf, 0, sizeof(m_szBuf)); 
		m_overLapped.hEvent = WSACreateEvent(); 
	} 
}; 
 
class CPerSocketData 
{ 
public: 
	SOCKET m_socketListen; 
	SOCKET m_socketClient; 
	 
 
	CPerSocketData() 
	{ 
		m_socketClient = NULL; 
		m_socketListen = NULL; 
	} 
}; 
 
class CIOCPServer 
{ 
public: 
	CIOCPServer(void); 
	~CIOCPServer(void); 
 
public: 
	//主要的功能:接口,给使用者来用的 
	//启动服务器 
	BOOL StartServer(UINT nListenPort); 
 
	//停止服务器 
	BOOL StopServer(); 
 
	//投递请求:Accept 
	BOOL PostAccept(CPerSocketData* pSocketData); 
	//投递请求:Recv 
	BOOL PostRecv(CPerSocketData* pSocketData); 
	//投递请求:Send 
	BOOL PostSend(LPCTSTR lpszText, DWORD dwLength); 
 
	CString GetLastErrorMsg(); 
 
protected: 
	//内部用的的函数,自我调用 
	//服务的线程 
	static DWORD WINAPI ThreadTalking(LPVOID lParam); 
	//加载WinSock库 
	BOOL InitWinSock(); 
	//绑定的函数 
	CPerSocketData* AssignSocketToCompletionPort(SOCKET hSocket, BOOL bListenSocket); 
 
	void CloseSocket(); 
 
protected: 
	long m_nThreadNum; 
	HANDLE m_hCompletionPort; 
 
	CString m_strError; 
 
	SOCKET m_socketListen; 
 
	CList<CPerSocketData*, CPerSocketData*>m_lstSocketData; 
	CList<COverlappedEx*, COverlappedEx*>m_lstOverLapEx; 
};