工具项目

MiscHelper.cpp 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : http://www.jessma.org
  6. * Project : https://github.com/ldcsaa
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 75375912, 44636872
  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. #include "stdafx.h"
  24. #include "MiscHelper.h"
  25. BOOL AddPackHeader(const WSABUF * pBuffers, int iCount, unique_ptr<WSABUF[]>& buffers, DWORD dwMaxPackSize, USHORT usPackHeaderFlag, DWORD& header)
  26. {
  27. ASSERT(pBuffers && iCount > 0);
  28. DWORD iLength = 0;
  29. for(int i = 0; i < iCount; i++)
  30. {
  31. const WSABUF& buf = pBuffers[i];
  32. buffers[i + 1] = buf;
  33. iLength += buf.len;
  34. }
  35. if(iLength == 0 || iLength > dwMaxPackSize)
  36. {
  37. ::SetLastError(ERROR_BAD_LENGTH);
  38. return FALSE;
  39. }
  40. header = (usPackHeaderFlag << TCP_PACK_LENGTH_BITS) | iLength;
  41. buffers[0].len = sizeof(header);
  42. buffers[0].buf = (char*)&header;
  43. return TRUE;
  44. }