|
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![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 13 #define WIN32_LEAN_AND_MEAN 14 #include <windows.h> 15![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 16 #ifndef SOCKET 17 typedef unsigned int SOCKET; 18 #endif 19![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 20 #pragma once 21![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 22![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//************************************************************************/ 23![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* 应用自实现消息协议接口 */ 24![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//************************************************************************/ 25 class NET_API ISocketEvent 26![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) { 27 public: 28 virtual int OnRcvMsg(SOCKET hSock , const char* buf, int nLen) = 0; 29 }; 30![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//************************************************************************/ 31![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* 通讯服务端接口 */ 32![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//************************************************************************/ 33 class NET_API INetServer 34![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) { 35 public: 36 INetServer( ISocketEvent &event ):m_event(event) 37![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {} 38 virtual ~INetServer() 39![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {} 40![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 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![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 47 protected: 48 ISocketEvent &m_event; 49 }; 50![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//************************************************************************/ 51![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* 通讯客户端接口 */ 52![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//************************************************************************/ 53 class NET_API INetClient 54![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) { 55 public: 56 INetClient( ISocketEvent &event ):m_event(event) 57![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {} 58 virtual ~INetClient() 59![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {} 60![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 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![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 70 extern "C" 71![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) { 72 NET_API INetServer * CreateServer( ISocketEvent &event ); 73 NET_API INetClient * CreateClient( ISocketEvent &event ); 74 };
|