TcpPackClient.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "TcpClient.h"
  25. #include "MiscHelper.h"
  26. template<class T> class CTcpPackClientT : public IPackClient, public T
  27. {
  28. using __super = T;
  29. using __super::SetLastError;
  30. using __super::m_itPool;
  31. public:
  32. using __super::Stop;
  33. using __super::Wait;
  34. using __super::GetState;
  35. public:
  36. virtual BOOL SendPackets(const WSABUF pBuffers[], int iCount)
  37. {
  38. int iNewCount = iCount + 1;
  39. unique_ptr<WSABUF[]> buffers(new WSABUF[iNewCount]);
  40. DWORD dwHeader;
  41. if(!::AddPackHeader(pBuffers, iCount, buffers, m_dwMaxPackSize, m_usHeaderFlag, dwHeader))
  42. return FALSE;
  43. return __super::SendPackets(buffers.get(), iNewCount);
  44. }
  45. protected:
  46. virtual EnHandleResult DoFireReceive(ITcpClient* pSender, const BYTE* pData, int iLength)
  47. {
  48. return ParsePack(this, &m_pkInfo, &m_lsBuffer, (CTcpPackClientT*)pSender, m_dwMaxPackSize, m_usHeaderFlag, pData, iLength);
  49. }
  50. virtual BOOL BeforeUnpause()
  51. {
  52. return (ParsePack(this, &m_pkInfo, &m_lsBuffer, (CTcpPackClientT*)this, m_dwMaxPackSize, m_usHeaderFlag) != HR_ERROR);
  53. }
  54. virtual BOOL CheckParams()
  55. {
  56. if ((m_dwMaxPackSize > 0 && m_dwMaxPackSize <= TCP_PACK_MAX_SIZE_LIMIT) &&
  57. (m_usHeaderFlag >= 0 && m_usHeaderFlag <= TCP_PACK_HEADER_FLAG_LIMIT) )
  58. return __super::CheckParams();
  59. SetLastError(SE_INVALID_PARAM, __FUNCTION__, ERROR_INVALID_PARAMETER);
  60. return FALSE;
  61. }
  62. virtual void Reset()
  63. {
  64. m_lsBuffer.Clear();
  65. m_pkInfo.Reset();
  66. __super::Reset();
  67. }
  68. public:
  69. virtual void SetMaxPackSize (DWORD dwMaxPackSize) {ENSURE_HAS_STOPPED(); m_dwMaxPackSize = dwMaxPackSize;}
  70. virtual void SetPackHeaderFlag (USHORT usPackHeaderFlag) {ENSURE_HAS_STOPPED(); m_usHeaderFlag = usPackHeaderFlag;}
  71. virtual DWORD GetMaxPackSize () {return m_dwMaxPackSize;}
  72. virtual USHORT GetPackHeaderFlag() {return m_usHeaderFlag;}
  73. private:
  74. EnHandleResult DoFireSuperReceive(ITcpClient* pSender, const BYTE* pData, int iLength)
  75. {return __super::DoFireReceive(pSender, pData, iLength);}
  76. friend EnHandleResult ParsePack<> (CTcpPackClientT* pThis, TPackInfo<TItemListEx>* pInfo, TItemListEx* pBuffer, CTcpPackClientT* pSocket,
  77. DWORD dwMaxPackSize, USHORT usPackHeaderFlag);
  78. public:
  79. CTcpPackClientT(ITcpClientListener* pListener)
  80. : T (pListener)
  81. , m_dwMaxPackSize (TCP_PACK_DEFAULT_MAX_SIZE)
  82. , m_usHeaderFlag (TCP_PACK_DEFAULT_HEADER_FLAG)
  83. , m_pkInfo (nullptr)
  84. , m_lsBuffer (m_itPool)
  85. {
  86. }
  87. virtual ~CTcpPackClientT()
  88. {
  89. ENSURE_STOP();
  90. }
  91. private:
  92. DWORD m_dwMaxPackSize;
  93. USHORT m_usHeaderFlag;
  94. TPackInfo<TItemListEx> m_pkInfo;
  95. TItemListEx m_lsBuffer;
  96. };
  97. typedef CTcpPackClientT<CTcpClient> CTcpPackClient;
  98. #ifdef _SSL_SUPPORT
  99. #include "SSLClient.h"
  100. typedef CTcpPackClientT<CSSLClient> CSSLPackClient;
  101. #endif