工具项目

TcpPackAgent.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : http://www.jessma.org
  6. * Project : https://github.com/ldcsaa
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 75375912, 44636872
  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. #include "../Common/Src/BufferPool.h"
  27. template<class T> class CTcpPackAgentT : public IPackSocket, public T
  28. {
  29. public:
  30. virtual BOOL SendPackets(CONNID dwConnID, const WSABUF pBuffers[], int iCount)
  31. {
  32. int iNewCount = iCount + 1;
  33. unique_ptr<WSABUF[]> buffers(new WSABUF[iNewCount]);
  34. DWORD header;
  35. if(!::AddPackHeader(pBuffers, iCount, buffers, m_dwMaxPackSize, m_usHeaderFlag, header))
  36. return FALSE;
  37. return __super::SendPackets(dwConnID, buffers.get(), iNewCount);
  38. }
  39. protected:
  40. virtual EnHandleResult DoFireHandShake(TSocketObj* pSocketObj)
  41. {
  42. EnHandleResult result = __super::DoFireHandShake(pSocketObj);
  43. if(result != HR_ERROR)
  44. {
  45. TBuffer* pBuffer = m_bfPool.PickFreeBuffer(pSocketObj->connID);
  46. VERIFY(SetConnectionReserved(pSocketObj, TBufferPackInfo::Construct(pBuffer)));
  47. }
  48. return result;
  49. }
  50. virtual EnHandleResult DoFireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  51. {
  52. TBufferPackInfo* pInfo = nullptr;
  53. GetConnectionReserved(pSocketObj, (PVOID*)&pInfo);
  54. ASSERT(pInfo);
  55. TBuffer* pBuffer = (TBuffer*)pInfo->pBuffer;
  56. ASSERT(pBuffer && pBuffer->IsValid());
  57. return ParsePack(this, pInfo, pBuffer, pSocketObj, m_dwMaxPackSize, m_usHeaderFlag, pData, iLength);
  58. }
  59. virtual EnHandleResult DoFireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  60. {
  61. EnHandleResult result = __super::DoFireClose(pSocketObj, enOperation, iErrorCode);
  62. TBufferPackInfo* pInfo = nullptr;
  63. GetConnectionReserved(pSocketObj, (PVOID*)&pInfo);
  64. if(pInfo != nullptr)
  65. {
  66. m_bfPool.PutFreeBuffer(pInfo->pBuffer);
  67. TBufferPackInfo::Destruct(pInfo);
  68. }
  69. return result;
  70. }
  71. virtual EnHandleResult DoFireShutdown()
  72. {
  73. EnHandleResult result = __super::DoFireShutdown();
  74. m_bfPool.Clear();
  75. return result;
  76. }
  77. virtual BOOL CheckParams()
  78. {
  79. if ((m_dwMaxPackSize > 0 && m_dwMaxPackSize <= TCP_PACK_MAX_SIZE_LIMIT) &&
  80. (m_usHeaderFlag >= 0 && m_usHeaderFlag <= TCP_PACK_HEADER_FLAG_LIMIT) )
  81. return __super::CheckParams();
  82. SetLastError(SE_INVALID_PARAM, __FUNCTION__, ERROR_INVALID_PARAMETER);
  83. return FALSE;
  84. }
  85. virtual void PrepareStart()
  86. {
  87. __super::PrepareStart();
  88. m_bfPool.SetMaxCacheSize (GetMaxConnectionCount());
  89. m_bfPool.SetItemCapacity (GetSocketBufferSize());
  90. m_bfPool.SetItemPoolSize (GetFreeBufferObjPool());
  91. m_bfPool.SetItemPoolHold (GetFreeBufferObjHold());
  92. m_bfPool.SetBufferLockTime (GetFreeSocketObjLockTime());
  93. m_bfPool.SetBufferPoolSize (GetFreeSocketObjPool());
  94. m_bfPool.SetBufferPoolHold (GetFreeSocketObjHold());
  95. m_bfPool.Prepare();
  96. }
  97. public:
  98. virtual void SetMaxPackSize (DWORD dwMaxPackSize) {m_dwMaxPackSize = dwMaxPackSize;}
  99. virtual void SetPackHeaderFlag (USHORT usPackHeaderFlag) {m_usHeaderFlag = usPackHeaderFlag;}
  100. virtual DWORD GetMaxPackSize () {return m_dwMaxPackSize;}
  101. virtual USHORT GetPackHeaderFlag() {return m_usHeaderFlag;}
  102. private:
  103. EnHandleResult DoFireSuperReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  104. {return __super::DoFireReceive(pSocketObj, pData, iLength);}
  105. friend EnHandleResult ParsePack<> (CTcpPackAgentT* pThis, TBufferPackInfo* pInfo, TBuffer* pBuffer, TSocketObj* pSocket,
  106. DWORD dwMaxPackSize, USHORT usPackHeaderFlag, const BYTE* pData, int iLength);
  107. public:
  108. CTcpPackAgentT(ITcpAgentListener* pListener)
  109. : T (pListener)
  110. , m_dwMaxPackSize (TCP_PACK_DEFAULT_MAX_SIZE)
  111. , m_usHeaderFlag (TCP_PACK_DEFAULT_HEADER_FLAG)
  112. {
  113. }
  114. virtual ~CTcpPackAgentT()
  115. {
  116. Stop();
  117. }
  118. private:
  119. DWORD m_dwMaxPackSize;
  120. USHORT m_usHeaderFlag;
  121. CBufferPool m_bfPool;
  122. };
  123. typedef CTcpPackAgentT<CTcpAgent> CTcpPackAgent;
  124. #ifdef _SSL_SUPPORT
  125. #include "SSLAgent.h"
  126. typedef CTcpPackAgentT<CSSLAgent> CSSLPackAgent;
  127. #endif