MiscHelper.h 3.4 KB

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