TcpPullAgent.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 CTcpPullAgentT : 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 DoFireConnect(TAgentSocketObj* pSocketObj)
  55. {
  56. EnHandleResult result = __super::DoFireConnect(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(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. 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(TAgentSocketObj* 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. virtual void ReleaseGCSocketObj(BOOL bForce = FALSE)
  104. {
  105. __super::ReleaseGCSocketObj(bForce);
  106. #ifdef USE_EXTERNAL_GC
  107. m_bfPool.ReleaseGCBuffer(bForce);
  108. #endif
  109. }
  110. private:
  111. void ReleaseConnectionExtra(TAgentSocketObj* pSocketObj)
  112. {
  113. TBuffer* pBuffer = nullptr;
  114. GetConnectionReserved(pSocketObj, (PVOID*)&pBuffer);
  115. if(pBuffer != nullptr)
  116. {
  117. m_bfPool.PutFreeBuffer(pBuffer);
  118. ENSURE(SetConnectionReserved(pSocketObj, nullptr));
  119. }
  120. }
  121. public:
  122. CTcpPullAgentT(ITcpAgentListener* pListener)
  123. : T(pListener)
  124. {
  125. }
  126. virtual ~CTcpPullAgentT()
  127. {
  128. ENSURE_STOP();
  129. }
  130. private:
  131. CBufferPool m_bfPool;
  132. };
  133. typedef CTcpPullAgentT<CTcpAgent> CTcpPullAgent;
  134. #ifdef _SSL_SUPPORT
  135. #include "SSLAgent.h"
  136. typedef CTcpPullAgentT<CSSLAgent> CSSLPullAgent;
  137. #endif