工具项目

TcpPullAgent.h 3.2KB

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