TcpPullServer.h 3.2 KB

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