UdpArqServer.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "UdpArqServer.h"
  24. #ifdef _UDP_SUPPORT
  25. BOOL CUdpArqServer::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 CUdpArqServer::PrepareStart()
  39. {
  40. __super::PrepareStart();
  41. m_ssPool.SetSessionLockTime(GetFreeSocketObjLockTime());
  42. m_ssPool.SetSessionPoolSize(GetFreeSocketObjPool());
  43. m_ssPool.SetSessionPoolHold(GetFreeSocketObjHold());
  44. m_ssPool.Prepare();
  45. }
  46. void CUdpArqServer::Reset()
  47. {
  48. ::ClearPtrMap(m_rcBuffers);
  49. m_ssPool.Clear();
  50. __super::Reset();
  51. }
  52. void CUdpArqServer::OnWorkerThreadStart(THR_ID dwThreadID)
  53. {
  54. {
  55. CCriSecLock locallock(m_csRcBuffers);
  56. m_rcBuffers[dwThreadID] = new CBufferPtr(m_arqAttr.dwMaxMessageSize);
  57. }
  58. while((DWORD)m_rcBuffers.size() < GetWorkerThreadCount())
  59. ::WaitFor(3);
  60. }
  61. BOOL CUdpArqServer::Send(CONNID dwConnID, const BYTE* pBuffer, int iLength, int iOffset)
  62. {
  63. ASSERT(pBuffer && iLength > 0 && iLength <= (int)m_arqAttr.dwMaxMessageSize);
  64. int result = NO_ERROR;
  65. if(pBuffer && iLength > 0 && iLength <= (int)m_arqAttr.dwMaxMessageSize)
  66. {
  67. if(iOffset != 0) pBuffer += iOffset;
  68. TUdpSocketObj* pSocketObj = FindSocketObj(dwConnID);
  69. if(TUdpSocketObj::IsValid(pSocketObj))
  70. result = SendArq(pSocketObj, pBuffer, iLength);
  71. else
  72. result = ERROR_OBJECT_NOT_FOUND;
  73. }
  74. else
  75. result = ERROR_INVALID_PARAMETER;
  76. if(result != NO_ERROR)
  77. ::SetLastError(result);
  78. return (result == NO_ERROR);
  79. }
  80. BOOL CUdpArqServer::SendPackets(CONNID dwConnID, const WSABUF pBuffers[], int iCount)
  81. {
  82. ASSERT(pBuffers && iCount > 0);
  83. if(!pBuffers || iCount <= 0)
  84. return ERROR_INVALID_PARAMETER;
  85. if(iCount == 1)
  86. return Send(dwConnID, (const BYTE*)pBuffers[0].buf, pBuffers[0].len);
  87. TUdpSocketObj* pSocketObj = FindSocketObj(dwConnID);
  88. if(!TUdpSocketObj::IsValid(pSocketObj))
  89. {
  90. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  91. return FALSE;
  92. }
  93. int iLength = 0;
  94. int iMaxLen = (int)m_arqAttr.dwMaxMessageSize;
  95. for(int i = 0; i < iCount; i++)
  96. iLength += pBuffers[i].len;
  97. if(iLength <= 0 || iLength > iMaxLen)
  98. return ERROR_INCORRECT_SIZE;
  99. CBufferPtr sndBuffer(iLength);
  100. sndBuffer.SetSize(0);
  101. for(int i = 0; i < iCount; i++)
  102. {
  103. int iBufLen = pBuffers[i].len;
  104. if(iBufLen > 0)
  105. {
  106. BYTE* pBuffer = (BYTE*)pBuffers[i].buf;
  107. ASSERT(pBuffer);
  108. sndBuffer.Cat(pBuffer, iBufLen);
  109. }
  110. }
  111. int result = SendArq(pSocketObj, sndBuffer.Ptr(), (int)sndBuffer.Size());
  112. if(result != NO_ERROR)
  113. ::SetLastError(result);
  114. return (result == NO_ERROR);
  115. }
  116. int CUdpArqServer::SendArq(TUdpSocketObj* pSocketObj, const BYTE* pBuffer, int iLength)
  117. {
  118. CArqSessionEx* pSession = nullptr;
  119. GetConnectionReserved(pSocketObj, (PVOID*)&pSession);
  120. if(pSession == nullptr)
  121. return ERROR_OBJECT_NOT_FOUND;
  122. CLocalSafeCounter localcounter(*pSession);
  123. return pSession->Send(pBuffer, iLength);
  124. }
  125. int CUdpArqServer::ArqOutputProc(const char* pBuffer, int iLength, IKCPCB* kcp, LPVOID pv)
  126. {
  127. TUdpSocketObj* pSocketObj = (TUdpSocketObj*)pv;
  128. if(!TUdpSocketObj::IsValid(pSocketObj))
  129. return ERROR_OBJECT_NOT_FOUND;
  130. CUdpArqServer* pServer = (CUdpArqServer*)IUdpArqServer::FromS((IUdpServer*)pSocketObj->pHolder);
  131. TItemPtr itPtr(pServer->m_bfObjPool, pServer->m_bfObjPool.PickFreeItem());
  132. itPtr->Cat((const BYTE*)pBuffer, iLength);
  133. return pServer->SendInternal(pSocketObj, itPtr);
  134. }
  135. EnHandleResult CUdpArqServer::FireAccept(TUdpSocketObj* pSocketObj)
  136. {
  137. EnHandleResult result = DoFireAccept(pSocketObj);
  138. if(result != HR_ERROR)
  139. {
  140. CArqSessionEx* pSession = m_ssPool.PickFreeSession(this, pSocketObj, m_arqAttr);
  141. ENSURE(SetConnectionReserved(pSocketObj, pSession));
  142. }
  143. return result;
  144. }
  145. EnHandleResult CUdpArqServer::FireReceive(TUdpSocketObj* pSocketObj, const BYTE* pData, int iLength)
  146. {
  147. CArqSessionEx* pSession = nullptr;
  148. GetConnectionReserved(pSocketObj, (PVOID*)&pSession);
  149. CLocalSafeCounter localcounter(*pSession);
  150. CBufferPtr& rcBuffer = *m_rcBuffers[SELF_THREAD_ID];
  151. return pSession->Receive(pData, iLength, rcBuffer.Ptr(), (int)rcBuffer.Size());
  152. }
  153. EnHandleResult CUdpArqServer::FireClose(TUdpSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  154. {
  155. EnHandleResult result = DoFireClose(pSocketObj, enOperation, iErrorCode);
  156. CArqSessionEx* pSession = nullptr;
  157. GetConnectionReserved(pSocketObj, (PVOID*)&pSession);
  158. if(pSession != nullptr)
  159. m_ssPool.PutFreeSession(pSession);
  160. return result;
  161. }
  162. BOOL CUdpArqServer::GetWaitingSendMessageCount(CONNID dwConnID, int& iCount)
  163. {
  164. TUdpSocketObj* pSocketObj = FindSocketObj(dwConnID);
  165. if(!TUdpSocketObj::IsValid(pSocketObj))
  166. {
  167. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  168. return FALSE;
  169. }
  170. CArqSessionEx* pSession = nullptr;
  171. GetConnectionReserved(pSocketObj, (PVOID*)&pSession);
  172. if(pSession == nullptr)
  173. {
  174. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  175. return FALSE;
  176. }
  177. {
  178. CLocalSafeCounter localcounter(*pSession);
  179. iCount = pSession->GetWaitingSend();
  180. }
  181. return (iCount >= 0);
  182. }
  183. #endif