Path.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: 2024-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 class Defragmenter;
  35. public:
  36. ZT_INLINE Path(const int64_t l,const InetAddress &r) noexcept :
  37. m_localSocket(l),
  38. m_lastIn(0),
  39. m_lastOut(0),
  40. m_latency(-1),
  41. m_addr(r)
  42. {
  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 (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 { return m_latency; }
  95. /**
  96. * Check path aliveness
  97. *
  98. * @param now Current time
  99. */
  100. ZT_INLINE bool alive(const int64_t now) const noexcept { return ((now - m_lastIn.load()) < ZT_PATH_ALIVE_TIMEOUT); }
  101. /**
  102. * @return Physical address
  103. */
  104. ZT_INLINE const InetAddress &address() const noexcept { return m_addr; }
  105. /**
  106. * @return Local socket as specified by external code
  107. */
  108. ZT_INLINE int64_t localSocket() const noexcept { return m_localSocket; }
  109. /**
  110. * @return Last time we received anything
  111. */
  112. ZT_INLINE int64_t lastIn() const noexcept { return m_lastIn.load(); }
  113. /**
  114. * @return Last time we sent something
  115. */
  116. ZT_INLINE int64_t lastOut() const noexcept { return m_lastOut.load(); }
  117. private:
  118. const int64_t m_localSocket;
  119. std::atomic<int64_t> m_lastIn;
  120. std::atomic<int64_t> m_lastOut;
  121. std::atomic<int> m_latency;
  122. const InetAddress m_addr;
  123. Meter<> m_inMeter;
  124. Meter<> m_outMeter;
  125. // These fields belong to Defragmenter but are kept in Path for performance
  126. // as it's much faster this way than having Defragmenter maintain another
  127. // mapping from paths to inbound message IDs.
  128. Set<uint64_t> m_inboundFragmentedMessages;
  129. Mutex m_inboundFragmentedMessages_l;
  130. std::atomic<int> __refCount;
  131. };
  132. } // namespace ZeroTier
  133. #endif