xrpld
Loading...
Searching...
No Matches
SlotImp.cpp
1#include <xrpld/peerfinder/detail/SlotImp.h>
2
3#include <xrpld/peerfinder/PeerfinderManager.h>
4#include <xrpld/peerfinder/Slot.h>
5#include <xrpld/peerfinder/detail/Tuning.h>
6
7#include <xrpl/beast/container/detail/aged_unordered_container.h>
8#include <xrpl/beast/net/IPEndpoint.h>
9#include <xrpl/beast/utility/instrumentation.h>
10
11#include <cstdint>
12#include <utility>
13
14namespace xrpl::PeerFinder {
15
19 bool fixed,
20 clock_type& clock)
21 : recent(clock)
22 , inbound_(true)
23 , fixed_(fixed)
24 , reserved_(false)
25 , state_(State::Accept)
29 , checked(false)
30 , canAccept(false)
32{
33}
34
36 : recent(clock)
37 , inbound_(false)
38 , fixed_(fixed)
39 , reserved_(false)
40 , state_(State::Connect)
43 , checked(true)
44 , canAccept(true)
46{
47}
48
49void
51{
52 // Must go through activate() to set active state
53 XRPL_ASSERT(
54 state != State::Active, "xrpl::PeerFinder::SlotImp::state : input state is not active");
55
56 // The state must be different
57 XRPL_ASSERT(
58 state_ != state,
59 "xrpl::PeerFinder::SlotImp::state : input state is different from "
60 "current");
61
62 // You can't transition into the initial states
63 XRPL_ASSERT(
65 "xrpl::PeerFinder::SlotImp::state : input state is not an initial");
66
67 // Can only become connected from outbound connect state
68 XRPL_ASSERT(
70 "xrpl::PeerFinder::SlotImp::state : input state is not connected an "
71 "invalid state");
72
73 // Can't gracefully close on an outbound connection attempt
74 XRPL_ASSERT(
76 "xrpl::PeerFinder::SlotImp::state : input state is not closing an "
77 "invalid state");
78
79 state_ = state;
80}
81
82void
84{
85 // Can only become active from the accept or connected state
86 XRPL_ASSERT(
88 "xrpl::PeerFinder::SlotImp::activate : valid state");
89
92}
93
94//------------------------------------------------------------------------------
95
96Slot::~Slot() = default;
97
98//------------------------------------------------------------------------------
99
101{
102}
103
104void
106{
107 auto const result(cache_.emplace(ep, hops));
108 if (!result.second)
109 {
110 // NOTE Other logic depends on this <= inequality.
111 if (hops <= result.first->second)
112 {
113 result.first->second = hops;
114 cache_.touch(result.first);
115 }
116 }
117}
118
119bool
121{
122 auto const iter(cache_.find(ep));
123 if (iter == cache_.end())
124 return false;
125 // We avoid sending an endpoint if we heard it
126 // from them recently at the same or lower hop count.
127 // NOTE Other logic depends on this <= inequality.
128 return iter->second <= hops;
129}
130
131void
136
137} // namespace xrpl::PeerFinder
std::chrono::steady_clock::time_point time_point
A version-independent IP address and port combination.
Definition IPEndpoint.h:17
beast::aged_unordered_map< beast::IP::Endpoint, std::uint32_t > cache_
Definition SlotImp.h:149
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:120
void insert(beast::IP::Endpoint const &ep, std::uint32_t hops)
Called for each valid endpoint received for a slot.
Definition SlotImp.cpp:105
static constexpr std::int32_t kUnknownPort
Definition SlotImp.h:167
bool fixed() const override
Returns true if this is a fixed connection.
Definition SlotImp.h:35
class xrpl::PeerFinder::SlotImp::RecentT recent
SlotImp(beast::IP::Endpoint const &localEndpoint, beast::IP::Endpoint remoteEndpoint, bool fixed, clock_type &clock)
Definition SlotImp.cpp:16
std::atomic< std::int32_t > listeningPort_
Definition SlotImp.h:168
std::optional< beast::IP::Endpoint > const & localEndpoint() const override
The local endpoint of the socket, when known.
Definition SlotImp.h:59
State state() const override
Returns the state of the connection.
Definition SlotImp.h:47
clock_type::time_point whenAcceptEndpoints
Definition SlotImp.h:189
beast::IP::Endpoint const & remoteEndpoint() const override
The remote endpoint of socket.
Definition SlotImp.h:53
beast::IP::Endpoint remoteEndpoint_
Definition SlotImp.h:163
std::optional< beast::IP::Endpoint > localEndpoint_
Definition SlotImp.h:164
void activate(clock_type::time_point const &now)
Definition SlotImp.cpp:83
std::enable_if_t< IsAgedContainer< AgedContainer >::value, 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