开源的socket服务端客户端,支持C# C++

UdpServer.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 4.1.3
  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. #pragma once
  25. #include "SocketHelper.h"
  26. #include "../../Common/Src/Event.h"
  27. #include "../../Common/Src/RWLock.h"
  28. #include "../../Common/Src/STLHelper.h"
  29. #include "../../Common/Src/RingBuffer.h"
  30. #include "../../Common/Src/PrivateHeap.h"
  31. class CUdpServer : public IUdpServer
  32. {
  33. public:
  34. virtual BOOL Start (LPCTSTR pszBindAddress, USHORT usPort);
  35. virtual BOOL Stop ();
  36. virtual BOOL Send (CONNID dwConnID, const BYTE* pBuffer, int iLength, int iOffset = 0);
  37. virtual BOOL SendPackets(CONNID dwConnID, const WSABUF pBuffers[], int iCount);
  38. virtual BOOL HasStarted () {return m_enState == SS_STARTED || m_enState == SS_STARTING;}
  39. virtual EnServiceState GetState () {return m_enState;}
  40. virtual BOOL Disconnect (CONNID dwConnID, BOOL bForce = TRUE);
  41. virtual BOOL DisconnectLongConnections (DWORD dwPeriod, BOOL bForce = TRUE);
  42. virtual BOOL DisconnectSilenceConnections(DWORD dwPeriod, BOOL bForce = TRUE);
  43. virtual BOOL GetListenAddress (TCHAR lpszAddress[], int& iAddressLen, USHORT& usPort);
  44. virtual BOOL GetLocalAddress (CONNID dwConnID, TCHAR lpszAddress[], int& iAddressLen, USHORT& usPort);
  45. virtual BOOL GetRemoteAddress (CONNID dwConnID, TCHAR lpszAddress[], int& iAddressLen, USHORT& usPort);
  46. virtual BOOL GetPendingDataLength (CONNID dwConnID, int& iPending);
  47. virtual DWORD GetConnectionCount ();
  48. virtual BOOL GetAllConnectionIDs (CONNID pIDs[], DWORD& dwCount);
  49. virtual BOOL GetConnectPeriod (CONNID dwConnID, DWORD& dwPeriod);
  50. virtual BOOL GetSilencePeriod (CONNID dwConnID, DWORD& dwPeriod);
  51. virtual EnSocketError GetLastError () {return m_enLastError;}
  52. virtual LPCTSTR GetLastErrorDesc () {return ::GetSocketErrorDesc(m_enLastError);}
  53. public:
  54. virtual BOOL SetConnectionExtra(CONNID dwConnID, PVOID pExtra);
  55. virtual BOOL GetConnectionExtra(CONNID dwConnID, PVOID* ppExtra);
  56. virtual void SetSendPolicy (EnSendPolicy enSendPolicy) {m_enSendPolicy = enSendPolicy;}
  57. virtual void SetMaxConnectionCount (DWORD dwMaxConnectionCount) {m_dwMaxConnectionCount = dwMaxConnectionCount;}
  58. virtual void SetWorkerThreadCount (DWORD dwWorkerThreadCount) {m_dwWorkerThreadCount = dwWorkerThreadCount;}
  59. virtual void SetFreeSocketObjLockTime (DWORD dwFreeSocketObjLockTime) {m_dwFreeSocketObjLockTime = dwFreeSocketObjLockTime;}
  60. virtual void SetFreeSocketObjPool (DWORD dwFreeSocketObjPool) {m_dwFreeSocketObjPool = dwFreeSocketObjPool;}
  61. virtual void SetFreeBufferObjPool (DWORD dwFreeBufferObjPool) {m_dwFreeBufferObjPool = dwFreeBufferObjPool;}
  62. virtual void SetFreeSocketObjHold (DWORD dwFreeSocketObjHold) {m_dwFreeSocketObjHold = dwFreeSocketObjHold;}
  63. virtual void SetFreeBufferObjHold (DWORD dwFreeBufferObjHold) {m_dwFreeBufferObjHold = dwFreeBufferObjHold;}
  64. virtual void SetMaxDatagramSize (DWORD dwMaxDatagramSize) {m_dwMaxDatagramSize = dwMaxDatagramSize;}
  65. virtual void SetPostReceiveCount (DWORD dwPostReceiveCount) {m_dwPostReceiveCount = dwPostReceiveCount;}
  66. virtual void SetDetectAttempts (DWORD dwDetectAttempts) {m_dwDetectAttempts = dwDetectAttempts;}
  67. virtual void SetDetectInterval (DWORD dwDetectInterval) {m_dwDetectInterval = dwDetectInterval;}
  68. virtual void SetMarkSilence (BOOL bMarkSilence) {m_bMarkSilence = bMarkSilence;}
  69. virtual EnSendPolicy GetSendPolicy () {return m_enSendPolicy;}
  70. virtual DWORD GetMaxConnectionCount () {return m_dwMaxConnectionCount;}
  71. virtual DWORD GetWorkerThreadCount () {return m_dwWorkerThreadCount;}
  72. virtual DWORD GetFreeSocketObjLockTime () {return m_dwFreeSocketObjLockTime;}
  73. virtual DWORD GetFreeSocketObjPool () {return m_dwFreeSocketObjPool;}
  74. virtual DWORD GetFreeBufferObjPool () {return m_dwFreeBufferObjPool;}
  75. virtual DWORD GetFreeSocketObjHold () {return m_dwFreeSocketObjHold;}
  76. virtual DWORD GetFreeBufferObjHold () {return m_dwFreeBufferObjHold;}
  77. virtual DWORD GetMaxDatagramSize () {return m_dwMaxDatagramSize;}
  78. virtual DWORD GetPostReceiveCount () {return m_dwPostReceiveCount;}
  79. virtual DWORD GetDetectAttempts () {return m_dwDetectAttempts;}
  80. virtual DWORD GetDetectInterval () {return m_dwDetectInterval;}
  81. virtual BOOL IsMarkSilence () {return m_bMarkSilence;}
  82. protected:
  83. virtual EnHandleResult FirePrepareListen(SOCKET soListen)
  84. {return m_pListener->OnPrepareListen(this, soListen);}
  85. virtual EnHandleResult FireAccept(TUdpSocketObj* pSocketObj)
  86. {
  87. EnHandleResult rs = m_pListener->OnAccept(this, pSocketObj->connID, (UINT_PTR)(&pSocketObj->remoteAddr));
  88. if(rs != HR_ERROR) rs = FireHandShake(pSocketObj);
  89. return rs;
  90. }
  91. virtual EnHandleResult FireHandShake(TUdpSocketObj* pSocketObj)
  92. {return m_pListener->OnHandShake(this, pSocketObj->connID);}
  93. virtual EnHandleResult FireReceive(TUdpSocketObj* pSocketObj, const BYTE* pData, int iLength)
  94. {return m_pListener->OnReceive(this, pSocketObj->connID, pData, iLength);}
  95. virtual EnHandleResult FireReceive(TUdpSocketObj* pSocketObj, int iLength)
  96. {return m_pListener->OnReceive(this, pSocketObj->connID, iLength);}
  97. virtual EnHandleResult FireSend(TUdpSocketObj* pSocketObj, const BYTE* pData, int iLength)
  98. {return m_pListener->OnSend(this, pSocketObj->connID, pData, iLength);}
  99. virtual EnHandleResult FireClose(TUdpSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  100. {return m_pListener->OnClose(this, pSocketObj->connID, enOperation, iErrorCode);}
  101. virtual EnHandleResult FireShutdown()
  102. {return m_pListener->OnShutdown(this);}
  103. void SetLastError(EnSocketError code, LPCSTR func, int ec);
  104. virtual BOOL CheckParams();
  105. virtual void PrepareStart();
  106. virtual void Reset();
  107. virtual void OnWorkerThreadEnd(DWORD dwThreadID) {}
  108. private:
  109. EnHandleResult TriggerFireAccept(TUdpSocketObj* pSocketObj);
  110. EnHandleResult TriggerFireReceive(TUdpSocketObj* pSocketObj, TUdpBufferObj* pBufferObj);
  111. EnHandleResult TriggerFireSend(TUdpSocketObj* pSocketObj, TUdpBufferObj* pBufferObj);
  112. EnHandleResult TriggerFireClose(TUdpSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode);
  113. protected:
  114. BOOL SetConnectionExtra(TUdpSocketObj* pSocketObj, PVOID pExtra);
  115. BOOL GetConnectionExtra(TUdpSocketObj* pSocketObj, PVOID* ppExtra);
  116. BOOL SetConnectionReserved(CONNID dwConnID, PVOID pReserved);
  117. BOOL GetConnectionReserved(CONNID dwConnID, PVOID* ppReserved);
  118. BOOL SetConnectionReserved(TUdpSocketObj* pSocketObj, PVOID pReserved);
  119. BOOL GetConnectionReserved(TUdpSocketObj* pSocketObj, PVOID* ppReserved);
  120. BOOL SetConnectionReserved2(CONNID dwConnID, PVOID pReserved2);
  121. BOOL GetConnectionReserved2(CONNID dwConnID, PVOID* ppReserved2);
  122. BOOL SetConnectionReserved2(TUdpSocketObj* pSocketObj, PVOID pReserved2);
  123. BOOL GetConnectionReserved2(TUdpSocketObj* pSocketObj, PVOID* ppReserved2);
  124. private:
  125. static UINT WINAPI WorkerThreadProc(LPVOID pv);
  126. static UINT WINAPI DetecotrThreadProc(LPVOID pv);
  127. private:
  128. BOOL CheckStarting();
  129. BOOL CheckStoping();
  130. BOOL CreateListenSocket(LPCTSTR pszBindAddress, USHORT usPort);
  131. BOOL CreateCompletePort();
  132. BOOL CreateWorkerThreads();
  133. BOOL CreateDetectorThread();
  134. BOOL StartAccept();
  135. void CloseListenSocket();
  136. void WaitForPostReceiveRelease();
  137. void DisconnectClientSocket();
  138. void WaitForClientSocketClose();
  139. void ReleaseClientSocket();
  140. void ReleaseFreeSocket();
  141. void ReleaseFreeBuffer();
  142. void WaitForWorkerThreadEnd();
  143. void WaitForDetectorThreadEnd();
  144. void CloseCompletePort();
  145. TUdpBufferObj* GetFreeBufferObj(int iLen = -1);
  146. TUdpSocketObj* GetFreeSocketObj(CONNID dwConnID);
  147. void AddFreeBufferObj(TUdpBufferObj* pBufferObj);
  148. void AddFreeSocketObj(CONNID dwConnID, EnSocketCloseFlag enFlag = SCF_NONE, EnSocketOperation enOperation = SO_UNKNOWN, int iErrorCode = 0);
  149. void AddFreeSocketObj(TUdpSocketObj* pSocketObj, EnSocketCloseFlag enFlag = SCF_NONE, EnSocketOperation enOperation = SO_UNKNOWN, int iErrorCode = 0);
  150. TUdpSocketObj* CreateSocketObj();
  151. void DeleteSocketObj(TUdpSocketObj* pSocketObj);
  152. BOOL InvalidSocketObj(TUdpSocketObj* pSocketObj);
  153. void ReleaseGCSocketObj(BOOL bForce = FALSE);
  154. void AddClientSocketObj(CONNID dwConnID, TUdpSocketObj* pSocketObj);
  155. void CloseClientSocketObj(TUdpSocketObj* pSocketObj, EnSocketCloseFlag enFlag = SCF_NONE, EnSocketOperation enOperation = SO_UNKNOWN, int iErrorCode = 0);
  156. TUdpSocketObj* FindSocketObj(CONNID dwConnID);
  157. CONNID FindConnectionID(SOCKADDR_IN* pAddr);
  158. private:
  159. EnIocpAction CheckIocpCommand(OVERLAPPED* pOverlapped, DWORD dwBytes, ULONG_PTR ulCompKey);
  160. void ForceDisconnect(CONNID dwConnID);
  161. void HandleIo (CONNID dwConnID, TUdpBufferObj* pBufferObj, DWORD dwBytes, DWORD dwErrorCode);
  162. void HandleError (CONNID dwConnID, TUdpBufferObj* pBufferObj, DWORD dwErrorCode);
  163. void HandleZeroBytes(CONNID dwConnID, TUdpBufferObj* pBufferObj);
  164. CONNID HandleAccept (TUdpBufferObj* pBufferObj);
  165. void HandleSend (CONNID dwConnID, TUdpBufferObj* pBufferObj);
  166. void HandleReceive (CONNID dwConnID, TUdpBufferObj* pBufferObj);
  167. int SendInternal(CONNID dwConnID, const BYTE* pBuffer, int iLength);
  168. int SendPack (TUdpSocketObj* pSocketObj, const BYTE* pBuffer, int iLength);
  169. int SendSafe (TUdpSocketObj* pSocketObj, const BYTE* pBuffer, int iLength);
  170. int SendDirect (TUdpSocketObj* pSocketObj, const BYTE* pBuffer, int iLength);
  171. int CatAndPost (TUdpSocketObj* pSocketObj, const BYTE* pBuffer, int iLength, BOOL isPostSend);
  172. BOOL DoAccept ();
  173. int DoReceive (TUdpBufferObj* pBufferObj);
  174. int DoSend (CONNID dwConnID);
  175. int DoSend (TUdpSocketObj* pSocketObj);
  176. int DoSendPack (TUdpSocketObj* pSocketObj);
  177. int DoSendSafe (TUdpSocketObj* pSocketObj);
  178. int SendItem (TUdpSocketObj* pSocketObj);
  179. void DetectConnections ();
  180. BOOL SendDetectPackage (CONNID dwConnID, TUdpSocketObj* pSocketObj);
  181. BOOL NeedDetectorThread () {return m_dwDetectAttempts > 0 && m_dwDetectInterval > 0;}
  182. public:
  183. CUdpServer(IUdpServerListener* pListener)
  184. : m_pListener (pListener)
  185. , m_hCompletePort (nullptr)
  186. , m_soListen (INVALID_SOCKET)
  187. , m_iRemainPostReceives (0)
  188. , m_enLastError (SE_OK)
  189. , m_enState (SS_STOPPED)
  190. , m_hDetector (nullptr)
  191. , m_enSendPolicy (SP_PACK)
  192. , m_dwMaxConnectionCount (DEFAULT_MAX_CONNECTION_COUNT)
  193. , m_dwWorkerThreadCount (DEFAULT_WORKER_THREAD_COUNT)
  194. , m_dwFreeSocketObjLockTime (DEFAULT_FREE_SOCKETOBJ_LOCK_TIME)
  195. , m_dwFreeSocketObjPool (DEFAULT_FREE_SOCKETOBJ_POOL)
  196. , m_dwFreeBufferObjPool (DEFAULT_FREE_BUFFEROBJ_POOL)
  197. , m_dwFreeSocketObjHold (DEFAULT_FREE_SOCKETOBJ_HOLD)
  198. , m_dwFreeBufferObjHold (DEFAULT_FREE_BUFFEROBJ_HOLD)
  199. , m_dwMaxDatagramSize (DEFAULT_UDP_MAX_DATAGRAM_SIZE)
  200. , m_dwPostReceiveCount (DEFAULT_UDP_POST_RECEIVE_COUNT)
  201. , m_dwDetectAttempts (DEFAULT_UDP_DETECT_ATTEMPTS)
  202. , m_dwDetectInterval (DEFAULT_UDP_DETECT_INTERVAL)
  203. , m_bMarkSilence (TRUE)
  204. {
  205. ASSERT(m_wsSocket.IsValid());
  206. ASSERT(m_pListener);
  207. }
  208. virtual ~CUdpServer()
  209. {
  210. Stop();
  211. }
  212. private:
  213. EnSendPolicy m_enSendPolicy;
  214. DWORD m_dwMaxConnectionCount;
  215. DWORD m_dwWorkerThreadCount;
  216. DWORD m_dwFreeSocketObjLockTime;
  217. DWORD m_dwFreeSocketObjPool;
  218. DWORD m_dwFreeBufferObjPool;
  219. DWORD m_dwFreeSocketObjHold;
  220. DWORD m_dwFreeBufferObjHold;
  221. DWORD m_dwMaxDatagramSize;
  222. DWORD m_dwPostReceiveCount;
  223. DWORD m_dwDetectAttempts;
  224. DWORD m_dwDetectInterval;
  225. BOOL m_bMarkSilence;
  226. private:
  227. CInitSocket m_wsSocket;
  228. private:
  229. IUdpServerListener* m_pListener;
  230. SOCKET m_soListen;
  231. HANDLE m_hCompletePort;
  232. EnServiceState m_enState;
  233. EnSocketError m_enLastError;
  234. HANDLE m_hDetector;
  235. vector<HANDLE> m_vtWorkerThreads;
  236. CPrivateHeap m_phSocket;
  237. CUdpBufferObjPool m_bfObjPool;
  238. CSpinGuard m_csState;
  239. CCriSec m_csAccept;
  240. CEvt m_evDetector;
  241. TUdpSocketObjPtrPool m_bfActiveSockets;
  242. CRWLock m_csClientSocket;
  243. TSockAddrMap m_mpClientAddr;
  244. TUdpSocketObjPtrList m_lsFreeSocket;
  245. TUdpSocketObjPtrQueue m_lsGCSocket;
  246. volatile long m_iRemainPostReceives;
  247. };