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