MiscHelper.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #include "stdafx.h"
  25. #include "MiscHelper.h"
  26. BOOL AddPackHeader(const WSABUF * pBuffers, int iCount, unique_ptr<WSABUF[]>& buffers, DWORD dwMaxPackSize, USHORT usPackHeaderFlag, DWORD& header)
  27. {
  28. ASSERT(pBuffers && iCount > 0);
  29. DWORD iLength = 0;
  30. for(int i = 0; i < iCount; i++)
  31. {
  32. const WSABUF& buf = pBuffers[i];
  33. buffers[i + 1] = buf;
  34. iLength += buf.len;
  35. }
  36. if(iLength == 0 || iLength > dwMaxPackSize)
  37. {
  38. ::SetLastError(ERROR_BAD_LENGTH);
  39. return FALSE;
  40. }
  41. header = (usPackHeaderFlag << TCP_PACK_LENGTH_BITS) | iLength;
  42. buffers[0].len = sizeof(header);
  43. buffers[0].buf = (char*)&header;
  44. return TRUE;
  45. }