xrpld
Loading...
Searching...
No Matches
Checker.h
1#pragma once
2
3#include <xrpl/beast/net/IPAddressConversion.h>
4
5#include <boost/asio/io_context.hpp>
6#include <boost/asio/ip/tcp.hpp>
7#include <boost/intrusive/list.hpp>
8
9#include <condition_variable>
10#include <memory>
11#include <mutex>
12
13namespace xrpl::PeerFinder {
14
16template <class Protocol = boost::asio::ip::tcp>
18{
19private:
20 using error_code = boost::system::error_code;
21
22 struct BasicAsyncOp : boost::intrusive::list_base_hook<
23 boost::intrusive::link_mode<boost::intrusive::normal_link>>
24 {
25 virtual ~BasicAsyncOp() = default;
26
27 virtual void
28 stop() = 0;
29
30 virtual void
31 operator()(error_code const& ec) = 0;
32 };
33
34 template <class Handler>
36 {
37 using socket_type = Protocol::socket;
38 using endpoint_type = Protocol::endpoint;
39
42 Handler handler;
43
44 AsyncOp(Checker& owner, boost::asio::io_context& ioContext, Handler&& handler);
45
46 ~AsyncOp() override
47 {
48 checker.remove(*this);
49 }
50
51 void
52 stop() override;
53
54 void
55 operator()(error_code const& ec) override; // NOLINT(readability-identifier-naming)
56 };
57
58 //--------------------------------------------------------------------------
59
60 using list_type =
61 boost::intrusive::make_list<BasicAsyncOp, boost::intrusive::constant_time_size<true>>::type;
62
65 boost::asio::io_context& ioContext_;
67 bool stop_ = false;
68
69public:
70 explicit Checker(boost::asio::io_context& ioContext);
71
79
86 void
88
90 void
92
97 template <class Handler>
98 void
99 asyncConnect(beast::IP::Endpoint const& endpoint, Handler&& handler);
100
101private:
102 void
103 remove(BasicAsyncOp& op);
104};
105
106//------------------------------------------------------------------------------
107
108template <class Protocol>
109template <class Handler>
111 Checker& owner,
112 boost::asio::io_context& ioContext,
113 Handler&&
114 handler) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved) -- forwarded in init
115 : checker(owner), socket(ioContext), handler(std::forward<Handler>(handler))
116{
117}
118
119template <class Protocol>
120template <class Handler>
121void
127
128template <class Protocol>
129template <class Handler>
130void
135
136//------------------------------------------------------------------------------
137
138template <class Protocol>
139Checker<Protocol>::Checker(boost::asio::io_context& ioContext) : ioContext_(ioContext)
140{
141}
142
143template <class Protocol>
148
149template <class Protocol>
150void
152{
153 std::scoped_lock const lock(mutex_);
154 if (!stop_)
155 {
156 stop_ = true;
157 for (auto& c : list_)
158 c.stop();
159 }
160}
161
162template <class Protocol>
163void
165{
167 while (!list_.empty())
168 cond_.wait(lock);
169}
170
171template <class Protocol>
172template <class Handler>
173void
174Checker<Protocol>::asyncConnect(beast::IP::Endpoint const& endpoint, Handler&& handler)
175{
176 auto const op =
178 {
179 std::scoped_lock const lock(mutex_);
180 list_.push_back(*op);
181 }
182 op->socket.async_connect(
184 std::bind(&BasicAsyncOp::operator(), op, std::placeholders::_1));
185}
186
187template <class Protocol>
188void
190{
191 std::scoped_lock const lock(mutex_);
192 list_.erase(list_.iterator_to(op));
193 if (list_.size() == 0)
194 cond_.notify_all();
195}
196
197} // namespace xrpl::PeerFinder
T bind(T... args)
A version-independent IP address and port combination.
Definition IPEndpoint.h:17
boost::system::error_code error_code
Definition Checker.h:20
void asyncConnect(beast::IP::Endpoint const &endpoint, Handler &&handler)
Performs an async connection test on the specified endpoint.
Definition Checker.h:174
void stop()
Stop the service.
Definition Checker.h:151
Checker(boost::asio::io_context &ioContext)
Definition Checker.h:139
boost::intrusive::make_list< BasicAsyncOp, boost::intrusive::constant_time_size< true > >::type list_type
Definition Checker.h:60
std::condition_variable cond_
Definition Checker.h:64
boost::asio::io_context & ioContext_
Definition Checker.h:65
void remove(BasicAsyncOp &op)
Definition Checker.h:189
void wait()
Block until all pending I/O completes.
Definition Checker.h:164
~Checker()
Destroy the service.
Definition Checker.h:144
T forward(T... args)
T make_shared(T... args)
STL namespace.
static boost::asio::ip::tcp::endpoint toAsioEndpoint(IP::Endpoint const &address)
void operator()(error_code const &ec) override
Definition Checker.h:131
AsyncOp(Checker &owner, boost::asio::io_context &ioContext, Handler &&handler)
Definition Checker.h:110
Protocol::endpoint endpoint_type
Definition Checker.h:38
virtual void operator()(error_code const &ec)=0