工具项目

TcpPullServer.h 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "TcpServer.h"
  25. #include "MiscHelper.h"
  26. #include "../Common/Src/BufferPool.h"
  27. template<class T> class CTcpPullServerT : 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. ASSERT(pBuffer);
  65. if(pBuffer != nullptr)
  66. m_bfPool.PutFreeBuffer(pBuffer);
  67. return result;
  68. }
  69. virtual EnHandleResult DoFireShutdown()
  70. {
  71. EnHandleResult result = __super::DoFireShutdown();
  72. m_bfPool.Clear();
  73. return result;
  74. }
  75. virtual void PrepareStart()
  76. {
  77. __super::PrepareStart();
  78. m_bfPool.SetMaxCacheSize (GetMaxConnectionCount());
  79. m_bfPool.SetItemCapacity (GetSocketBufferSize());
  80. m_bfPool.SetItemPoolSize (GetFreeBufferObjPool());
  81. m_bfPool.SetItemPoolHold (GetFreeBufferObjHold());
  82. m_bfPool.SetBufferLockTime (GetFreeSocketObjLockTime());
  83. m_bfPool.SetBufferPoolSize (GetFreeSocketObjPool());
  84. m_bfPool.SetBufferPoolHold (GetFreeSocketObjHold());
  85. m_bfPool.Prepare();
  86. }
  87. public:
  88. CTcpPullServerT(ITcpServerListener* pListener)
  89. : T(pListener)
  90. {
  91. }
  92. virtual ~CTcpPullServerT()
  93. {
  94. Stop();
  95. }
  96. private:
  97. CBufferPool m_bfPool;
  98. };
  99. typedef CTcpPullServerT<CTcpServer> CTcpPullServer;
  100. #ifdef _SSL_SUPPORT
  101. #include "SSLServer.h"
  102. typedef CTcpPullServerT<CSSLServer> CSSLPullServer;
  103. #endif