三年,三年,又三年,終於,終於,終於不用再等啦!就在今天,HP-Socket for Linux v1.0 震撼發佈!仍是同樣的接口,同樣的高效,同樣的簡便,同樣的味道。
HP-Socket for Linux提供了與Windows版本一致的API接口,實現代碼則徹底獨立。HP-Socket for Linux使用了C++14標準的新特性,須要GCC 5.x以上版本的編譯器來編譯。發佈包中提供了HP-Socket組件及示例Demo的Visual Studio項目工程,安裝配置好Visual Studio的Visual C++ for Linux Development插件後便可自行編譯。固然,你也能夠本身編寫Makefile文件來編譯這些代碼。
注:HP-Socket for Linux的編譯和運行須要2.6.32及以上版本的Linux內核。
html
HP-Socket提供如下幾類組件,詳細內容請參考《HP-Socket網絡通訊框架開發指南》: linux
安裝指引git
$ sudo ./install.sh -h Usage: install.sh [...O.P.T.I.O.N.S...] -----------------+------------------------------------------------ -p|--prefix : install/uninstall path (default: /usr/local) -l|--libdir : lib dir (default: x86 -> lib, x64 -> lib64) -d|--with-demo : install demos or not (default: true) -u|--uninstall : execute uninstall operation from install path -v|--version : print installing hp-socket version -h|--help : print this usage message -----------------+------------------------------------------------
工做流程github
示例代碼緩存
1 #include <hpsocket/HPSocket.h> 2 3 /* Listener Class */ 4 class CListenerImpl : public CTcpPullServerListener 5 { 6 7 public: 8 // 5. process network events 9 virtual EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen); 10 virtual EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient); 11 virtual EnHandleResult OnHandShake(ITcpServer* pSender, CONNID dwConnID); 12 virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, int iLength); 13 virtual EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); 14 virtual EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode); 15 virtual EnHandleResult OnShutdown(ITcpServer* pSender); 16 }; 17 18 int main(int argc, char* const argv[]) 19 { 20 // 1. Create listener object 21 CListenerImpl s_listener; 22 // 2. Create component object (and binding with listener object) 23 CTcpPullServerPtr s_pserver(&s_listener); 24 25 // 3. Start component object 26 if(!s_pserver->Start("0.0.0.0", 5555)) 27 exit(1); 28 29 /* wait for exit */ 30 // ... ... 31 32 // 6. (optional) Stop component object 33 s_pserver->Stop() 34 35 return 0; 36 37 // 7. Destroy component object automatically 38 // 8. Destroy listener object automatically 39 }
1 #include <hpsocket/HPSocket4C.h> 2 3 // 5. process network events 4 EnHandleResult __stdcall OnConnect(HP_Agent pSender, HP_CONNID dwConnID); 5 EnHandleResult __stdcall OnReceive(HP_Agent pSender, HP_CONNID dwConnID, int iLength); 6 EnHandleResult __stdcall OnSend(HP_Agent pSender, HP_CONNID dwConnID, const BYTE* pData, int iLength); 7 EnHandleResult __stdcall OnClose(HP_Agent pSender, HP_CONNID dwConnID, En_HP_SocketOperation enOperation, int iErrorCode); 8 EnHandleResult __stdcall OnShutdown(HP_Agent pSender); 9 10 int main(int argc, char* const argv[]) 11 { 12 HP_TcpPullAgentListener s_listener; 13 HP_TcpPullAgent s_agent; 14 15 // 1. Create listener object 16 s_listener = ::Create_HP_TcpPullAgentListener(); 17 // 2. Create component object (and binding with listener object) 18 s_agent = ::Create_HP_TcpPullAgent(s_listener); 19 20 /* Set listener callbacks */ 21 ::HP_Set_FN_Agent_OnConnect(s_listener, OnConnect); 22 ::HP_Set_FN_Agent_OnSend(s_listener, OnSend); 23 ::HP_Set_FN_Agent_OnPullReceive(s_listener, OnReceive); 24 ::HP_Set_FN_Agent_OnClose(s_listener, OnClose); 25 ::HP_Set_FN_Agent_OnShutdown(s_listener, OnShutdown); 26 27 // 3. Start component object 28 if(::HP_Agent_HasStarted(s_agent)) 29 exit(1); 30 31 // 4. Connect to dest host 32 ::HP_Agent_Connect(s_agent, "remote.host.1", REMOTE_PORT_1, nullptr); 33 ::HP_Agent_Connect(s_agent, "remote.host.2", REMOTE_PORT_2, nullptr); 34 ::HP_Agent_Connect(s_agent, "remote.host.3", REMOTE_PORT_3, nullptr); 35 36 /* wait for exit */ 37 // ... ... 38 39 // 6. (optional) Stop component object 40 ::HP_Agent_Stop(s_agent) 41 42 // 7. Destroy component object 43 ::Destroy_HP_TcpPullAgent(s_agent); 44 // 8. Destroy listener object 45 ::Destroy_HP_TcpPullAgentListener(s_listener); 46 47 return 0; 48 }
組件列表網絡
HP-Socket for Linux v1.0目前發佈了一系列基礎組件,後續版本將會陸續發佈SSL組件和HTTP組件。架構