UdpArqClient.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "UdpArqClient.h"
  24. #ifdef _UDP_SUPPORT
  25. BOOL CUdpArqClient::CheckParams()
  26. {
  27. DWORD dwMaxDatagramSize = GetMaxDatagramSize();
  28. if(m_dwMtu == 0)
  29. m_arqAttr.dwMtu = dwMaxDatagramSize;
  30. else
  31. {
  32. if(m_dwMtu > dwMaxDatagramSize)
  33. return FALSE;
  34. m_arqAttr.dwMtu = m_dwMtu;
  35. }
  36. return __super::CheckParams() && m_arqAttr.IsValid();
  37. }
  38. void CUdpArqClient::PrepareStart()
  39. {
  40. __super::PrepareStart();
  41. }
  42. void CUdpArqClient::Reset()
  43. {
  44. m_arqSession.Reset();
  45. __super::Reset();
  46. }
  47. void CUdpArqClient::OnWorkerThreadStart(THR_ID dwThreadID)
  48. {
  49. m_arqBuffer.Malloc(m_arqAttr.dwMaxMessageSize);
  50. m_arqTimer = ::CreateTimer(m_arqAttr.dwFlushInterval);
  51. }
  52. void CUdpArqClient::OnWorkerThreadEnd(THR_ID dwThreadID)
  53. {
  54. if(IS_VALID_FD(m_arqTimer))
  55. {
  56. close(m_arqTimer);
  57. m_arqTimer = INVALID_FD;
  58. }
  59. m_arqBuffer.Free();
  60. }
  61. HANDLE CUdpArqClient::GetUserEvent()
  62. {
  63. return m_arqTimer;
  64. }
  65. BOOL CUdpArqClient::OnUserEvent()
  66. {
  67. ::ReadTimer(m_arqTimer);
  68. return m_arqSession.Check();
  69. }
  70. BOOL CUdpArqClient::Send(const BYTE* pBuffer, int iLength, int iOffset)
  71. {
  72. ASSERT(pBuffer && iLength > 0 && iLength <= (int)m_arqAttr.dwMaxMessageSize);
  73. int result = NO_ERROR;
  74. if(pBuffer && iLength > 0 && iLength <= (int)m_arqAttr.dwMaxMessageSize)
  75. {
  76. if(IsConnected())
  77. {
  78. if(iOffset != 0) pBuffer += iOffset;
  79. result = m_arqSession.Send(pBuffer, iLength);
  80. }
  81. else
  82. result = ERROR_INVALID_STATE;
  83. }
  84. else
  85. result = ERROR_INVALID_PARAMETER;
  86. if(result != NO_ERROR)
  87. ::SetLastError(result);
  88. return (result == NO_ERROR);
  89. }
  90. BOOL CUdpArqClient::SendPackets(const WSABUF pBuffers[], int iCount)
  91. {
  92. ASSERT(pBuffers && iCount > 0);
  93. if(!pBuffers || iCount <= 0)
  94. return ERROR_INVALID_PARAMETER;
  95. if(iCount == 1)
  96. return Send((const BYTE*)pBuffers[0].buf, pBuffers[0].len);
  97. if(!IsConnected())
  98. return ERROR_INVALID_STATE;
  99. int iLength = 0;
  100. int iMaxLen = (int)m_arqAttr.dwMaxMessageSize;
  101. for(int i = 0; i < iCount; i++)
  102. iLength += pBuffers[i].len;
  103. if(iLength <= 0 || iLength > iMaxLen)
  104. return ERROR_INCORRECT_SIZE;
  105. CBufferPtr sndBuffer(iLength);
  106. sndBuffer.SetSize(0);
  107. for(int i = 0; i < iCount; i++)
  108. {
  109. int iBufLen = pBuffers[i].len;
  110. if(iBufLen > 0)
  111. {
  112. BYTE* pBuffer = (BYTE*)pBuffers[i].buf;
  113. ASSERT(pBuffer);
  114. sndBuffer.Cat(pBuffer, iBufLen);
  115. }
  116. }
  117. int result = m_arqSession.Send(sndBuffer.Ptr(), (int)sndBuffer.Size());
  118. if(result != NO_ERROR)
  119. ::SetLastError(result);
  120. return (result == NO_ERROR);
  121. }
  122. int CUdpArqClient::ArqOutputProc(const char* pBuffer, int iLength, IKCPCB* kcp, LPVOID pv)
  123. {
  124. CUdpArqClient* pClient = (CUdpArqClient*)pv;
  125. BOOL isOK = pClient->__super::Send((const BYTE*)pBuffer, iLength);
  126. return isOK ? NO_ERROR : ::WSAGetLastError();
  127. }
  128. EnHandleResult CUdpArqClient::FireConnect()
  129. {
  130. EnHandleResult result = DoFireConnect(this);
  131. if(result != HR_ERROR)
  132. m_arqSession.Renew(this, this, m_arqAttr);
  133. return result;
  134. }
  135. EnHandleResult CUdpArqClient::FireReceive(const BYTE* pData, int iLength)
  136. {
  137. return m_arqSession.Receive(pData, iLength, m_arqBuffer.Ptr(), (int)m_arqBuffer.Size());
  138. }
  139. BOOL CUdpArqClient::GetWaitingSendMessageCount(int& iCount)
  140. {
  141. iCount = m_arqSession.GetWaitingSend();
  142. return (iCount >= 0);
  143. }
  144. #endif