UDPProxyServer.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "NativeFeatureIncludes.h"
  11. #if _RAKNET_SUPPORT_UDPProxyServer==1 && _RAKNET_SUPPORT_UDPForwarder==1
  12. #include "UDPProxyServer.h"
  13. #include "BitStream.h"
  14. #include "UDPProxyCommon.h"
  15. #include "RakPeerInterface.h"
  16. #include "MessageIdentifiers.h"
  17. using namespace RakNet;
  18. STATIC_FACTORY_DEFINITIONS(UDPProxyServer,UDPProxyServer);
  19. UDPProxyServer::UDPProxyServer()
  20. {
  21. resultHandler=0;
  22. socketFamily=AF_INET;
  23. }
  24. UDPProxyServer::~UDPProxyServer()
  25. {
  26. }
  27. void UDPProxyServer::SetSocketFamily(unsigned short _socketFamily)
  28. {
  29. socketFamily=_socketFamily;
  30. }
  31. void UDPProxyServer::SetResultHandler(UDPProxyServerResultHandler *rh)
  32. {
  33. resultHandler=rh;
  34. }
  35. bool UDPProxyServer::LoginToCoordinator(RakNet::RakString password, SystemAddress coordinatorAddress)
  36. {
  37. unsigned int insertionIndex;
  38. bool objectExists;
  39. insertionIndex=loggingInCoordinators.GetIndexFromKey(coordinatorAddress,&objectExists);
  40. if (objectExists==true)
  41. return false;
  42. loggedInCoordinators.GetIndexFromKey(coordinatorAddress,&objectExists);
  43. if (objectExists==true)
  44. return false;
  45. RakNet::BitStream outgoingBs;
  46. outgoingBs.Write((MessageID)ID_UDP_PROXY_GENERAL);
  47. outgoingBs.Write((MessageID)ID_UDP_PROXY_LOGIN_REQUEST_FROM_SERVER_TO_COORDINATOR);
  48. outgoingBs.Write(password);
  49. rakPeerInterface->Send(&outgoingBs, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, coordinatorAddress, false);
  50. loggingInCoordinators.InsertAtIndex(coordinatorAddress, insertionIndex, _FILE_AND_LINE_ );
  51. return true;
  52. }
  53. void UDPProxyServer::SetServerPublicIP(RakString ip)
  54. {
  55. serverPublicIp = ip;
  56. }
  57. void UDPProxyServer::Update(void)
  58. {
  59. }
  60. PluginReceiveResult UDPProxyServer::OnReceive(Packet *packet)
  61. {
  62. // Make sure incoming messages from from UDPProxyCoordinator
  63. if (packet->data[0]==ID_UDP_PROXY_GENERAL && packet->length>1)
  64. {
  65. bool objectExists;
  66. switch (packet->data[1])
  67. {
  68. case ID_UDP_PROXY_FORWARDING_REQUEST_FROM_COORDINATOR_TO_SERVER:
  69. if (loggedInCoordinators.GetIndexFromKey(packet->systemAddress, &objectExists)!=(unsigned int)-1)
  70. {
  71. OnForwardingRequestFromCoordinatorToServer(packet);
  72. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  73. }
  74. break;
  75. case ID_UDP_PROXY_NO_PASSWORD_SET_FROM_COORDINATOR_TO_SERVER:
  76. case ID_UDP_PROXY_WRONG_PASSWORD_FROM_COORDINATOR_TO_SERVER:
  77. case ID_UDP_PROXY_ALREADY_LOGGED_IN_FROM_COORDINATOR_TO_SERVER:
  78. case ID_UDP_PROXY_LOGIN_SUCCESS_FROM_COORDINATOR_TO_SERVER:
  79. {
  80. unsigned int removalIndex = loggingInCoordinators.GetIndexFromKey(packet->systemAddress, &objectExists);
  81. if (objectExists)
  82. {
  83. loggingInCoordinators.RemoveAtIndex(removalIndex);
  84. RakNet::BitStream incomingBs(packet->data, packet->length, false);
  85. incomingBs.IgnoreBytes(2);
  86. RakNet::RakString password;
  87. incomingBs.Read(password);
  88. switch (packet->data[1])
  89. {
  90. case ID_UDP_PROXY_NO_PASSWORD_SET_FROM_COORDINATOR_TO_SERVER:
  91. if (resultHandler)
  92. resultHandler->OnNoPasswordSet(password, this);
  93. break;
  94. case ID_UDP_PROXY_WRONG_PASSWORD_FROM_COORDINATOR_TO_SERVER:
  95. if (resultHandler)
  96. resultHandler->OnWrongPassword(password, this);
  97. break;
  98. case ID_UDP_PROXY_ALREADY_LOGGED_IN_FROM_COORDINATOR_TO_SERVER:
  99. if (resultHandler)
  100. resultHandler->OnAlreadyLoggedIn(password, this);
  101. break;
  102. case ID_UDP_PROXY_LOGIN_SUCCESS_FROM_COORDINATOR_TO_SERVER:
  103. // RakAssert(loggedInCoordinators.GetIndexOf(packet->systemAddress)==(unsigned int)-1);
  104. loggedInCoordinators.Insert(packet->systemAddress, packet->systemAddress, true, _FILE_AND_LINE_);
  105. if (resultHandler)
  106. resultHandler->OnLoginSuccess(password, this);
  107. break;
  108. }
  109. }
  110. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  111. }
  112. }
  113. }
  114. return RR_CONTINUE_PROCESSING;
  115. }
  116. void UDPProxyServer::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
  117. {
  118. (void) lostConnectionReason;
  119. (void) rakNetGUID;
  120. loggingInCoordinators.RemoveIfExists(systemAddress);
  121. loggedInCoordinators.RemoveIfExists(systemAddress);
  122. }
  123. void UDPProxyServer::OnRakPeerStartup(void)
  124. {
  125. udpForwarder.Startup();
  126. }
  127. void UDPProxyServer::OnRakPeerShutdown(void)
  128. {
  129. udpForwarder.Shutdown();
  130. loggingInCoordinators.Clear(true,_FILE_AND_LINE_);
  131. loggedInCoordinators.Clear(true,_FILE_AND_LINE_);
  132. }
  133. void UDPProxyServer::OnAttach(void)
  134. {
  135. if (rakPeerInterface->IsActive())
  136. OnRakPeerStartup();
  137. }
  138. void UDPProxyServer::OnDetach(void)
  139. {
  140. OnRakPeerShutdown();
  141. }
  142. void UDPProxyServer::OnForwardingRequestFromCoordinatorToServer(Packet *packet)
  143. {
  144. SystemAddress sourceAddress, targetAddress;
  145. RakNet::BitStream incomingBs(packet->data, packet->length, false);
  146. incomingBs.IgnoreBytes(2);
  147. incomingBs.Read(sourceAddress);
  148. incomingBs.Read(targetAddress);
  149. RakNet::TimeMS timeoutOnNoDataMS;
  150. incomingBs.Read(timeoutOnNoDataMS);
  151. RakAssert(timeoutOnNoDataMS > 0 && timeoutOnNoDataMS <= UDP_FORWARDER_MAXIMUM_TIMEOUT);
  152. unsigned short forwardingPort=0;
  153. UDPForwarderResult success = udpForwarder.StartForwarding(sourceAddress, targetAddress, timeoutOnNoDataMS, 0, socketFamily, &forwardingPort, 0);
  154. RakNet::BitStream outgoingBs;
  155. outgoingBs.Write((MessageID)ID_UDP_PROXY_GENERAL);
  156. outgoingBs.Write((MessageID)ID_UDP_PROXY_FORWARDING_REPLY_FROM_SERVER_TO_COORDINATOR);
  157. outgoingBs.Write(sourceAddress);
  158. outgoingBs.Write(targetAddress);
  159. outgoingBs.Write(serverPublicIp);
  160. outgoingBs.Write((unsigned char) success);
  161. outgoingBs.Write(forwardingPort);
  162. rakPeerInterface->Send(&outgoingBs, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, packet->systemAddress, false);
  163. }
  164. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号