UDPProxyServer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 A RakNet plugin performing networking to communicate with UDPProxyServer. It allows UDPProxyServer to control our instance of UDPForwarder.
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_UDPProxyServer==1 && _RAKNET_SUPPORT_UDPForwarder==1
  15. #ifndef __UDP_PROXY_SERVER_H
  16. #define __UDP_PROXY_SERVER_H
  17. #include "Export.h"
  18. #include "RakNetTypes.h"
  19. #include "PluginInterface2.h"
  20. #include "UDPForwarder.h"
  21. #include "RakString.h"
  22. namespace RakNet
  23. {
  24. class UDPProxyServer;
  25. /// Callback to handle results of calling UDPProxyServer::LoginToCoordinator()
  26. /// \ingroup UDP_PROXY_GROUP
  27. struct UDPProxyServerResultHandler
  28. {
  29. UDPProxyServerResultHandler() {}
  30. virtual ~UDPProxyServerResultHandler() {}
  31. /// Called when our login succeeds
  32. /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator()
  33. /// \param[out] proxyServer The plugin calling this callback
  34. virtual void OnLoginSuccess(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0;
  35. /// We are already logged in.
  36. /// This login failed, but the system is operational as if it succeeded
  37. /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator()
  38. /// \param[out] proxyServer The plugin calling this callback
  39. virtual void OnAlreadyLoggedIn(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0;
  40. /// The coordinator operator forgot to call UDPProxyCoordinator::SetRemoteLoginPassword()
  41. /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator()
  42. /// \param[out] proxyServer The plugin calling this callback
  43. virtual void OnNoPasswordSet(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0;
  44. /// The coordinator operator set a different password in UDPProxyCoordinator::SetRemoteLoginPassword() than what we passed
  45. /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator()
  46. /// \param[out] proxyServer The plugin calling this callback
  47. virtual void OnWrongPassword(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0;
  48. };
  49. /// \brief UDPProxyServer to control our instance of UDPForwarder
  50. /// \details When NAT Punchthrough fails, it is possible to use a non-NAT system to forward messages from us to the recipient, and vice-versa.<BR>
  51. /// The class to forward messages is UDPForwarder, and it is triggered over the network via the UDPProxyServer plugin.<BR>
  52. /// The UDPProxyServer connects to UDPProxyServer to get a list of servers running UDPProxyServer, and the coordinator will relay our forwarding request.
  53. /// \ingroup UDP_PROXY_GROUP
  54. class RAK_DLL_EXPORT UDPProxyServer : public PluginInterface2
  55. {
  56. public:
  57. // GetInstance() and DestroyInstance(instance*)
  58. STATIC_FACTORY_DECLARATIONS(UDPProxyServer)
  59. UDPProxyServer();
  60. ~UDPProxyServer();
  61. /// Sets the socket family to use, either IPV4 or IPV6
  62. /// \param[in] socketFamily For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC.
  63. void SetSocketFamily(unsigned short _socketFamily);
  64. /// Receives the results of calling LoginToCoordinator()
  65. /// Set before calling LoginToCoordinator or you won't know what happened
  66. /// \param[in] resultHandler
  67. void SetResultHandler(UDPProxyServerResultHandler *rh);
  68. /// Before the coordinator will register the UDPProxyServer, you must login
  69. /// \pre Must be connected to the coordinator
  70. /// \pre Coordinator must have set a password with UDPProxyCoordinator::SetRemoteLoginPassword()
  71. /// \returns false if already logged in, or logging in. Returns true otherwise
  72. bool LoginToCoordinator(RakNet::RakString password, SystemAddress coordinatorAddress);
  73. /// \brief The server IP reported to the client is the IP address from the server to the coordinator.
  74. /// If the server and coordinator are on the same LAN, you need to call SetServerPublicIP() to tell the client what address to connect to
  75. /// \param[in] ip IP address to report in UDPProxyClientResultHandler::OnForwardingSuccess() and UDPProxyClientResultHandler::OnForwardingNotification() as proxyIPAddress
  76. void SetServerPublicIP(RakString ip);
  77. /// Operative class that performs the forwarding
  78. /// Exposed so you can call UDPForwarder::SetMaxForwardEntries() if you want to change away from the default
  79. /// UDPForwarder::Startup(), UDPForwarder::Shutdown(), and UDPForwarder::Update() are called automatically by the plugin
  80. UDPForwarder udpForwarder;
  81. virtual void OnAttach(void);
  82. virtual void OnDetach(void);
  83. /// \internal
  84. virtual void Update(void);
  85. virtual PluginReceiveResult OnReceive(Packet *packet);
  86. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
  87. virtual void OnRakPeerStartup(void);
  88. virtual void OnRakPeerShutdown(void);
  89. protected:
  90. void OnForwardingRequestFromCoordinatorToServer(Packet *packet);
  91. DataStructures::OrderedList<SystemAddress, SystemAddress> loggingInCoordinators;
  92. DataStructures::OrderedList<SystemAddress, SystemAddress> loggedInCoordinators;
  93. UDPProxyServerResultHandler *resultHandler;
  94. unsigned short socketFamily;
  95. RakString serverPublicIp;
  96. };
  97. } // End namespace
  98. #endif
  99. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号