xrpld
Loading...
Searching...
No Matches
SlotImp.cpp
1#include <xrpl/peerfinder/detail/SlotImp.h>
2
3#include <xrpl/beast/net/IPEndpoint.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/peerfinder/Slot.h>
6#include <xrpl/peerfinder/Types.h>
7#include <xrpl/peerfinder/detail/Tuning.h>
8
9#include <cstdint>
10#include <utility>
11
12namespace xrpl::peer_finder {
13
17 bool fixed,
18 clock_type& clock)
19 : recent(clock)
20 , inbound_(true)
21 , fixed_(fixed)
22 , reserved_(false)
23 , state_(State::Accept)
27 , checked(false)
28 , canAccept(false)
30{
31}
32
34 : recent(clock)
35 , inbound_(false)
36 , fixed_(fixed)
37 , reserved_(false)
38 , state_(State::Connect)
41 , checked(true)
42 , canAccept(true)
44{
45}
46
47void
49{
50 // Must go through activate() to set active state
51 XRPL_ASSERT(
52 state != State::Active, "xrpl::peer_finder::SlotImp::state : input state is not active");
53
54 // The state must be different
55 XRPL_ASSERT(
56 state_ != state,
57 "xrpl::peer_finder::SlotImp::state : input state is different from "
58 "current");
59
60 // You can't transition into the initial states
61 XRPL_ASSERT(
63 "xrpl::peer_finder::SlotImp::state : input state is not an initial");
64
65 // Can only become connected from outbound connect state
66 XRPL_ASSERT(
68 "xrpl::peer_finder::SlotImp::state : input state is not connected an "
69 "invalid state");
70
71 // Can't gracefully close on an outbound connection attempt
72 XRPL_ASSERT(
74 "xrpl::peer_finder::SlotImp::state : input state is not closing an "
75 "invalid state");
76
77 state_ = state;
78}
79
80void
82{
83 // Can only become active from the accept or connected state
84 XRPL_ASSERT(
86 "xrpl::peer_finder::SlotImp::activate : valid state");
87
90}
91
92//------------------------------------------------------------------------------
93
94Slot::~Slot() = default;
95
96//------------------------------------------------------------------------------
97
101
102void
104{
105 auto const result(cache_.emplace(ep, hops));
106 if (!result.second)
107 {
108 // NOTE Other logic depends on this <= inequality.
109 if (hops <= result.first->second)
110 {
111 result.first->second = hops;
112 cache_.touch(result.first);
113 }
114 }
115}
116
117bool
119{
120 auto const iter(cache_.find(ep));
121 if (iter == cache_.end())
122 return false;
123 // We avoid sending an endpoint if we heard it
124 // from them recently at the same or lower hop count.
125 // NOTE Other logic depends on this <= inequality.
126 return iter->second <= hops;
127}
128
129void
134
135} // namespace xrpl::peer_finder
std::chrono::steady_clock::time_point time_point
A version-independent IP address and port combination.
Definition IPEndpoint.h:24
bool filter(beast::ip::Endpoint const &ep, std::uint32_t hops)
Returns true if we should not send endpoint to the slot.
Definition SlotImp.cpp:118
void insert(beast::ip::Endpoint const &ep, std::uint32_t hops)
Called for each valid endpoint received for a slot.
Definition SlotImp.cpp:103
beast::aged_unordered_map< beast::ip::Endpoint, std::uint32_t > cache_
Definition SlotImp.h:156
std::atomic< std::int32_t > listeningPort_
Definition SlotImp.h:175
class xrpl::peer_finder::SlotImp::RecentT recent
std::optional< beast::ip::Endpoint > const & localEndpoint() const override
The local endpoint of the socket, when known.
Definition SlotImp.h:63
State state() const override
Returns the state of the connection.
Definition SlotImp.h:51
beast::ip::Endpoint const & remoteEndpoint() const override
The remote endpoint of socket.
Definition SlotImp.h:57
clock_type::time_point whenAcceptEndpoints
Definition SlotImp.h:196
SlotImp(beast::ip::Endpoint const &localEndpoint, beast::ip::Endpoint remoteEndpoint, bool fixed, clock_type &clock)
Definition SlotImp.cpp:14
void activate(clock_type::time_point const &now)
Definition SlotImp.cpp:81
std::optional< beast::ip::Endpoint > localEndpoint_
Definition SlotImp.h:171
bool fixed() const override
Returns true if this is a fixed connection.
Definition SlotImp.h:39
static std::int32_t constexpr kUnknownPort
Definition SlotImp.h:174
beast::ip::Endpoint remoteEndpoint_
Definition SlotImp.h:170
std::size_t expire(AgedContainer &c, std::chrono::duration< Rep, Period > const &age)
Expire aged container items past the specified age.
STL namespace.
constexpr std::chrono::seconds kLiveCacheSecondsToLive(30)
beast::AbstractClock< std::chrono::steady_clock > clock_type