TcpServer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 4.2.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. #pragma once
  25. #include "SocketHelper.h"
  26. #include "../../Common/Src/Event.h"
  27. #include "../../Common/Src/STLHelper.h"
  28. #include "../../Common/Src/RingBuffer.h"
  29. #include "../../Common/Src/PrivateHeap.h"
  30. class CTcpServer : public ITcpServer
  31. {
  32. public:
  33. virtual BOOL Start (LPCTSTR lpszBindAddress, USHORT usPort);
  34. virtual BOOL Stop ();
  35. virtual BOOL Send (CONNID dwConnID, const BYTE* pBuffer, int iLength, int iOffset = 0);
  36. virtual BOOL SendSmallFile (CONNID dwConnID, LPCTSTR lpszFileName, const LPWSABUF pHead = nullptr, const LPWSABUF pTail = nullptr);
  37. virtual BOOL SendPackets (CONNID dwConnID, const WSABUF pBuffers[], int iCount) {return DoSendPackets(dwConnID, pBuffers, 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 IsSecure () {return FALSE;}
  55. virtual BOOL SetConnectionExtra(CONNID dwConnID, PVOID pExtra);
  56. virtual BOOL GetConnectionExtra(CONNID dwConnID, PVOID* ppExtra);
  57. virtual void SetSendPolicy (EnSendPolicy enSendPolicy) {m_enSendPolicy = enSendPolicy;}
  58. virtual void SetMaxConnectionCount (DWORD dwMaxConnectionCount) {m_dwMaxConnectionCount = dwMaxConnectionCount;}
  59. virtual void SetWorkerThreadCount (DWORD dwWorkerThreadCount) {m_dwWorkerThreadCount = dwWorkerThreadCount;}
  60. virtual void SetSocketListenQueue (DWORD dwSocketListenQueue) {m_dwSocketListenQueue = dwSocketListenQueue;}
  61. virtual void SetAcceptSocketCount (DWORD dwAcceptSocketCount) {m_dwAcceptSocketCount = dwAcceptSocketCount;}
  62. virtual void SetSocketBufferSize (DWORD dwSocketBufferSize) {m_dwSocketBufferSize = dwSocketBufferSize;}
  63. virtual void SetFreeSocketObjLockTime (DWORD dwFreeSocketObjLockTime) {m_dwFreeSocketObjLockTime = dwFreeSocketObjLockTime;}
  64. virtual void SetFreeSocketObjPool (DWORD dwFreeSocketObjPool) {m_dwFreeSocketObjPool = dwFreeSocketObjPool;}
  65. virtual void SetFreeBufferObjPool (DWORD dwFreeBufferObjPool) {m_dwFreeBufferObjPool = dwFreeBufferObjPool;}
  66. virtual void SetFreeSocketObjHold (DWORD dwFreeSocketObjHold) {m_dwFreeSocketObjHold = dwFreeSocketObjHold;}
  67. virtual void SetFreeBufferObjHold (DWORD dwFreeBufferObjHold) {m_dwFreeBufferObjHold = dwFreeBufferObjHold;}
  68. virtual void SetKeepAliveTime (DWORD dwKeepAliveTime) {m_dwKeepAliveTime = dwKeepAliveTime;}
  69. virtual void SetKeepAliveInterval (DWORD dwKeepAliveInterval) {m_dwKeepAliveInterval = dwKeepAliveInterval;}
  70. virtual void SetMarkSilence (BOOL bMarkSilence) {m_bMarkSilence = bMarkSilence;}
  71. virtual EnSendPolicy GetSendPolicy () {return m_enSendPolicy;}
  72. virtual DWORD GetMaxConnectionCount () {return m_dwMaxConnectionCount;}
  73. virtual DWORD GetWorkerThreadCount () {return m_dwWorkerThreadCount;}
  74. virtual DWORD GetSocketListenQueue () {return m_dwSocketListenQueue;}
  75. virtual DWORD GetAcceptSocketCount () {return m_dwAcceptSocketCount;}
  76. virtual DWORD GetSocketBufferSize () {return m_dwSocketBufferSize;}
  77. virtual DWORD GetFreeSocketObjLockTime () {return m_dwFreeSocketObjLockTime;}
  78. virtual DWORD GetFreeSocketObjPool () {return m_dwFreeSocketObjPool;}
  79. virtual DWORD GetFreeBufferObjPool () {return m_dwFreeBufferObjPool;}
  80. virtual DWORD GetFreeSocketObjHold () {return m_dwFreeSocketObjHold;}
  81. virtual DWORD GetFreeBufferObjHold () {return m_dwFreeBufferObjHold;}
  82. virtual DWORD GetKeepAliveTime () {return m_dwKeepAliveTime;}
  83. virtual DWORD GetKeepAliveInterval () {return m_dwKeepAliveInterval;}
  84. virtual BOOL IsMarkSilence () {return m_bMarkSilence;}
  85. virtual void CloseClient(CONNID dwConnID) {
  86. int result = ETIMEDOUT;
  87. ::PostIocpClose(m_hCompletePort, dwConnID, result);
  88. }
  89. protected:
  90. virtual EnHandleResult FirePrepareListen(SOCKET soListen)
  91. {return DoFirePrepareListen(soListen);}
  92. virtual EnHandleResult FireAccept(TSocketObj* pSocketObj)
  93. {
  94. EnHandleResult rs = DoFireAccept(pSocketObj);
  95. if(rs != HR_ERROR) rs = FireHandShake(pSocketObj);
  96. return rs;
  97. }
  98. virtual EnHandleResult FireHandShake(TSocketObj* pSocketObj)
  99. {return DoFireHandShake(pSocketObj);}
  100. virtual EnHandleResult FireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  101. {return DoFireReceive(pSocketObj, pData, iLength);}
  102. virtual EnHandleResult FireReceive(TSocketObj* pSocketObj, int iLength)
  103. {return DoFireReceive(pSocketObj, iLength);}
  104. virtual EnHandleResult FireSend(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  105. {return DoFireSend(pSocketObj, pData, iLength);}
  106. virtual EnHandleResult FireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  107. {return DoFireClose(pSocketObj, enOperation, iErrorCode);}
  108. virtual EnHandleResult FireShutdown()
  109. {return DoFireShutdown();}
  110. virtual EnHandleResult DoFirePrepareListen(SOCKET soListen)
  111. {return m_pListener->OnPrepareListen(this, soListen);}
  112. virtual EnHandleResult DoFireAccept(TSocketObj* pSocketObj)
  113. {return m_pListener->OnAccept(this, pSocketObj->connID, pSocketObj->socket);}
  114. virtual EnHandleResult DoFireHandShake(TSocketObj* pSocketObj)
  115. {return m_pListener->OnHandShake(this, pSocketObj->connID);}
  116. virtual EnHandleResult DoFireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  117. {return m_pListener->OnReceive(this, pSocketObj->connID, pData, iLength);}
  118. virtual EnHandleResult DoFireReceive(TSocketObj* pSocketObj, int iLength)
  119. {return m_pListener->OnReceive(this, pSocketObj->connID, iLength);}
  120. virtual EnHandleResult DoFireSend(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  121. {return m_pListener->OnSend(this, pSocketObj->connID, pData, iLength);}
  122. virtual EnHandleResult DoFireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  123. {return m_pListener->OnClose(this, pSocketObj->connID, enOperation, iErrorCode);}
  124. virtual EnHandleResult DoFireShutdown()
  125. {return m_pListener->OnShutdown(this);}
  126. void SetLastError(EnSocketError code, LPCSTR func, int ec);
  127. virtual BOOL CheckParams();
  128. virtual void PrepareStart();
  129. virtual void Reset();
  130. virtual void OnWorkerThreadEnd(DWORD dwThreadID) {}
  131. BOOL DoSendPackets(CONNID dwConnID, const WSABUF pBuffers[], int iCount);
  132. BOOL DoSendPackets(TSocketObj* pSocketObj, const WSABUF pBuffers[], int iCount);
  133. TSocketObj* FindSocketObj(CONNID dwConnID);
  134. private:
  135. EnHandleResult TriggerFireAccept(TSocketObj* pSocketObj);
  136. EnHandleResult TriggerFireReceive(TSocketObj* pSocketObj, TBufferObj* pBufferObj);
  137. EnHandleResult TriggerFireSend(TSocketObj* pSocketObj, TBufferObj* pBufferObj);
  138. EnHandleResult TriggerFireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode);
  139. protected:
  140. BOOL SetConnectionExtra(TSocketObj* pSocketObj, PVOID pExtra);
  141. BOOL GetConnectionExtra(TSocketObj* pSocketObj, PVOID* ppExtra);
  142. BOOL SetConnectionReserved(CONNID dwConnID, PVOID pReserved);
  143. BOOL GetConnectionReserved(CONNID dwConnID, PVOID* ppReserved);
  144. BOOL SetConnectionReserved(TSocketObj* pSocketObj, PVOID pReserved);
  145. BOOL GetConnectionReserved(TSocketObj* pSocketObj, PVOID* ppReserved);
  146. BOOL SetConnectionReserved2(CONNID dwConnID, PVOID pReserved2);
  147. BOOL GetConnectionReserved2(CONNID dwConnID, PVOID* ppReserved2);
  148. BOOL SetConnectionReserved2(TSocketObj* pSocketObj, PVOID pReserved2);
  149. BOOL GetConnectionReserved2(TSocketObj* pSocketObj, PVOID* ppReserved2);
  150. private:
  151. BOOL CheckStarting();
  152. BOOL CheckStoping();
  153. BOOL CreateListenSocket(LPCTSTR lpszBindAddress, USHORT usPort);
  154. BOOL CreateCompletePort();
  155. BOOL CreateWorkerThreads();
  156. BOOL StartAccept();
  157. void CloseListenSocket();
  158. void WaitForAcceptSocketClose();
  159. void DisconnectClientSocket();
  160. void WaitForClientSocketClose();
  161. void ReleaseClientSocket();
  162. void ReleaseFreeSocket();
  163. void ReleaseFreeBuffer();
  164. void WaitForWorkerThreadEnd();
  165. void CloseCompletePort();
  166. TBufferObj* GetFreeBufferObj(int iLen = -1);
  167. TSocketObj* GetFreeSocketObj(CONNID dwConnID, SOCKET soClient);
  168. void AddFreeBufferObj(TBufferObj* pBufferObj);
  169. void AddFreeSocketObj(TSocketObj* pSocketObj, EnSocketCloseFlag enFlag = SCF_NONE, EnSocketOperation enOperation = SO_UNKNOWN, int iErrorCode = 0);
  170. TSocketObj* CreateSocketObj();
  171. void DeleteSocketObj(TSocketObj* pSocketObj);
  172. BOOL InvalidSocketObj(TSocketObj* pSocketObj);
  173. void ReleaseGCSocketObj(BOOL bForce = FALSE);
  174. void AddClientSocketObj(CONNID dwConnID, TSocketObj* pSocketObj);
  175. void CloseClientSocketObj(TSocketObj* pSocketObj, EnSocketCloseFlag enFlag = SCF_NONE, EnSocketOperation enOperation = SO_UNKNOWN, int iErrorCode = 0, int iShutdownFlag = SD_SEND);
  176. private:
  177. static UINT WINAPI WorkerThreadProc(LPVOID pv);
  178. EnIocpAction CheckIocpCommand(OVERLAPPED* pOverlapped, DWORD dwBytes, ULONG_PTR ulCompKey);
  179. void ForceDisconnect(CONNID dwConnID);
  180. void HandleIo (CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj, DWORD dwBytes, DWORD dwErrorCode);
  181. void HandleError (CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj, DWORD dwErrorCode);
  182. void HandleAccept (SOCKET soListen, TBufferObj* pBufferObj);
  183. void HandleSend (CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj);
  184. void HandleReceive (CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj);
  185. int SendInternal(TSocketObj* pSocketObj, const WSABUF pBuffers[], int iCount);
  186. int SendPack (TSocketObj* pSocketObj, const BYTE* pBuffer, int iLength);
  187. int SendSafe (TSocketObj* pSocketObj, const BYTE* pBuffer, int iLength);
  188. int SendDirect (TSocketObj* pSocketObj, const BYTE* pBuffer, int iLength);
  189. int CatAndPost (TSocketObj* pSocketObj, const BYTE* pBuffer, int iLength, BOOL isPostSend);
  190. BOOL DoAccept ();
  191. int DoReceive (CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj);
  192. int DoSend (CONNID dwConnID);
  193. int DoSend (TSocketObj* pSocketObj);
  194. int DoSendPack (TSocketObj* pSocketObj);
  195. int DoSendSafe (TSocketObj* pSocketObj);
  196. int SendItem (TSocketObj* pSocketObj);
  197. void CheckError (TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode);
  198. public:
  199. CTcpServer(ITcpServerListener* pListener)
  200. : m_pListener (pListener)
  201. , m_hCompletePort (nullptr)
  202. , m_soListen (INVALID_SOCKET)
  203. , m_iRemainAcceptSockets (0)
  204. , m_pfnAcceptEx (nullptr)
  205. , m_pfnGetAcceptExSockaddrs (nullptr)
  206. , m_pfnDisconnectEx (nullptr)
  207. , m_enLastError (SE_OK)
  208. , m_enState (SS_STOPPED)
  209. , m_enSendPolicy (SP_PACK)
  210. , m_dwMaxConnectionCount (DEFAULT_MAX_CONNECTION_COUNT)
  211. , m_dwWorkerThreadCount (DEFAULT_WORKER_THREAD_COUNT)
  212. , m_dwSocketListenQueue (DEFAULT_TCP_SERVER_SOCKET_LISTEN_QUEUE)
  213. , m_dwAcceptSocketCount (DEFAULT_TCP_SERVER_ACCEPT_SOCKET_COUNT)
  214. , m_dwSocketBufferSize (DEFAULT_TCP_SOCKET_BUFFER_SIZE)
  215. , m_dwFreeSocketObjLockTime (DEFAULT_FREE_SOCKETOBJ_LOCK_TIME)
  216. , m_dwFreeSocketObjPool (DEFAULT_FREE_SOCKETOBJ_POOL)
  217. , m_dwFreeBufferObjPool (DEFAULT_FREE_BUFFEROBJ_POOL)
  218. , m_dwFreeSocketObjHold (DEFAULT_FREE_SOCKETOBJ_HOLD)
  219. , m_dwFreeBufferObjHold (DEFAULT_FREE_BUFFEROBJ_HOLD)
  220. , m_dwKeepAliveTime (DEFALUT_TCP_KEEPALIVE_TIME)
  221. , m_dwKeepAliveInterval (DEFALUT_TCP_KEEPALIVE_INTERVAL)
  222. , m_bMarkSilence (TRUE)
  223. {
  224. ASSERT(m_wsSocket.IsValid());
  225. ASSERT(m_pListener);
  226. }
  227. virtual ~CTcpServer()
  228. {
  229. Stop();
  230. }
  231. private:
  232. EnSendPolicy m_enSendPolicy;
  233. DWORD m_dwMaxConnectionCount;
  234. DWORD m_dwWorkerThreadCount;
  235. DWORD m_dwSocketListenQueue;
  236. DWORD m_dwAcceptSocketCount;
  237. DWORD m_dwSocketBufferSize;
  238. DWORD m_dwFreeSocketObjLockTime;
  239. DWORD m_dwFreeSocketObjPool;
  240. DWORD m_dwFreeBufferObjPool;
  241. DWORD m_dwFreeSocketObjHold;
  242. DWORD m_dwFreeBufferObjHold;
  243. DWORD m_dwKeepAliveTime;
  244. DWORD m_dwKeepAliveInterval;
  245. BOOL m_bMarkSilence;
  246. private:
  247. CInitSocket m_wsSocket;
  248. LPFN_ACCEPTEX m_pfnAcceptEx;
  249. LPFN_GETACCEPTEXSOCKADDRS m_pfnGetAcceptExSockaddrs;
  250. LPFN_DISCONNECTEX m_pfnDisconnectEx;
  251. private:
  252. ITcpServerListener* m_pListener;
  253. SOCKET m_soListen;
  254. HANDLE m_hCompletePort;
  255. EnServiceState m_enState;
  256. EnSocketError m_enLastError;
  257. vector<HANDLE> m_vtWorkerThreads;
  258. CPrivateHeap m_phSocket;
  259. CBufferObjPool m_bfObjPool;
  260. CSpinGuard m_csState;
  261. TSocketObjPtrPool m_bfActiveSockets;
  262. TSocketObjPtrList m_lsFreeSocket;
  263. TSocketObjPtrQueue m_lsGCSocket;
  264. volatile long m_iRemainAcceptSockets;
  265. };