TcpPullServer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "TcpServer.h"
  25. #include "MiscHelper.h"
  26. template<class T> class CTcpPullServerT : public IPullSocket, 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. public:
  39. using __super::Stop;
  40. using __super::Wait;
  41. using __super::GetState;
  42. public:
  43. virtual EnFetchResult Fetch(CONNID dwConnID, BYTE* pData, int iLength)
  44. {
  45. TBuffer* pBuffer = m_bfPool[dwConnID];
  46. return ::FetchBuffer(pBuffer, pData, iLength);
  47. }
  48. virtual EnFetchResult Peek(CONNID dwConnID, BYTE* pData, int iLength)
  49. {
  50. TBuffer* pBuffer = m_bfPool[dwConnID];
  51. return ::PeekBuffer(pBuffer, pData, iLength);
  52. }
  53. protected:
  54. virtual EnHandleResult DoFireAccept(TSocketObj* pSocketObj)
  55. {
  56. EnHandleResult result = __super::DoFireAccept(pSocketObj);
  57. if(result != HR_ERROR)
  58. {
  59. TBuffer* pBuffer = m_bfPool.PutCacheBuffer(pSocketObj->connID);
  60. ENSURE(SetConnectionReserved(pSocketObj, pBuffer));
  61. }
  62. return result;
  63. }
  64. virtual EnHandleResult DoFireHandShake(TSocketObj* pSocketObj)
  65. {
  66. EnHandleResult result = __super::DoFireHandShake(pSocketObj);
  67. if(result == HR_ERROR)
  68. ReleaseConnectionExtra(pSocketObj);
  69. return result;
  70. }
  71. virtual EnHandleResult DoFireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  72. {
  73. TBuffer* pBuffer = nullptr;
  74. GetConnectionReserved(pSocketObj, (PVOID*)&pBuffer);
  75. ASSERT(pBuffer && pBuffer->IsValid());
  76. pBuffer->Cat(pData, iLength);
  77. return __super::DoFireReceive(pSocketObj, pBuffer->Length());
  78. }
  79. virtual EnHandleResult DoFireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  80. {
  81. EnHandleResult result = __super::DoFireClose(pSocketObj, enOperation, iErrorCode);
  82. ReleaseConnectionExtra(pSocketObj);
  83. return result;
  84. }
  85. virtual EnHandleResult DoFireShutdown()
  86. {
  87. EnHandleResult result = __super::DoFireShutdown();
  88. m_bfPool.Clear();
  89. return result;
  90. }
  91. virtual void PrepareStart()
  92. {
  93. __super::PrepareStart();
  94. m_bfPool.SetMaxCacheSize (GetMaxConnectionCount());
  95. m_bfPool.SetItemCapacity (GetSocketBufferSize());
  96. m_bfPool.SetItemPoolSize (GetFreeBufferObjPool());
  97. m_bfPool.SetItemPoolHold (GetFreeBufferObjHold());
  98. m_bfPool.SetBufferLockTime (GetFreeSocketObjLockTime());
  99. m_bfPool.SetBufferPoolSize (GetFreeSocketObjPool());
  100. m_bfPool.SetBufferPoolHold (GetFreeSocketObjHold());
  101. m_bfPool.Prepare();
  102. }
  103. private:
  104. void ReleaseConnectionExtra(TSocketObj* pSocketObj)
  105. {
  106. TBuffer* pBuffer = nullptr;
  107. GetConnectionReserved(pSocketObj, (PVOID*)&pBuffer);
  108. if(pBuffer != nullptr)
  109. {
  110. m_bfPool.PutFreeBuffer(pBuffer);
  111. ENSURE(SetConnectionReserved(pSocketObj, nullptr));
  112. }
  113. }
  114. public:
  115. CTcpPullServerT(ITcpServerListener* pListener)
  116. : T(pListener)
  117. {
  118. }
  119. virtual ~CTcpPullServerT()
  120. {
  121. ENSURE_STOP();
  122. }
  123. private:
  124. CBufferPool m_bfPool;
  125. };
  126. typedef CTcpPullServerT<CTcpServer> CTcpPullServer;
  127. #ifdef _SSL_SUPPORT
  128. #include "SSLServer.h"
  129. typedef CTcpPullServerT<CSSLServer> CSSLPullServer;
  130. #endif