UDPForwarder.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 Forwards UDP datagrams. Independent of RakNet's protocol.
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_UDPForwarder==1
  15. #ifndef __UDP_FORWARDER_H
  16. #define __UDP_FORWARDER_H
  17. #include "Export.h"
  18. #include "RakNetTypes.h"
  19. #include "SocketIncludes.h"
  20. #include "UDPProxyCommon.h"
  21. #include "SimpleMutex.h"
  22. #include "RakString.h"
  23. #include "RakThread.h"
  24. #include "DS_Queue.h"
  25. #include "DS_OrderedList.h"
  26. #include "LocklessTypes.h"
  27. #include "DS_ThreadsafeAllocatingQueue.h"
  28. namespace RakNet
  29. {
  30. enum UDPForwarderResult
  31. {
  32. UDPFORWARDER_FORWARDING_ALREADY_EXISTS,
  33. UDPFORWARDER_NO_SOCKETS,
  34. UDPFORWARDER_BIND_FAILED,
  35. UDPFORWARDER_INVALID_PARAMETERS,
  36. UDPFORWARDER_NOT_RUNNING,
  37. UDPFORWARDER_SUCCESS,
  38. UDPFORWARDER_RESULT_COUNT
  39. };
  40. /// \brief Forwards UDP datagrams. Independent of RakNet's protocol.
  41. /// \ingroup NAT_PUNCHTHROUGH_GROUP
  42. class RAK_DLL_EXPORT UDPForwarder
  43. {
  44. public:
  45. UDPForwarder();
  46. virtual ~UDPForwarder();
  47. /// Starts the system.
  48. /// Required to call before StartForwarding
  49. void Startup(void);
  50. /// Stops the system, and frees all sockets
  51. void Shutdown(void);
  52. /// Sets the maximum number of forwarding entries allowed
  53. /// Set according to your available bandwidth and the estimated average bandwidth per forwarded address.
  54. /// \param[in] maxEntries The maximum number of simultaneous forwarding entries. Defaults to 64 (32 connections)
  55. void SetMaxForwardEntries(unsigned short maxEntries);
  56. /// \return The \a maxEntries parameter passed to SetMaxForwardEntries(), or the default if it was never called
  57. int GetMaxForwardEntries(void) const;
  58. /// \return How many entries have been used
  59. int GetUsedForwardEntries(void) const;
  60. /// Forwards datagrams from source to destination, and vice-versa
  61. /// Does nothing if this forward entry already exists via a previous call
  62. /// \pre Call Startup()
  63. /// \note RakNet's protocol will ensure a message is sent at least every 15 seconds, so if routing RakNet messages, it is a reasonable value for timeoutOnNoDataMS, plus an some extra seconds for latency
  64. /// \param[in] source The source IP and port
  65. /// \param[in] destination Where to forward to (and vice-versa)
  66. /// \param[in] timeoutOnNoDataMS If no messages are forwarded for this many MS, then automatically remove this entry.
  67. /// \param[in] forceHostAddress Force binding on a particular address. 0 to use any.
  68. /// \param[in] socketFamily IP version: For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC.
  69. /// \param[out] forwardingPort New opened port for forwarding
  70. /// \param[out] forwardingSocket New opened socket for forwarding
  71. /// \return UDPForwarderResult
  72. UDPForwarderResult StartForwarding(
  73. SystemAddress source, SystemAddress destination, RakNet::TimeMS timeoutOnNoDataMS,
  74. const char *forceHostAddress, unsigned short socketFamily,
  75. unsigned short *forwardingPort, __UDPSOCKET__ *forwardingSocket);
  76. /// No longer forward datagrams from source to destination
  77. /// \param[in] source The source IP and port
  78. /// \param[in] destination Where to forward to
  79. void StopForwarding(SystemAddress source, SystemAddress destination);
  80. struct ForwardEntry
  81. {
  82. ForwardEntry();
  83. ~ForwardEntry();
  84. SystemAddress addr1Unconfirmed, addr2Unconfirmed, addr1Confirmed, addr2Confirmed;
  85. RakNet::TimeMS timeLastDatagramForwarded;
  86. __UDPSOCKET__ socket;
  87. RakNet::TimeMS timeoutOnNoDataMS;
  88. short socketFamily;
  89. };
  90. protected:
  91. friend RAK_THREAD_DECLARATION(UpdateUDPForwarderGlobal);
  92. void UpdateUDPForwarder(void);
  93. void RecvFrom(RakNet::TimeMS curTime, ForwardEntry *forwardEntry);
  94. struct StartForwardingInputStruct
  95. {
  96. SystemAddress source;
  97. SystemAddress destination;
  98. RakNet::TimeMS timeoutOnNoDataMS;
  99. RakString forceHostAddress;
  100. unsigned short socketFamily;
  101. unsigned int inputId;
  102. };
  103. DataStructures::ThreadsafeAllocatingQueue<StartForwardingInputStruct> startForwardingInput;
  104. struct StartForwardingOutputStruct
  105. {
  106. unsigned short forwardingPort;
  107. __UDPSOCKET__ forwardingSocket;
  108. UDPForwarderResult result;
  109. unsigned int inputId;
  110. };
  111. DataStructures::Queue<StartForwardingOutputStruct> startForwardingOutput;
  112. SimpleMutex startForwardingOutputMutex;
  113. struct StopForwardingStruct
  114. {
  115. SystemAddress source;
  116. SystemAddress destination;
  117. };
  118. DataStructures::ThreadsafeAllocatingQueue<StopForwardingStruct> stopForwardingCommands;
  119. unsigned int nextInputId;
  120. // New entries are added to forwardListNotUpdated
  121. DataStructures::List<ForwardEntry*> forwardListNotUpdated;
  122. // SimpleMutex forwardListNotUpdatedMutex;
  123. unsigned short maxForwardEntries;
  124. RakNet::LocklessUint32_t isRunning, threadRunning;
  125. };
  126. } // End namespace
  127. #endif
  128. #endif // #if _RAKNET_SUPPORT_UDPForwarder==1
粤ICP备19079148号