TcpPackAgent.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "TcpAgent.h"
  25. #include "MiscHelper.h"
  26. template<class T> class CTcpPackAgentT : public IPackSocket, public T
  27. {
  28. using __super = T;
  29. using __super::SetConnectionReserved;
  30. using __super::GetConnectionReserved;
  31. using __super::GetMaxConnectionCount;
  32. using __super::GetSocketBufferSize;
  33. using __super::GetFreeBufferObjPool;
  34. using __super::GetFreeBufferObjHold;
  35. using __super::GetFreeSocketObjLockTime;
  36. using __super::GetFreeSocketObjPool;
  37. using __super::GetFreeSocketObjHold;
  38. using __super::SetLastError;
  39. public:
  40. using __super::Stop;
  41. using __super::Wait;
  42. using __super::GetState;
  43. public:
  44. virtual BOOL SendPackets(CONNID dwConnID, const WSABUF pBuffers[], int iCount)
  45. {
  46. int iNewCount = iCount + 1;
  47. unique_ptr<WSABUF[]> buffers(new WSABUF[iNewCount]);
  48. DWORD dwHeader;
  49. if(!::AddPackHeader(pBuffers, iCount, buffers, m_dwMaxPackSize, m_usHeaderFlag, dwHeader))
  50. return FALSE;
  51. return __super::SendPackets(dwConnID, buffers.get(), iNewCount);
  52. }
  53. protected:
  54. virtual EnHandleResult DoFireConnect(TAgentSocketObj* pSocketObj)
  55. {
  56. EnHandleResult result = __super::DoFireConnect(pSocketObj);
  57. if(result != HR_ERROR)
  58. {
  59. TBuffer* pBuffer = m_bfPool.PickFreeBuffer(pSocketObj->connID);
  60. ENSURE(SetConnectionReserved(pSocketObj, TBufferPackInfo::Construct(pBuffer)));
  61. }
  62. return result;
  63. }
  64. virtual EnHandleResult DoFireHandShake(TAgentSocketObj* pSocketObj)
  65. {
  66. EnHandleResult result = __super::DoFireHandShake(pSocketObj);
  67. if(result == HR_ERROR)
  68. ReleaseConnectionExtra(pSocketObj);
  69. return result;
  70. }
  71. virtual EnHandleResult DoFireReceive(TAgentSocketObj* pSocketObj, const BYTE* pData, int iLength)
  72. {
  73. TBufferPackInfo* pInfo = nullptr;
  74. GetConnectionReserved(pSocketObj, (PVOID*)&pInfo);
  75. ASSERT(pInfo);
  76. TBuffer* pBuffer = (TBuffer*)pInfo->pBuffer;
  77. ASSERT(pBuffer && pBuffer->IsValid());
  78. return ParsePack(this, pInfo, pBuffer, pSocketObj, m_dwMaxPackSize, m_usHeaderFlag, pData, iLength);
  79. }
  80. virtual EnHandleResult DoFireClose(TAgentSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  81. {
  82. EnHandleResult result = __super::DoFireClose(pSocketObj, enOperation, iErrorCode);
  83. ReleaseConnectionExtra(pSocketObj);
  84. return result;
  85. }
  86. virtual EnHandleResult DoFireShutdown()
  87. {
  88. EnHandleResult result = __super::DoFireShutdown();
  89. m_bfPool.Clear();
  90. return result;
  91. }
  92. virtual BOOL BeforeUnpause(TAgentSocketObj* pSocketObj)
  93. {
  94. CReentrantCriSecLock locallock(pSocketObj->csIo);
  95. if(!TAgentSocketObj::IsValid(pSocketObj))
  96. return FALSE;
  97. if(pSocketObj->IsPaused())
  98. return TRUE;
  99. TBufferPackInfo* pInfo = nullptr;
  100. GetConnectionReserved(pSocketObj, (PVOID*)&pInfo);
  101. ASSERT(pInfo);
  102. TBuffer* pBuffer = (TBuffer*)pInfo->pBuffer;
  103. ASSERT(pBuffer && pBuffer->IsValid());
  104. return (ParsePack(this, pInfo, pBuffer, pSocketObj, m_dwMaxPackSize, m_usHeaderFlag) != HR_ERROR);
  105. }
  106. virtual BOOL CheckParams()
  107. {
  108. if ((m_dwMaxPackSize > 0 && m_dwMaxPackSize <= TCP_PACK_MAX_SIZE_LIMIT) &&
  109. (m_usHeaderFlag >= 0 && m_usHeaderFlag <= TCP_PACK_HEADER_FLAG_LIMIT) )
  110. return __super::CheckParams();
  111. SetLastError(SE_INVALID_PARAM, __FUNCTION__, ERROR_INVALID_PARAMETER);
  112. return FALSE;
  113. }
  114. virtual void PrepareStart()
  115. {
  116. __super::PrepareStart();
  117. m_bfPool.SetMaxCacheSize (GetMaxConnectionCount());
  118. m_bfPool.SetItemCapacity (GetSocketBufferSize());
  119. m_bfPool.SetItemPoolSize (GetFreeBufferObjPool());
  120. m_bfPool.SetItemPoolHold (GetFreeBufferObjHold());
  121. m_bfPool.SetBufferLockTime (GetFreeSocketObjLockTime());
  122. m_bfPool.SetBufferPoolSize (GetFreeSocketObjPool());
  123. m_bfPool.SetBufferPoolHold (GetFreeSocketObjHold());
  124. m_bfPool.Prepare();
  125. }
  126. public:
  127. virtual void SetMaxPackSize (DWORD dwMaxPackSize) {ENSURE_HAS_STOPPED(); m_dwMaxPackSize = dwMaxPackSize;}
  128. virtual void SetPackHeaderFlag (USHORT usPackHeaderFlag) {ENSURE_HAS_STOPPED(); m_usHeaderFlag = usPackHeaderFlag;}
  129. virtual DWORD GetMaxPackSize () {return m_dwMaxPackSize;}
  130. virtual USHORT GetPackHeaderFlag() {return m_usHeaderFlag;}
  131. private:
  132. void ReleaseConnectionExtra(TAgentSocketObj* pSocketObj)
  133. {
  134. TBufferPackInfo* pInfo = nullptr;
  135. GetConnectionReserved(pSocketObj, (PVOID*)&pInfo);
  136. if(pInfo != nullptr)
  137. {
  138. m_bfPool.PutFreeBuffer(pInfo->pBuffer);
  139. TBufferPackInfo::Destruct(pInfo);
  140. ENSURE(SetConnectionReserved(pSocketObj, nullptr));
  141. }
  142. }
  143. EnHandleResult DoFireSuperReceive(TAgentSocketObj* pSocketObj, const BYTE* pData, int iLength)
  144. {return __super::DoFireReceive(pSocketObj, pData, iLength);}
  145. friend EnHandleResult ParsePack<>(CTcpPackAgentT* pThis, TBufferPackInfo* pInfo, TBuffer* pBuffer, TAgentSocketObj* pSocket, DWORD dwMaxPackSize, USHORT usPackHeaderFlag);
  146. public:
  147. CTcpPackAgentT(ITcpAgentListener* pListener)
  148. : T (pListener)
  149. , m_dwMaxPackSize (TCP_PACK_DEFAULT_MAX_SIZE)
  150. , m_usHeaderFlag (TCP_PACK_DEFAULT_HEADER_FLAG)
  151. {
  152. }
  153. virtual ~CTcpPackAgentT()
  154. {
  155. ENSURE_STOP();
  156. }
  157. private:
  158. DWORD m_dwMaxPackSize;
  159. USHORT m_usHeaderFlag;
  160. CBufferPool m_bfPool;
  161. };
  162. typedef CTcpPackAgentT<CTcpAgent> CTcpPackAgent;
  163. #ifdef _SSL_SUPPORT
  164. #include "SSLAgent.h"
  165. typedef CTcpPackAgentT<CSSLAgent> CSSLPackAgent;
  166. #endif