UdpArqServer.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. void CUdpArqServer::ReleaseGCSocketObj(BOOL bForce)
  62. {
  63. __super::ReleaseGCSocketObj(bForce);
  64. #ifdef USE_EXTERNAL_GC
  65. m_ssPool.ReleaseGCSession(bForce);
  66. #endif
  67. }
  68. BOOL CUdpArqServer::Send(CONNID dwConnID, const BYTE* pBuffer, int iLength, int iOffset)
  69. {
  70. ASSERT(pBuffer && iLength > 0 && iLength <= (int)m_arqAttr.dwMaxMessageSize);
  71. int result = NO_ERROR;
  72. if(pBuffer && iLength > 0 && iLength <= (int)m_arqAttr.dwMaxMessageSize)
  73. {
  74. if(iOffset != 0) pBuffer += iOffset;
  75. TUdpSocketObj* pSocketObj = FindSocketObj(dwConnID);
  76. if(TUdpSocketObj::IsValid(pSocketObj))
  77. result = SendArq(pSocketObj, pBuffer, iLength);
  78. else
  79. result = ERROR_OBJECT_NOT_FOUND;
  80. }
  81. else
  82. result = ERROR_INVALID_PARAMETER;
  83. if(result != NO_ERROR)
  84. ::SetLastError(result);
  85. return (result == NO_ERROR);
  86. }
  87. BOOL CUdpArqServer::SendPackets(CONNID dwConnID, const WSABUF pBuffers[], int iCount)
  88. {
  89. ASSERT(pBuffers && iCount > 0);
  90. if(!pBuffers || iCount <= 0)
  91. return ERROR_INVALID_PARAMETER;
  92. if(iCount == 1)
  93. return Send(dwConnID, (const BYTE*)pBuffers[0].buf, pBuffers[0].len);
  94. TUdpSocketObj* pSocketObj = FindSocketObj(dwConnID);
  95. if(!TUdpSocketObj::IsValid(pSocketObj))
  96. {
  97. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  98. return FALSE;
  99. }
  100. int iLength = 0;
  101. int iMaxLen = (int)m_arqAttr.dwMaxMessageSize;
  102. for(int i = 0; i < iCount; i++)
  103. iLength += pBuffers[i].len;
  104. if(iLength <= 0 || iLength > iMaxLen)
  105. return ERROR_INCORRECT_SIZE;
  106. CBufferPtr sndBuffer(iLength);
  107. sndBuffer.SetSize(0);
  108. for(int i = 0; i < iCount; i++)
  109. {
  110. int iBufLen = pBuffers[i].len;
  111. if(iBufLen > 0)
  112. {
  113. BYTE* pBuffer = (BYTE*)pBuffers[i].buf;
  114. ASSERT(pBuffer);
  115. sndBuffer.Cat(pBuffer, iBufLen);
  116. }
  117. }
  118. int result = SendArq(pSocketObj, sndBuffer.Ptr(), (int)sndBuffer.Size());
  119. if(result != NO_ERROR)
  120. ::SetLastError(result);
  121. return (result == NO_ERROR);
  122. }
  123. int CUdpArqServer::SendArq(TUdpSocketObj* pSocketObj, const BYTE* pBuffer, int iLength)
  124. {
  125. CArqSessionEx* pSession = nullptr;
  126. GetConnectionReserved(pSocketObj, (PVOID*)&pSession);
  127. if(pSession == nullptr)
  128. return ERROR_OBJECT_NOT_FOUND;
  129. CLocalSafeCounter localcounter(*pSession);
  130. return pSession->Send(pBuffer, iLength);
  131. }
  132. int CUdpArqServer::ArqOutputProc(const char* pBuffer, int iLength, IKCPCB* kcp, LPVOID pv)
  133. {
  134. TUdpSocketObj* pSocketObj = (TUdpSocketObj*)pv;
  135. if(!TUdpSocketObj::IsValid(pSocketObj))
  136. return ERROR_OBJECT_NOT_FOUND;
  137. CUdpArqServer* pServer = (CUdpArqServer*)IUdpArqServer::FromS((IUdpServer*)pSocketObj->pHolder);
  138. TItemPtr itPtr(pServer->m_bfObjPool, pServer->m_bfObjPool.PickFreeItem());
  139. itPtr->Cat((const BYTE*)pBuffer, iLength);
  140. return pServer->SendInternal(pSocketObj, itPtr);
  141. }
  142. EnHandleResult CUdpArqServer::FireAccept(TUdpSocketObj* pSocketObj)
  143. {
  144. EnHandleResult result = DoFireAccept(pSocketObj);
  145. if(result != HR_ERROR)
  146. {
  147. CArqSessionEx* pSession = m_ssPool.PickFreeSession(this, pSocketObj, m_arqAttr);
  148. ENSURE(SetConnectionReserved(pSocketObj, pSession));
  149. }
  150. return result;
  151. }
  152. EnHandleResult CUdpArqServer::FireReceive(TUdpSocketObj* pSocketObj, const BYTE* pData, int iLength)
  153. {
  154. CArqSessionEx* pSession = nullptr;
  155. GetConnectionReserved(pSocketObj, (PVOID*)&pSession);
  156. CLocalSafeCounter localcounter(*pSession);
  157. CBufferPtr& rcBuffer = *m_rcBuffers[SELF_THREAD_ID];
  158. return pSession->Receive(pData, iLength, rcBuffer.Ptr(), (int)rcBuffer.Size());
  159. }
  160. EnHandleResult CUdpArqServer::FireClose(TUdpSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  161. {
  162. EnHandleResult result = DoFireClose(pSocketObj, enOperation, iErrorCode);
  163. CArqSessionEx* pSession = nullptr;
  164. GetConnectionReserved(pSocketObj, (PVOID*)&pSession);
  165. if(pSession != nullptr)
  166. m_ssPool.PutFreeSession(pSession);
  167. return result;
  168. }
  169. BOOL CUdpArqServer::GetWaitingSendMessageCount(CONNID dwConnID, int& iCount)
  170. {
  171. TUdpSocketObj* pSocketObj = FindSocketObj(dwConnID);
  172. if(!TUdpSocketObj::IsValid(pSocketObj))
  173. {
  174. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  175. return FALSE;
  176. }
  177. CArqSessionEx* pSession = nullptr;
  178. GetConnectionReserved(pSocketObj, (PVOID*)&pSession);
  179. if(pSession == nullptr)
  180. {
  181. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  182. return FALSE;
  183. }
  184. {
  185. CLocalSafeCounter localcounter(*pSession);
  186. iCount = pSession->GetWaitingSend();
  187. }
  188. return (iCount >= 0);
  189. }
  190. #endif