www.pudn.com > ChatUseIOCP.rar > DtServerSocket.h


#pragma once 
 
 
#ifndef _VECTOR_ 
#include  
#endif 
 
#ifndef DTEXCEPTION 
	#include "dtexception.h" 
	#ifndef DTEXCEPTION 
		#error DtServerSocketClient, DtException must be included 
	#endif 
#endif 
 
#include  
#pragma comment(lib, "Ws2_32.lib") 
#pragma comment(lib, "Mswsock.lib") 
 
#define DTSERVERID 9999			/// All server log messages will use this id. 
 
using namespace std; 
 
//#include "DtServerSocketClient.h" 
namespace Datatal 
{ 
 
#ifndef CUSTOM_ERROR 
#define CUSTOM_ERROR						15000L 
#endif 
 
#define SERVER_ERROR						CUSTOM_ERROR + 100 
#define SERVER_ERROR_RECIEVE				SERVER_ERROR + 1 
#define SERVER_ERROR_SEND					SERVER_ERROR + 2 
#define SERVER_ERROR_GENERAL				SERVER_ERROR + 3 
#define SERVER_ERROR_BUFFER_OVERFLOW		SERVER_ERROR + 4 
 
class DtServerSocketClient; 
 
////////////////////////////////////////////////////////////////////////// 
/// 
///		 
///  @class			DtServerSocket 
///  @version		1.0 
///	 @author		Jonas Gauffin 
///  @homepage		http://www.gauffin.org/cpp 
///	 
///  @brief			Baseclass for the server - Part of the IOCP Server framework.  
///  
///  @since 2003-05-10	jg	File created 
///  @since	2003-08-01	jg	Major rebuild of the class. 
/// 
////////////////////////////////////////////////////////////////////////// 
class DtServerSocket 
{ 
 
protected: 
	SOCKET		m_sdListen;					/// Socket used to listen. 
	DWORD		m_dwPort;					/// Port that we listen on. 
	size_t		m_nClients;					/// Number of loaded clients 
	size_t		m_nMaxClients;				/// Maximum number of clients that we may have. 
	DWORD		m_dwServerFull;				/// When the server got full. 
	typedef vector< DtServerSocketClient* > CLIENTS; 
	CLIENTS		m_paClients; 
 
	DtCriticalSection m_cs; 
 
public: 
	DtServerSocket(void); 
	~DtServerSocket(void); 
 
 
	/// Load a client, the only thing you have to do is to derive this method. 
	/// @param pClient Should return a pointer to a DtServerSocketClient derived class. 
	/// @param nId A id that identifieds the client. 
	virtual void LoadClient(DtServerSocketClient** pClient, int& nId) = 0; 
 
 
	/// @param dwListenPort The port that we should wait for connections on. 
	/// @param nClients Number of initial clients 
	/// @param nMaxClients Total number of clients that the server could have. 
	/// @returns 0 if success or system error code. 
	DWORD StartServer(DWORD dwListenPort, int nClients, int nMaxClients); 
 
	/// Stops the server. 
	void StopServer(void); 
 
	/// Check's how long the clients have been connected. 
	/// You should run this function in your main loop. 
	virtual void Maintenance(); 
 
	/// IoCompletion of the client threads calls this method. 
	static void CALLBACK DoneIO(DWORD dwErrorCode, 
		DWORD dwNumberOfBytesTransferred, 
		LPOVERLAPPED lpOverlapped); 
 
 
	/// Override this one to give the server a name. Gr8 for debugging purposes. 
	virtual const char* GetServerName() { return "A server"; }; 
 
	/// Override this one to get log printings 
	virtual void OnWriteLog(int nPrio, int nClientId, const char* pszCategory, const char* pszString) const {}; 
	void WriteLog(int nPrio, int nClientId, const char* pszCategory, const char* pszString, ...) const; 
 
}; 
 
class DtServerException : public DtException 
{ 
public: 
	/// A constructor for our exceptionclass 
	/// @param nErrorNumber The error that were raised. Should be GetLastError or -1 for custom error. 
	/// @param Name Name of the current error 
	/// @param Description A more detailed information about the raised error. 
	DtServerException(int nErrorNumber, const char* pszName, const char* pszFormattedString) 
	{ 
		m_nLine = 0; 
		m_szFileName[0] = NULL; 
 
		SetErrorNumber(nErrorNumber); 
		SetName(pszName); 
		SetDescription(pszFormattedString); 
	}; 
}; 
 
}; //namespace