TcpPackServer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #pragma once
  25. #include "TcpServer.h"
  26. #include "MiscHelper.h"
  27. #include "../../Common/Src/bufferpool.h"
  28. template<class T> class CTcpPackServerT : public IPackSocket, public T
  29. {
  30. public:
  31. virtual BOOL SendPackets(CONNID dwConnID, const WSABUF pBuffers[], int iCount)
  32. {
  33. int iNewCount = iCount + 1;
  34. unique_ptr<WSABUF[]> buffers(new WSABUF[iNewCount]);
  35. DWORD header;
  36. if(!::AddPackHeader(pBuffers, iCount, buffers, m_dwMaxPackSize, m_usHeaderFlag, header))
  37. return FALSE;
  38. return __super::SendPackets(dwConnID, buffers.get(), iNewCount);
  39. }
  40. protected:
  41. virtual EnHandleResult DoFireHandShake(TSocketObj* pSocketObj)
  42. {
  43. EnHandleResult result = __super::DoFireHandShake(pSocketObj);
  44. if(result != HR_ERROR)
  45. {
  46. TBuffer* pBuffer = m_bfPool.PickFreeBuffer(pSocketObj->connID);
  47. VERIFY(SetConnectionReserved(pSocketObj, TBufferPackInfo::Construct(pBuffer)));
  48. }
  49. return result;
  50. }
  51. virtual EnHandleResult DoFireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  52. {
  53. TBufferPackInfo* pInfo = nullptr;
  54. GetConnectionReserved(pSocketObj, (PVOID*)&pInfo);
  55. ASSERT(pInfo);
  56. TBuffer* pBuffer = (TBuffer*)pInfo->pBuffer;
  57. ASSERT(pBuffer && pBuffer->IsValid());
  58. return ParsePack(this, pInfo, pBuffer, pSocketObj, m_dwMaxPackSize, m_usHeaderFlag, pData, iLength);
  59. }
  60. virtual EnHandleResult DoFireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  61. {
  62. EnHandleResult result = __super::DoFireClose(pSocketObj, enOperation, iErrorCode);
  63. TBufferPackInfo* pInfo = nullptr;
  64. GetConnectionReserved(pSocketObj, (PVOID*)&pInfo);
  65. ASSERT(pInfo);
  66. if(pInfo != nullptr)
  67. {
  68. m_bfPool.PutFreeBuffer(pInfo->pBuffer);
  69. TBufferPackInfo::Destruct(pInfo);
  70. }
  71. return result;
  72. }
  73. virtual EnHandleResult DoFireShutdown()
  74. {
  75. EnHandleResult result = __super::DoFireShutdown();
  76. m_bfPool.Clear();
  77. return HR_OK;
  78. }
  79. virtual BOOL CheckParams()
  80. {
  81. if ((m_dwMaxPackSize > 0 && m_dwMaxPackSize <= TCP_PACK_MAX_SIZE_LIMIT) &&
  82. (m_usHeaderFlag >= 0 && m_usHeaderFlag <= TCP_PACK_HEADER_FLAG_LIMIT) )
  83. return __super::CheckParams();
  84. SetLastError(SE_INVALID_PARAM, __FUNCTION__, ERROR_INVALID_PARAMETER);
  85. return FALSE;
  86. }
  87. virtual void PrepareStart()
  88. {
  89. __super::PrepareStart();
  90. m_bfPool.SetMaxCacheSize (GetMaxConnectionCount());
  91. m_bfPool.SetItemCapacity (GetSocketBufferSize());
  92. m_bfPool.SetItemPoolSize (GetFreeBufferObjPool());
  93. m_bfPool.SetItemPoolHold (GetFreeBufferObjHold());
  94. m_bfPool.SetBufferLockTime (GetFreeSocketObjLockTime());
  95. m_bfPool.SetBufferPoolSize (GetFreeSocketObjPool());
  96. m_bfPool.SetBufferPoolHold (GetFreeSocketObjHold());
  97. m_bfPool.Prepare();
  98. }
  99. public:
  100. virtual void SetMaxPackSize (DWORD dwMaxPackSize) {m_dwMaxPackSize = dwMaxPackSize;}
  101. virtual void SetPackHeaderFlag (USHORT usPackHeaderFlag) {m_usHeaderFlag = usPackHeaderFlag;}
  102. virtual DWORD GetMaxPackSize () {return m_dwMaxPackSize;}
  103. virtual USHORT GetPackHeaderFlag() {return m_usHeaderFlag;}
  104. private:
  105. EnHandleResult DoFireSuperReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  106. {return __super::DoFireReceive(pSocketObj, pData, iLength);}
  107. friend EnHandleResult ParsePack<> (CTcpPackServerT* pThis, TBufferPackInfo* pInfo, TBuffer* pBuffer, TSocketObj* pSocket,
  108. DWORD dwMaxPackSize, USHORT usPackHeaderFlag, const BYTE* pData, int iLength);
  109. public:
  110. CTcpPackServerT(ITcpServerListener* pListener)
  111. : T (pListener)
  112. , m_dwMaxPackSize (TCP_PACK_DEFAULT_MAX_SIZE)
  113. , m_usHeaderFlag (TCP_PACK_DEFAULT_HEADER_FLAG)
  114. {
  115. }
  116. virtual ~CTcpPackServerT()
  117. {
  118. Stop();
  119. }
  120. private:
  121. DWORD m_dwMaxPackSize;
  122. USHORT m_usHeaderFlag;
  123. CBufferPool m_bfPool;
  124. };
  125. typedef CTcpPackServerT<CTcpServer> CTcpPackServer;
  126. #ifdef _SSL_SUPPORT
  127. #include "SSLServer.h"
  128. typedef CTcpPackServerT<CSSLServer> CSSLPackServer;
  129. #endif