xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
peerfinder
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
12
namespace
xrpl::peer_finder
{
13
14
SlotImp::SlotImp
(
15
beast::ip::Endpoint
const
&
localEndpoint
,
16
beast::ip::Endpoint
remoteEndpoint
,
17
bool
fixed
,
18
ClockType
& clock)
19
:
recent
(clock)
20
,
inbound_
(true)
21
,
fixed_
(
fixed
)
22
,
reserved_
(false)
23
,
state_
(
State
::Accept)
24
,
remoteEndpoint_
(
std
::move(
remoteEndpoint
))
25
,
localEndpoint_
(
localEndpoint
)
26
,
listeningPort_
(
kUnknownPort
)
27
,
checked
(false)
28
,
canAccept
(false)
29
,
connectivityCheckInProgress
(false)
30
{
31
}
32
33
SlotImp::SlotImp
(
beast::ip::Endpoint
remoteEndpoint
,
bool
fixed
,
ClockType
& clock)
34
:
recent
(clock)
35
,
inbound_
(false)
36
,
fixed_
(
fixed
)
37
,
reserved_
(false)
38
,
state_
(
State
::Connect)
39
,
remoteEndpoint_
(
std
::move(
remoteEndpoint
))
40
,
listeningPort_
(
kUnknownPort
)
41
,
checked
(true)
42
,
canAccept
(true)
43
,
connectivityCheckInProgress
(false)
44
{
45
}
46
47
void
48
SlotImp::state
(
State
state
)
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(
62
state
!=
State::Accept
&&
state
!=
State::Connect
,
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(
67
state
!=
State::Connected
|| (!
inbound_
&&
state_
==
State::Connect
),
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(
73
state
!=
State::Closing
||
state_
!=
State::Connect
,
74
"xrpl::peer_finder::SlotImp::state : input state is not closing an "
75
"invalid state"
);
76
77
state_
=
state
;
78
}
79
80
void
81
SlotImp::activate
(
ClockType::time_point
const
& now)
82
{
83
// Can only become active from the accept or connected state
84
XRPL_ASSERT(
85
state_
==
State::Accept
||
state_
==
State::Connected
,
86
"xrpl::peer_finder::SlotImp::activate : valid state"
);
87
88
state_
=
State::Active
;
89
whenAcceptEndpoints
= now;
90
}
91
92
//------------------------------------------------------------------------------
93
94
Slot::~Slot
() =
default
;
95
96
//------------------------------------------------------------------------------
97
98
SlotImp::RecentT::RecentT
(
ClockType
& clock) :
cache_
(clock)
99
{
100
}
101
102
void
103
SlotImp::RecentT::insert
(
beast::ip::Endpoint
const
& ep,
std::uint32_t
hops)
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
117
bool
118
SlotImp::RecentT::filter
(
beast::ip::Endpoint
const
& ep,
std::uint32_t
hops)
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
129
void
130
SlotImp::RecentT::expire
()
131
{
132
beast::expire
(
cache_
,
tuning::kLiveCacheSecondsToLive
);
133
}
134
135
}
// namespace xrpl::peer_finder
beast::AbstractClock< std::chrono::steady_clock >::time_point
std::chrono::steady_clock::time_point time_point
Definition
abstract_clock.h:41
beast::ip::Endpoint
A version-independent IP address and port combination.
Definition
IPEndpoint.h:24
xrpl::peer_finder::SlotImp::RecentT::filter
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
xrpl::peer_finder::SlotImp::RecentT::cache_
beast::AgedUnorderedMap< beast::ip::Endpoint, std::uint32_t > cache_
Definition
SlotImp.h:156
xrpl::peer_finder::SlotImp::RecentT::insert
void insert(beast::ip::Endpoint const &ep, std::uint32_t hops)
Called for each valid endpoint received for a slot.
Definition
SlotImp.cpp:103
xrpl::peer_finder::SlotImp::RecentT::expire
void expire()
Definition
SlotImp.cpp:130
xrpl::peer_finder::SlotImp::RecentT::RecentT
RecentT(ClockType &clock)
Definition
SlotImp.cpp:98
xrpl::peer_finder::SlotImp::checked
bool checked
Definition
SlotImp.h:182
xrpl::peer_finder::SlotImp::listeningPort_
std::atomic< std::int32_t > listeningPort_
Definition
SlotImp.h:175
xrpl::peer_finder::SlotImp::connectivityCheckInProgress
bool connectivityCheckInProgress
Definition
SlotImp.h:190
xrpl::peer_finder::SlotImp::reserved_
bool reserved_
Definition
SlotImp.h:168
xrpl::peer_finder::SlotImp::SlotImp
SlotImp(beast::ip::Endpoint const &localEndpoint, beast::ip::Endpoint remoteEndpoint, bool fixed, ClockType &clock)
Definition
SlotImp.cpp:14
xrpl::peer_finder::SlotImp::recent
class xrpl::peer_finder::SlotImp::RecentT recent
xrpl::peer_finder::SlotImp::localEndpoint
std::optional< beast::ip::Endpoint > const & localEndpoint() const override
The local endpoint of the socket, when known.
Definition
SlotImp.h:63
xrpl::peer_finder::SlotImp::state_
State state_
Definition
SlotImp.h:169
xrpl::peer_finder::SlotImp::state
State state() const override
Returns the state of the connection.
Definition
SlotImp.h:51
xrpl::peer_finder::SlotImp::remoteEndpoint
beast::ip::Endpoint const & remoteEndpoint() const override
The remote endpoint of socket.
Definition
SlotImp.h:57
xrpl::peer_finder::SlotImp::inbound_
bool const inbound_
Definition
SlotImp.h:166
xrpl::peer_finder::SlotImp::canAccept
bool canAccept
Definition
SlotImp.h:186
xrpl::peer_finder::SlotImp::localEndpoint_
std::optional< beast::ip::Endpoint > localEndpoint_
Definition
SlotImp.h:171
xrpl::peer_finder::SlotImp::activate
void activate(ClockType::time_point const &now)
Definition
SlotImp.cpp:81
xrpl::peer_finder::SlotImp::fixed
bool fixed() const override
Returns true if this is a fixed connection.
Definition
SlotImp.h:39
xrpl::peer_finder::SlotImp::whenAcceptEndpoints
ClockType::time_point whenAcceptEndpoints
Definition
SlotImp.h:196
xrpl::peer_finder::SlotImp::kUnknownPort
static std::int32_t constexpr kUnknownPort
Definition
SlotImp.h:174
xrpl::peer_finder::SlotImp::fixed_
bool const fixed_
Definition
SlotImp.h:167
xrpl::peer_finder::SlotImp::remoteEndpoint_
beast::ip::Endpoint remoteEndpoint_
Definition
SlotImp.h:170
xrpl::peer_finder::Slot::~Slot
virtual ~Slot()=0
xrpl::peer_finder::Slot::State
State
Definition
include/xrpl/peerfinder/Slot.h:20
xrpl::peer_finder::Slot::State::Connected
@ Connected
Definition
include/xrpl/peerfinder/Slot.h:20
xrpl::peer_finder::Slot::State::Connect
@ Connect
Definition
include/xrpl/peerfinder/Slot.h:20
xrpl::peer_finder::Slot::State::Active
@ Active
Definition
include/xrpl/peerfinder/Slot.h:20
xrpl::peer_finder::Slot::State::Closing
@ Closing
Definition
include/xrpl/peerfinder/Slot.h:20
xrpl::peer_finder::Slot::State::Accept
@ Accept
Definition
include/xrpl/peerfinder/Slot.h:20
cstdint
std::uint32_t
beast::expire
std::size_t expire(AgedContainer &c, std::chrono::duration< Rep, Period > const &age)
Expire aged container items past the specified age.
Definition
aged_container_utility.h:15
std
STL namespace.
xrpl::peer_finder::tuning::kLiveCacheSecondsToLive
constexpr std::chrono::seconds kLiveCacheSecondsToLive(30)
xrpl::peer_finder
Definition
include/xrpl/peerfinder/Config.h:12
xrpl::peer_finder::ClockType
beast::AbstractClock< std::chrono::steady_clock > ClockType
Definition
peerfinder/Types.h:13
utility
Generated by
1.17.0