Path.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_PATH_HPP
  14. #define ZT_PATH_HPP
  15. #include "Constants.hpp"
  16. #include "InetAddress.hpp"
  17. #include "SharedPtr.hpp"
  18. #include "Utils.hpp"
  19. #include "Mutex.hpp"
  20. #include "Meter.hpp"
  21. #include "Containers.hpp"
  22. namespace ZeroTier {
  23. class RuntimeEnvironment;
  24. template< unsigned int MF, unsigned int MFP, unsigned int GCT, unsigned int GCS, typename P >
  25. class Defragmenter;
  26. /**
  27. * A path across the physical network
  28. */
  29. class Path
  30. {
  31. friend class SharedPtr< Path >;
  32. // Allow defragmenter to access fragment-in-flight info stored in Path for performance reasons.
  33. template< unsigned int MF, unsigned int MFP, unsigned int GCT, unsigned int GCS, typename P >
  34. friend
  35. class Defragmenter;
  36. public:
  37. ZT_INLINE Path(const int64_t l, const InetAddress &r) noexcept:
  38. m_localSocket(l),
  39. m_lastIn(0),
  40. m_lastOut(0),
  41. m_latency(-1),
  42. m_addr(r)
  43. {}
  44. /**
  45. * Send a packet via this path (last out time is also updated)
  46. *
  47. * @param RR Runtime environment
  48. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  49. * @param data Packet data
  50. * @param len Packet length
  51. * @param now Current time
  52. * @return True if transport reported success
  53. */
  54. bool send(const RuntimeEnvironment *RR, void *tPtr, const void *data, unsigned int len, int64_t now) noexcept;
  55. /**
  56. * Explicitly update last sent time
  57. *
  58. * @param now Time of send
  59. * @param bytes Bytes sent
  60. */
  61. ZT_INLINE void sent(const int64_t now, const unsigned int bytes) noexcept
  62. {
  63. m_lastOut.store(now);
  64. m_outMeter.log(now, bytes);
  65. }
  66. /**
  67. * Called when a packet is received from this remote path, regardless of content
  68. *
  69. * @param now Time of receive
  70. * @param bytes Bytes received
  71. */
  72. ZT_INLINE void received(const int64_t now, const unsigned int bytes) noexcept
  73. {
  74. m_lastIn.store(now);
  75. m_inMeter.log(now, bytes);
  76. }
  77. /**
  78. * Update latency with a new measurement
  79. *
  80. * @param newMeasurement New latency measurement in milliseconds
  81. */
  82. ZT_INLINE void updateLatency(const unsigned int newMeasurement) noexcept
  83. {
  84. int lat = m_latency;
  85. if (likely(lat > 0)) {
  86. m_latency = (lat + newMeasurement) / 2;
  87. } else {
  88. m_latency = newMeasurement;
  89. }
  90. }
  91. /**
  92. * @return Latency in milliseconds or -1 if unknown
  93. */
  94. ZT_INLINE int latency() const noexcept
  95. { return m_latency; }
  96. /**
  97. * Check path aliveness
  98. *
  99. * @param now Current time
  100. */
  101. ZT_INLINE bool alive(const int64_t now) const noexcept
  102. { return ((now - m_lastIn.load()) < ZT_PATH_ALIVE_TIMEOUT); }
  103. /**
  104. * @return Physical address
  105. */
  106. ZT_INLINE const InetAddress &address() const noexcept
  107. { return m_addr; }
  108. /**
  109. * @return Local socket as specified by external code
  110. */
  111. ZT_INLINE int64_t localSocket() const noexcept
  112. { return m_localSocket; }
  113. /**
  114. * @return Last time we received anything
  115. */
  116. ZT_INLINE int64_t lastIn() const noexcept
  117. { return m_lastIn.load(); }
  118. /**
  119. * @return Last time we sent something
  120. */
  121. ZT_INLINE int64_t lastOut() const noexcept
  122. { return m_lastOut.load(); }
  123. private:
  124. const int64_t m_localSocket;
  125. std::atomic< int64_t > m_lastIn;
  126. std::atomic< int64_t > m_lastOut;
  127. std::atomic< int > m_latency;
  128. const InetAddress m_addr;
  129. Meter<> m_inMeter;
  130. Meter<> m_outMeter;
  131. // These fields belong to Defragmenter but are kept in Path for performance
  132. // as it's much faster this way than having Defragmenter maintain another
  133. // mapping from paths to inbound message IDs.
  134. Set< uint64_t > m_inboundFragmentedMessages;
  135. Mutex m_inboundFragmentedMessages_l;
  136. std::atomic< int > __refCount;
  137. };
  138. } // namespace ZeroTier
  139. #endif