MiscHelper.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "SocketHelper.h"
  25. /* Pack Data Info */
  26. template<typename B = void> struct TPackInfo
  27. {
  28. bool header;
  29. DWORD length;
  30. B* pBuffer;
  31. static TPackInfo* Construct(B* pbuf = nullptr, bool head = true, DWORD len = sizeof(DWORD))
  32. {
  33. return new TPackInfo(pbuf, head, len);
  34. }
  35. static void Destruct(TPackInfo* pPackInfo)
  36. {
  37. if(pPackInfo)
  38. delete pPackInfo;
  39. }
  40. TPackInfo(B* pbuf = nullptr, bool head = true, DWORD len = sizeof(DWORD))
  41. : header(head), length(len), pBuffer(pbuf)
  42. {
  43. }
  44. void Reset()
  45. {
  46. header = true;
  47. length = sizeof(DWORD);
  48. pBuffer = nullptr;
  49. }
  50. };
  51. typedef TPackInfo<TBuffer> TBufferPackInfo;
  52. BOOL AddPackHeader(const WSABUF * pBuffers, int iCount, unique_ptr<WSABUF[]>& buffers, DWORD dwMaxPackSize, USHORT usPackHeaderFlag, DWORD& dwHeader);
  53. template<class B> EnFetchResult FetchBuffer(B* pBuffer, BYTE* pData, int iLength)
  54. {
  55. ASSERT(pBuffer != nullptr);
  56. ASSERT(pData != nullptr && iLength > 0);
  57. EnFetchResult result = FR_OK;
  58. if(pBuffer->Length() >= iLength)
  59. pBuffer->Fetch(pData, iLength);
  60. else
  61. result = FR_LENGTH_TOO_LONG;
  62. return result;
  63. }
  64. template<class B> EnFetchResult PeekBuffer(B* pBuffer, BYTE* pData, int iLength)
  65. {
  66. ASSERT(pBuffer != nullptr);
  67. ASSERT(pData != nullptr && iLength > 0);
  68. EnFetchResult result = FR_OK;
  69. if(pBuffer->Length() >= iLength)
  70. pBuffer->Peek(pData, iLength);
  71. else
  72. result = FR_LENGTH_TOO_LONG;
  73. return result;
  74. }
  75. template<class T, class B, class S> EnHandleResult ParsePack(T* pThis, TPackInfo<B>* pInfo, B* pBuffer, S* pSocket, DWORD dwMaxPackSize, USHORT usPackHeaderFlag)
  76. {
  77. EnHandleResult rs = HR_OK;
  78. int required = pInfo->length;
  79. int remain = pBuffer->Length();
  80. while(remain >= required)
  81. {
  82. if(pSocket->IsPaused())
  83. break;
  84. remain -= required;
  85. CBufferPtr buffer(required);
  86. pBuffer->Fetch(buffer, (int)buffer.Size());
  87. if(pInfo->header)
  88. {
  89. DWORD header = ::HToLE32(*((DWORD*)(BYTE*)buffer));
  90. if(usPackHeaderFlag != 0)
  91. {
  92. USHORT flag = (USHORT)(header >> TCP_PACK_LENGTH_BITS);
  93. if(flag != usPackHeaderFlag)
  94. {
  95. ::SetLastError(ERROR_INVALID_DATA);
  96. return HR_ERROR;
  97. }
  98. }
  99. DWORD len = header & TCP_PACK_LENGTH_MASK;
  100. if(len == 0 || len > dwMaxPackSize)
  101. {
  102. ::SetLastError(ERROR_BAD_LENGTH);
  103. return HR_ERROR;
  104. }
  105. required = len;
  106. }
  107. else
  108. {
  109. rs = pThis->DoFireSuperReceive(pSocket, (const BYTE*)buffer, (int)buffer.Size());
  110. if(rs == HR_ERROR)
  111. return rs;
  112. required = sizeof(DWORD);
  113. }
  114. pInfo->header = !pInfo->header;
  115. pInfo->length = required;
  116. }
  117. return rs;
  118. }
  119. template<class T, class B, class S> EnHandleResult ParsePack(T* pThis, TPackInfo<B>* pInfo, B* pBuffer, S* pSocket, DWORD dwMaxPackSize, USHORT usPackHeaderFlag, const BYTE* pData, int iLength)
  120. {
  121. pBuffer->Cat(pData, iLength);
  122. return ParsePack(pThis, pInfo, pBuffer, pSocket, dwMaxPackSize, usPackHeaderFlag);
  123. }