HPSocket.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 3.6.1
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. /******************************************************************************
  25. Module: HPSocket
  26. Usage:
  27. 方法一:
  28. --------------------------------------------------------------------------------------
  29. 0. 应用程序包含 HPTypeDef.h / SocketInterface.h / HPSocket.h 头文件
  30. 1. 调用 HP_Create_Xxx() 函数创建 HPSocket 对象
  31. 2. 使用完毕后调用 HP_Destroy_Xxx() 函数销毁 HPSocket 对象
  32. 方法二:
  33. --------------------------------------------------------------------------------------
  34. 0. 应用程序包含 SocketInterface.h 和 HPSocket.h 头文件
  35. 1. 创建 CXxxPtr 智能指针,通过智能指针使用 HPSocket 对象
  36. Release:
  37. <-- 动态链接库 -->
  38. 1. x86/HPSocket.dll - (32位/MBCS/Release)
  39. 2. x86/HPSocket_D.dll - (32位/MBCS/DeBug)
  40. 3. x86/HPSocket_U.dll - (32位/UNICODE/Release)
  41. 4. x86/HPSocket_UD.dll - (32位/UNICODE/DeBug)
  42. 5. x64/HPSocket.dll - (64位/MBCS/Release)
  43. 6. x64/HPSocket_D.dll - (64位/MBCS/DeBug)
  44. 7. x64/HPSocket_U.dll - (64位/UNICODE/Release)
  45. 8. x64/HPSocket_UD.dll - (64位/UNICODE/DeBug)
  46. <-- 静态链接库 -->
  47. !!注意!!:使用 HPSocket 静态库时,需要在工程属性中定义预处理宏 -> HPSOCKET_STATIC_LIB
  48. 1. x86/static/HPSocket.lib - (32位/MBCS/Release)
  49. 2. x86/static/HPSocket_D.lib - (32位/MBCS/DeBug)
  50. 3. x86/static/HPSocket_U.lib - (32位/UNICODE/Release)
  51. 4. x86/static/HPSocket_UD.lib - (32位/UNICODE/DeBug)
  52. 5. x64/static/HPSocket.lib - (64位/MBCS/Release)
  53. 6. x64/static/HPSocket_D.lib - (64位/MBCS/DeBug)
  54. 7. x64/static/HPSocket_U.lib - (64位/UNICODE/Release)
  55. 8. x64/static/HPSocket_UD.lib - (64位/UNICODE/DeBug)
  56. ******************************************************************************/
  57. #pragma once
  58. /**************************************************/
  59. /*********** imports / exports HPSocket ***********/
  60. #ifdef HPSOCKET_STATIC_LIB
  61. #define HPSOCKET_API EXTERN_C
  62. #else
  63. #ifdef HPSOCKET_EXPORTS
  64. #define HPSOCKET_API EXTERN_C __declspec(dllexport)
  65. #else
  66. #define HPSOCKET_API EXTERN_C __declspec(dllimport)
  67. #endif
  68. #endif
  69. #include "SocketInterface.h"
  70. /*****************************************************************************************************************************************************/
  71. /****************************************************************** TCP/UDP Exports ******************************************************************/
  72. /*****************************************************************************************************************************************************/
  73. /**************************************************/
  74. /************** HPSocket 对象智能指针 **************/
  75. template<class T, class _Listener, class _Creator> class CHPSocketPtr
  76. {
  77. public:
  78. CHPSocketPtr(_Listener* pListener = nullptr)
  79. {
  80. if(pListener)
  81. m_pObj = _Creator::Create(pListener);
  82. else
  83. m_pObj = nullptr;
  84. }
  85. ~CHPSocketPtr()
  86. {
  87. Reset();
  88. }
  89. public:
  90. CHPSocketPtr& Reset(T* pObj = nullptr)
  91. {
  92. if(pObj != m_pObj)
  93. {
  94. if(m_pObj)
  95. _Creator::Destroy(m_pObj);
  96. m_pObj = pObj;
  97. }
  98. return *this;
  99. }
  100. CHPSocketPtr& Attach(T* pObj)
  101. {
  102. return Reset(pObj);
  103. }
  104. T* Detach()
  105. {
  106. T* pObj = m_pObj;
  107. m_pObj = nullptr;
  108. return pObj;
  109. }
  110. BOOL IsValid () const {return m_pObj != nullptr ;}
  111. T* Get () const {return m_pObj ;}
  112. T* operator -> () const {return m_pObj ;}
  113. operator T* () const {return m_pObj ;}
  114. CHPSocketPtr& operator = (T* pObj) {return Reset(pObj) ;}
  115. private:
  116. CHPSocketPtr(const CHPSocketPtr&);
  117. CHPSocketPtr& operator = (const CHPSocketPtr&);
  118. private:
  119. T* m_pObj;
  120. };
  121. /**************************************************/
  122. /**************** HPSocket 导出函数 ****************/
  123. // 创建 ITcpServer 对象
  124. HPSOCKET_API ITcpServer* HP_Create_TcpServer(ITcpServerListener* pListener);
  125. // 创建 ITcpAgent 对象
  126. HPSOCKET_API ITcpAgent* HP_Create_TcpAgent(ITcpAgentListener* pListener);
  127. // 创建 ITcpClient 对象
  128. HPSOCKET_API ITcpClient* HP_Create_TcpClient(ITcpClientListener* pListener);
  129. // 创建 ITcpPullServer 对象
  130. HPSOCKET_API ITcpPullServer* HP_Create_TcpPullServer(ITcpServerListener* pListener);
  131. // 创建 ITcpPullAgent 对象
  132. HPSOCKET_API ITcpPullAgent* HP_Create_TcpPullAgent(ITcpAgentListener* pListener);
  133. // 创建 ITcpPullClient 对象
  134. HPSOCKET_API ITcpPullClient* HP_Create_TcpPullClient(ITcpClientListener* pListener);
  135. // 创建 ITcpPackServer 对象
  136. HPSOCKET_API ITcpPackServer* HP_Create_TcpPackServer(ITcpServerListener* pListener);
  137. // 创建 ITcpPackAgent 对象
  138. HPSOCKET_API ITcpPackAgent* HP_Create_TcpPackAgent(ITcpAgentListener* pListener);
  139. // 创建 ITcpPackClient 对象
  140. HPSOCKET_API ITcpPackClient* HP_Create_TcpPackClient(ITcpClientListener* pListener);
  141. // 创建 IUdpServer 对象
  142. HPSOCKET_API IUdpServer* HP_Create_UdpServer(IUdpServerListener* pListener);
  143. // 创建 IUdpClient 对象
  144. HPSOCKET_API IUdpClient* HP_Create_UdpClient(IUdpClientListener* pListener);
  145. // 创建 IUdpCast 对象
  146. HPSOCKET_API IUdpCast* HP_Create_UdpCast(IUdpCastListener* pListener);
  147. // 销毁 ITcpServer 对象
  148. HPSOCKET_API void HP_Destroy_TcpServer(ITcpServer* pServer);
  149. // 销毁 ITcpAgent 对象
  150. HPSOCKET_API void HP_Destroy_TcpAgent(ITcpAgent* pAgent);
  151. // 销毁 ITcpClient 对象
  152. HPSOCKET_API void HP_Destroy_TcpClient(ITcpClient* pClient);
  153. // 销毁 ITcpPullServer 对象
  154. HPSOCKET_API void HP_Destroy_TcpPullServer(ITcpPullServer* pServer);
  155. // 销毁 ITcpPullAgent 对象
  156. HPSOCKET_API void HP_Destroy_TcpPullAgent(ITcpPullAgent* pAgent);
  157. // 销毁 ITcpPullClient 对象
  158. HPSOCKET_API void HP_Destroy_TcpPullClient(ITcpPullClient* pClient);
  159. // 销毁 ITcpPackServer 对象
  160. HPSOCKET_API void HP_Destroy_TcpPackServer(ITcpPackServer* pServer);
  161. // 销毁 ITcpPackAgent 对象
  162. HPSOCKET_API void HP_Destroy_TcpPackAgent(ITcpPackAgent* pAgent);
  163. // 销毁 ITcpPackClient 对象
  164. HPSOCKET_API void HP_Destroy_TcpPackClient(ITcpPackClient* pClient);
  165. // 销毁 IUdpServer 对象
  166. HPSOCKET_API void HP_Destroy_UdpServer(IUdpServer* pServer);
  167. // 销毁 IUdpClient 对象
  168. HPSOCKET_API void HP_Destroy_UdpClient(IUdpClient* pClient);
  169. // 销毁 IUdpCast 对象
  170. HPSOCKET_API void HP_Destroy_UdpCast(IUdpCast* pCast);
  171. // ITcpServer 对象创建器
  172. struct TcpServer_Creator
  173. {
  174. static ITcpServer* Create(ITcpServerListener* pListener)
  175. {
  176. return HP_Create_TcpServer(pListener);
  177. }
  178. static void Destroy(ITcpServer* pServer)
  179. {
  180. HP_Destroy_TcpServer(pServer);
  181. }
  182. };
  183. // ITcpAgent 对象创建器
  184. struct TcpAgent_Creator
  185. {
  186. static ITcpAgent* Create(ITcpAgentListener* pListener)
  187. {
  188. return HP_Create_TcpAgent(pListener);
  189. }
  190. static void Destroy(ITcpAgent* pAgent)
  191. {
  192. HP_Destroy_TcpAgent(pAgent);
  193. }
  194. };
  195. // ITcpClient 对象创建器
  196. struct TcpClient_Creator
  197. {
  198. static ITcpClient* Create(ITcpClientListener* pListener)
  199. {
  200. return HP_Create_TcpClient(pListener);
  201. }
  202. static void Destroy(ITcpClient* pClient)
  203. {
  204. HP_Destroy_TcpClient(pClient);
  205. }
  206. };
  207. // ITcpPullServer 对象创建器
  208. struct TcpPullServer_Creator
  209. {
  210. static ITcpPullServer* Create(ITcpServerListener* pListener)
  211. {
  212. return HP_Create_TcpPullServer(pListener);
  213. }
  214. static void Destroy(ITcpPullServer* pServer)
  215. {
  216. HP_Destroy_TcpPullServer(pServer);
  217. }
  218. };
  219. // ITcpPullAgent 对象创建器
  220. struct TcpPullAgent_Creator
  221. {
  222. static ITcpPullAgent* Create(ITcpAgentListener* pListener)
  223. {
  224. return HP_Create_TcpPullAgent(pListener);
  225. }
  226. static void Destroy(ITcpPullAgent* pAgent)
  227. {
  228. HP_Destroy_TcpPullAgent(pAgent);
  229. }
  230. };
  231. // ITcpPullClient 对象创建器
  232. struct TcpPullClient_Creator
  233. {
  234. static ITcpPullClient* Create(ITcpClientListener* pListener)
  235. {
  236. return HP_Create_TcpPullClient(pListener);
  237. }
  238. static void Destroy(ITcpPullClient* pClient)
  239. {
  240. HP_Destroy_TcpPullClient(pClient);
  241. }
  242. };
  243. // ITcpPackServer 对象创建器
  244. struct TcpPackServer_Creator
  245. {
  246. static ITcpPackServer* Create(ITcpServerListener* pListener)
  247. {
  248. return HP_Create_TcpPackServer(pListener);
  249. }
  250. static void Destroy(ITcpPackServer* pServer)
  251. {
  252. HP_Destroy_TcpPackServer(pServer);
  253. }
  254. };
  255. // ITcpPackAgent 对象创建器
  256. struct TcpPackAgent_Creator
  257. {
  258. static ITcpPackAgent* Create(ITcpAgentListener* pListener)
  259. {
  260. return HP_Create_TcpPackAgent(pListener);
  261. }
  262. static void Destroy(ITcpPackAgent* pAgent)
  263. {
  264. HP_Destroy_TcpPackAgent(pAgent);
  265. }
  266. };
  267. // ITcpPackClient 对象创建器
  268. struct TcpPackClient_Creator
  269. {
  270. static ITcpPackClient* Create(ITcpClientListener* pListener)
  271. {
  272. return HP_Create_TcpPackClient(pListener);
  273. }
  274. static void Destroy(ITcpPackClient* pClient)
  275. {
  276. HP_Destroy_TcpPackClient(pClient);
  277. }
  278. };
  279. // IUdpServer 对象创建器
  280. struct UdpServer_Creator
  281. {
  282. static IUdpServer* Create(IUdpServerListener* pListener)
  283. {
  284. return HP_Create_UdpServer(pListener);
  285. }
  286. static void Destroy(IUdpServer* pServer)
  287. {
  288. HP_Destroy_UdpServer(pServer);
  289. }
  290. };
  291. // IUdpClient 对象创建器
  292. struct UdpClient_Creator
  293. {
  294. static IUdpClient* Create(IUdpClientListener* pListener)
  295. {
  296. return HP_Create_UdpClient(pListener);
  297. }
  298. static void Destroy(IUdpClient* pClient)
  299. {
  300. HP_Destroy_UdpClient(pClient);
  301. }
  302. };
  303. // IUdpClient 对象创建器
  304. struct UdpCast_Creator
  305. {
  306. static IUdpCast* Create(IUdpCastListener* pListener)
  307. {
  308. return HP_Create_UdpCast(pListener);
  309. }
  310. static void Destroy(IUdpCast* pCast)
  311. {
  312. HP_Destroy_UdpCast(pCast);
  313. }
  314. };
  315. // ITcpServer 对象智能指针
  316. typedef CHPSocketPtr<ITcpServer, ITcpServerListener, TcpServer_Creator> CTcpServerPtr;
  317. // ITcpAgent 对象智能指针
  318. typedef CHPSocketPtr<ITcpAgent, ITcpAgentListener, TcpAgent_Creator> CTcpAgentPtr;
  319. // ITcpClient 对象智能指针
  320. typedef CHPSocketPtr<ITcpClient, ITcpClientListener, TcpClient_Creator> CTcpClientPtr;
  321. // ITcpPullServer 对象智能指针
  322. typedef CHPSocketPtr<ITcpPullServer, ITcpServerListener, TcpPullServer_Creator> CTcpPullServerPtr;
  323. // ITcpPullAgent 对象智能指针
  324. typedef CHPSocketPtr<ITcpPullAgent, ITcpAgentListener, TcpPullAgent_Creator> CTcpPullAgentPtr;
  325. // ITcpPullClient 对象智能指针
  326. typedef CHPSocketPtr<ITcpPullClient, ITcpClientListener, TcpPullClient_Creator> CTcpPullClientPtr;
  327. // ITcpPackServer 对象智能指针
  328. typedef CHPSocketPtr<ITcpPackServer, ITcpServerListener, TcpPackServer_Creator> CTcpPackServerPtr;
  329. // ITcpPackAgent 对象智能指针
  330. typedef CHPSocketPtr<ITcpPackAgent, ITcpAgentListener, TcpPackAgent_Creator> CTcpPackAgentPtr;
  331. // ITcpPackClient 对象智能指针
  332. typedef CHPSocketPtr<ITcpPackClient, ITcpClientListener, TcpPackClient_Creator> CTcpPackClientPtr;
  333. // IUdpServer 对象智能指针
  334. typedef CHPSocketPtr<IUdpServer, IUdpServerListener, UdpServer_Creator> CUdpServerPtr;
  335. // IUdpClient 对象智能指针
  336. typedef CHPSocketPtr<IUdpClient, IUdpClientListener, UdpClient_Creator> CUdpClientPtr;
  337. // IUdpCast 对象智能指针
  338. typedef CHPSocketPtr<IUdpCast, IUdpCastListener, UdpCast_Creator> CUdpCastPtr;
  339. /*****************************************************************************************************************************************************/
  340. /******************************************************************** HTTP Exports *******************************************************************/
  341. /*****************************************************************************************************************************************************/
  342. // 创建 IHttpServer 对象
  343. HPSOCKET_API IHttpServer* HP_Create_HttpServer(IHttpServerListener* pListener);
  344. // 创建 IHttpAgent 对象
  345. HPSOCKET_API IHttpAgent* HP_Create_HttpAgent(IHttpAgentListener* pListener);
  346. // 创建 IHttpClient 对象
  347. HPSOCKET_API IHttpClient* HP_Create_HttpClient(IHttpClientListener* pListener);
  348. // 销毁 IHttpServer 对象
  349. HPSOCKET_API void HP_Destroy_HttpServer(IHttpServer* pServer);
  350. // 销毁 IHttpAgent 对象
  351. HPSOCKET_API void HP_Destroy_HttpAgent(IHttpAgent* pAgent);
  352. // 销毁 IHttpClient 对象
  353. HPSOCKET_API void HP_Destroy_HttpClient(IHttpClient* pClient);
  354. // IHttpServer 对象创建器
  355. struct HttpServer_Creator
  356. {
  357. static IHttpServer* Create(IHttpServerListener* pListener)
  358. {
  359. return HP_Create_HttpServer(pListener);
  360. }
  361. static void Destroy(IHttpServer* pServer)
  362. {
  363. HP_Destroy_HttpServer(pServer);
  364. }
  365. };
  366. // IHttpAgent 对象创建器
  367. struct HttpAgent_Creator
  368. {
  369. static IHttpAgent* Create(IHttpAgentListener* pListener)
  370. {
  371. return HP_Create_HttpAgent(pListener);
  372. }
  373. static void Destroy(IHttpAgent* pAgent)
  374. {
  375. HP_Destroy_HttpAgent(pAgent);
  376. }
  377. };
  378. // IHttpClient 对象创建器
  379. struct HttpClient_Creator
  380. {
  381. static IHttpClient* Create(IHttpClientListener* pListener)
  382. {
  383. return HP_Create_HttpClient(pListener);
  384. }
  385. static void Destroy(IHttpClient* pClient)
  386. {
  387. HP_Destroy_HttpClient(pClient);
  388. }
  389. };
  390. // IHttpServer 对象智能指针
  391. typedef CHPSocketPtr<IHttpServer, IHttpServerListener, HttpServer_Creator> CHttpServerPtr;
  392. // IHttpAgent 对象智能指针
  393. typedef CHPSocketPtr<IHttpAgent, IHttpAgentListener, HttpAgent_Creator> CHttpAgentPtr;
  394. // IHttpClient 对象智能指针
  395. typedef CHPSocketPtr<IHttpClient, IHttpClientListener, HttpClient_Creator> CHttpClientPtr;
  396. /*****************************************************************************************************************************************************/
  397. /*************************************************************** Global Function Exports *************************************************************/
  398. /*****************************************************************************************************************************************************/
  399. // 获取错误描述文本
  400. HPSOCKET_API LPCTSTR HP_GetSocketErrorDesc(EnSocketError enCode);
  401. // 调用系统的 GetLastError() 方法获取系统错误代码
  402. HPSOCKET_API DWORD SYS_GetLastError ();
  403. // 调用系统的 WSAGetLastError() 方法获取系统错误代码
  404. HPSOCKET_API int SYS_WSAGetLastError();
  405. // 调用系统的 setsockopt()
  406. HPSOCKET_API int SYS_SetSocketOption(SOCKET sock, int level, int name, LPVOID val, int len);
  407. // 调用系统的 getsockopt()
  408. HPSOCKET_API int SYS_GetSocketOption(SOCKET sock, int level, int name, LPVOID val, int* len);
  409. // 调用系统的 ioctlsocket()
  410. HPSOCKET_API int SYS_IoctlSocket(SOCKET sock, long cmd, u_long* arg);
  411. // 调用系统的 WSAIoctl()
  412. HPSOCKET_API int SYS_WSAIoctl(SOCKET sock, DWORD dwIoControlCode, LPVOID lpvInBuffer, DWORD cbInBuffer, LPVOID lpvOutBuffer, DWORD cbOutBuffer, LPDWORD lpcbBytesReturned);
  413. // 设置 socket 选项:IPPROTO_TCP -> TCP_NODELAY
  414. HPSOCKET_API int SYS_SSO_NoDelay(SOCKET sock, BOOL bNoDelay);
  415. // 设置 socket 选项:SOL_SOCKET -> SO_DONTLINGER
  416. HPSOCKET_API int SYS_SSO_DontLinger(SOCKET sock, BOOL bDont);
  417. // 设置 socket 选项:SOL_SOCKET -> SO_LINGER
  418. HPSOCKET_API int SYS_SSO_Linger(SOCKET sock, USHORT l_onoff, USHORT l_linger);
  419. // 设置 socket 选项:SOL_SOCKET -> SO_RCVBUF
  420. HPSOCKET_API int SYS_SSO_RecvBuffSize(SOCKET sock, int size);
  421. // 设置 socket 选项:SOL_SOCKET -> SO_SNDBUF
  422. HPSOCKET_API int SYS_SSO_SendBuffSize(SOCKET sock, int size);
  423. // 设置 socket 选项:SOL_SOCKET -> SO_REUSEADDR
  424. HPSOCKET_API int SYS_SSO_ReuseAddress(SOCKET sock, BOOL bReuse);
  425. // 获取 SOCKET 本地地址信息
  426. HPSOCKET_API BOOL SYS_GetSocketLocalAddress(SOCKET socket, TCHAR lpszAddress[], int* piAddressLen, USHORT* pusPort);
  427. // 获取 SOCKET 远程地址信息
  428. HPSOCKET_API BOOL SYS_GetSocketRemoteAddress(SOCKET socket, TCHAR lpszAddress[], int* piAddressLen, USHORT* pusPort);