UDPProxyCoordinator.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /// \file
  11. /// \brief Essentially maintains a list of servers running UDPProxyServer, and some state management for UDPProxyClient to find a free server to forward datagrams
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_UDPProxyCoordinator==1 && _RAKNET_SUPPORT_UDPForwarder==1
  15. #ifndef __UDP_PROXY_COORDINATOR_H
  16. #define __UDP_PROXY_COORDINATOR_H
  17. #include "Export.h"
  18. #include "RakNetTypes.h"
  19. #include "PluginInterface2.h"
  20. #include "RakString.h"
  21. #include "BitStream.h"
  22. #include "DS_Queue.h"
  23. #include "DS_OrderedList.h"
  24. namespace RakNet
  25. {
  26. /// When NAT Punchthrough fails, it is possible to use a non-NAT system to forward messages from us to the recipient, and vice-versa
  27. /// The class to forward messages is UDPForwarder, and it is triggered over the network via the UDPProxyServer plugin.
  28. /// The UDPProxyClient connects to UDPProxyCoordinator to get a list of servers running UDPProxyServer, and the coordinator will relay our forwarding request
  29. /// \brief Middleman between UDPProxyServer and UDPProxyClient, maintaining a list of UDPProxyServer, and managing state for clients to find an available forwarding server.
  30. /// \ingroup NAT_PUNCHTHROUGH_GROUP
  31. class RAK_DLL_EXPORT UDPProxyCoordinator : public PluginInterface2
  32. {
  33. public:
  34. // GetInstance() and DestroyInstance(instance*)
  35. STATIC_FACTORY_DECLARATIONS(UDPProxyCoordinator)
  36. UDPProxyCoordinator();
  37. virtual ~UDPProxyCoordinator();
  38. /// For UDPProxyServers logging in remotely, they must pass a password to UDPProxyServer::LoginToCoordinator(). It must match the password set here.
  39. /// If no password is set, they cannot login remotely.
  40. /// By default, no password is set
  41. void SetRemoteLoginPassword(RakNet::RakString password);
  42. /// \internal
  43. virtual void Update(void);
  44. virtual PluginReceiveResult OnReceive(Packet *packet);
  45. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
  46. struct SenderAndTargetAddress
  47. {
  48. SystemAddress senderClientAddress;
  49. RakNetGUID senderClientGuid;
  50. SystemAddress targetClientAddress;
  51. RakNetGUID targetClientGuid;
  52. };
  53. struct ServerWithPing
  54. {
  55. unsigned short ping;
  56. SystemAddress serverAddress;
  57. };
  58. struct ForwardingRequest
  59. {
  60. RakNet::TimeMS timeoutOnNoDataMS;
  61. RakNet::TimeMS timeoutAfterSuccess;
  62. SenderAndTargetAddress sata;
  63. SystemAddress requestingAddress; // Which system originally sent the network message to start forwarding
  64. SystemAddress currentlyAttemptedServerAddress;
  65. DataStructures::Queue<SystemAddress> remainingServersToTry;
  66. RakNet::BitStream serverSelectionBitstream;
  67. DataStructures::List<ServerWithPing> sourceServerPings, targetServerPings;
  68. RakNet::TimeMS timeRequestedPings;
  69. // Order based on sourceServerPings and targetServerPings
  70. void OrderRemainingServersToTry(void);
  71. };
  72. protected:
  73. static int ServerWithPingComp( const unsigned short &key, const UDPProxyCoordinator::ServerWithPing &data );
  74. static int ForwardingRequestComp( const SenderAndTargetAddress &key, ForwardingRequest* const &data);
  75. void OnForwardingRequestFromClientToCoordinator(Packet *packet);
  76. void OnLoginRequestFromServerToCoordinator(Packet *packet);
  77. void OnForwardingReplyFromServerToCoordinator(Packet *packet);
  78. void OnPingServersReplyFromClientToCoordinator(Packet *packet);
  79. void TryNextServer(SenderAndTargetAddress sata, ForwardingRequest *fw);
  80. void SendAllBusy(SystemAddress senderClientAddress, SystemAddress targetClientAddress, RakNetGUID targetClientGuid, SystemAddress requestingAddress);
  81. void Clear(void);
  82. void SendForwardingRequest(SystemAddress sourceAddress, SystemAddress targetAddress, SystemAddress serverAddress, RakNet::TimeMS timeoutOnNoDataMS);
  83. // Logged in servers
  84. //DataStructures::Multilist<ML_UNORDERED_LIST, SystemAddress> serverList;
  85. DataStructures::List<SystemAddress> serverList;
  86. // Forwarding requests in progress
  87. //DataStructures::Multilist<ML_ORDERED_LIST, ForwardingRequest*, SenderAndTargetAddress> forwardingRequestList;
  88. DataStructures::OrderedList<SenderAndTargetAddress, ForwardingRequest*, ForwardingRequestComp> forwardingRequestList;
  89. RakNet::RakString remoteLoginPassword;
  90. };
  91. } // End namespace
  92. #endif
  93. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号