|
1 // 下列 ifdef 块是创建使从 DLL 导出更简单的 2 //宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 NET_EXPORTS 3 // 符号编译的。在使用此 DLL 的 4 //任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将 5 // NET_API 函数视为是从此 DLL 导入的,而此 DLL 则将用此宏定义的 6 // 符号视为是被导出的。 7 #ifdef NET_EXPORTS 8 #define NET_API __declspec(dllexport) 9 #else 10 #define NET_API __declspec(dllimport) 11 #endif 12 13 #define WIN32_LEAN_AND_MEAN 14 #include <windows.h> 15 16 #ifndef SOCKET 17 typedef unsigned int SOCKET; 18 #endif 19 20 #pragma once 21 22 /**//************************************************************************/ 23 /**//* 应用自实现消息协议接口 */ 24 /**//************************************************************************/ 25 class NET_API ISocketEvent 26  { 27 public: 28 virtual int OnRcvMsg(SOCKET hSock , const char* buf, int nLen) = 0; 29 }; 30 /**//************************************************************************/ 31 /**//* 通讯服务端接口 */ 32 /**//************************************************************************/ 33 class NET_API INetServer 34  { 35 public: 36 INetServer( ISocketEvent &event ):m_event(event) 37 {} 38 virtual ~INetServer() 39 {} 40 41 virtual bool Start( unsigned short usPort , unsigned int unRecvSize = 4*1024 ) = 0 ; 42 virtual bool Stop() = 0 ; 43 virtual void Release() = 0 ; 44 virtual void SendMsg( SOCKET hSock , const char *Buf, int nLen ) = 0; 45 virtual void BroadcastMsg( const char *buf , int len ) = 0 ; 46 47 protected: 48 ISocketEvent &m_event; 49 }; 50 /**//************************************************************************/ 51 /**//* 通讯客户端接口 */ 52 /**//************************************************************************/ 53 class NET_API INetClient 54  { 55 public: 56 INetClient( ISocketEvent &event ):m_event(event) 57 {} 58 virtual ~INetClient() 59 {} 60 61 virtual bool Start( const char *pszIp , unsigned short usPort , unsigned int nMaxConnections ) = 0 ; 62 virtual bool Stop() = 0 ; 63 virtual void Release() = 0 ; 64 virtual void SendMsg( SOCKET hSock , const char *Buf, int nLen ) = 0; 65 virtual void BroadcastMsg( const char *buf , int len ) = 0 ; 66 protected: 67 ISocketEvent &m_event; 68 }; 69 70 extern "C" 71  { 72 NET_API INetServer * CreateServer( ISocketEvent &event ); 73 NET_API INetClient * CreateClient( ISocketEvent &event ); 74 };
|