TcpPackClient.h 3.1 KB

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