TcpPullAgent.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "TcpAgent.h"
  26. #include "../../Common/Src/bufferpool.h"
  27. template<class T> class CTcpPullAgentT : public IPullSocket, public T
  28. {
  29. public:
  30. virtual EnFetchResult Fetch(CONNID dwConnID, BYTE* pData, int iLength)
  31. {
  32. TBuffer* pBuffer = m_bfPool[dwConnID];
  33. return ::FetchBuffer(pBuffer, pData, iLength);
  34. }
  35. virtual EnFetchResult Peek(CONNID dwConnID, BYTE* pData, int iLength)
  36. {
  37. TBuffer* pBuffer = m_bfPool[dwConnID];
  38. return ::PeekBuffer(pBuffer, pData, iLength);
  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.PutCacheBuffer(pSocketObj->connID);
  47. VERIFY(SetConnectionReserved(pSocketObj, pBuffer));
  48. }
  49. return result;
  50. }
  51. virtual EnHandleResult DoFireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  52. {
  53. TBuffer* pBuffer = nullptr;
  54. GetConnectionReserved(pSocketObj, (PVOID*)&pBuffer);
  55. ASSERT(pBuffer && pBuffer->IsValid());
  56. pBuffer->Cat(pData, iLength);
  57. return __super::DoFireReceive(pSocketObj, pBuffer->Length());
  58. }
  59. virtual EnHandleResult DoFireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  60. {
  61. EnHandleResult result = __super::DoFireClose(pSocketObj, enOperation, iErrorCode);
  62. TBuffer* pBuffer = nullptr;
  63. GetConnectionReserved(pSocketObj, (PVOID*)&pBuffer);
  64. if(pBuffer != nullptr)
  65. m_bfPool.PutFreeBuffer(pBuffer);
  66. return result;
  67. }
  68. virtual EnHandleResult DoFireShutdown()
  69. {
  70. EnHandleResult result = __super::DoFireShutdown();
  71. m_bfPool.Clear();
  72. return result;
  73. }
  74. virtual void PrepareStart()
  75. {
  76. __super::PrepareStart();
  77. m_bfPool.SetMaxCacheSize (GetMaxConnectionCount());
  78. m_bfPool.SetItemCapacity (GetSocketBufferSize());
  79. m_bfPool.SetItemPoolSize (GetFreeBufferObjPool());
  80. m_bfPool.SetItemPoolHold (GetFreeBufferObjHold());
  81. m_bfPool.SetBufferLockTime (GetFreeSocketObjLockTime());
  82. m_bfPool.SetBufferPoolSize (GetFreeSocketObjPool());
  83. m_bfPool.SetBufferPoolHold (GetFreeSocketObjHold());
  84. m_bfPool.Prepare();
  85. }
  86. public:
  87. CTcpPullAgentT(ITcpAgentListener* pListener)
  88. : T(pListener)
  89. {
  90. }
  91. virtual ~CTcpPullAgentT()
  92. {
  93. Stop();
  94. }
  95. private:
  96. CBufferPool m_bfPool;
  97. };
  98. typedef CTcpPullAgentT<CTcpAgent> CTcpPullAgent;
  99. #ifdef _SSL_SUPPORT
  100. #include "SSLAgent.h"
  101. typedef CTcpPullAgentT<CSSLAgent> CSSLPullAgent;
  102. #endif