rippled
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 {
14namespace PeerFinder {
15
17template <class Protocol = boost::asio::ip::tcp>
19{
20private:
21 using error_code = boost::system::error_code;
22
23 struct basic_async_op : boost::intrusive::list_base_hook<
24 boost::intrusive::link_mode<boost::intrusive::normal_link>>
25 {
26 virtual ~basic_async_op() = default;
27
28 virtual void
29 stop() = 0;
30
31 virtual void
32 operator()(error_code const& ec) = 0;
33 };
34
35 template <class Handler>
37 {
38 using socket_type = typename Protocol::socket;
39 using endpoint_type = typename Protocol::endpoint;
40
43 Handler handler_;
44
45 async_op(Checker& owner, boost::asio::io_context& io_context, Handler&& handler);
46
47 virtual ~async_op();
48
49 void
50 stop() override;
51
52 void
53 operator()(error_code const& ec) override;
54 };
55
56 //--------------------------------------------------------------------------
57
58 using list_type = typename boost::intrusive::
59 make_list<basic_async_op, boost::intrusive::constant_time_size<true>>::type;
60
63 boost::asio::io_context& io_context_;
65 bool stop_ = false;
66
67public:
68 explicit Checker(boost::asio::io_context& io_context);
69
77
84 void
86
88 void
90
95 template <class Handler>
96 void
97 async_connect(beast::IP::Endpoint const& endpoint, Handler&& handler);
98
99private:
100 void
101 remove(basic_async_op& op);
102};
103
104//------------------------------------------------------------------------------
105
106template <class Protocol>
107template <class Handler>
109 Checker& owner,
110 boost::asio::io_context& io_context,
111 Handler&& handler) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
112 : checker_(owner), socket_(io_context), handler_(std::forward<Handler>(handler))
113{
114}
115
116template <class Protocol>
117template <class Handler>
122
123template <class Protocol>
124template <class Handler>
125void
127{
128 error_code ec;
129 socket_.cancel(ec);
130}
131
132template <class Protocol>
133template <class Handler>
134void
139
140//------------------------------------------------------------------------------
141
142template <class Protocol>
143Checker<Protocol>::Checker(boost::asio::io_context& io_context) : io_context_(io_context)
144{
145}
146
147template <class Protocol>
149{
150 wait();
151}
152
153template <class Protocol>
154void
156{
157 std::lock_guard const lock(mutex_);
158 if (!stop_)
159 {
160 stop_ = true;
161 for (auto& c : list_)
162 c.stop();
163 }
164}
165
166template <class Protocol>
167void
169{
170 std::unique_lock<std::mutex> lock(mutex_);
171 while (!list_.empty())
172 cond_.wait(lock);
173}
174
175template <class Protocol>
176template <class Handler>
177void
178Checker<Protocol>::async_connect(beast::IP::Endpoint const& endpoint, Handler&& handler)
179{
180 auto const op =
181 std::make_shared<async_op<Handler>>(*this, io_context_, std::forward<Handler>(handler));
182 {
183 std::lock_guard const lock(mutex_);
184 list_.push_back(*op);
185 }
186 op->socket_.async_connect(
188 std::bind(&basic_async_op::operator(), op, std::placeholders::_1));
189}
190
191template <class Protocol>
192void
194{
195 std::lock_guard const lock(mutex_);
196 list_.erase(list_.iterator_to(op));
197 if (list_.size() == 0)
198 cond_.notify_all();
199}
200
201} // namespace PeerFinder
202} // namespace xrpl
T bind(T... args)
A version-independent IP address and port combination.
Definition IPEndpoint.h:18
Tests remote listening sockets to make sure they are connectable.
Definition Checker.h:19
boost::system::error_code error_code
Definition Checker.h:21
void async_connect(beast::IP::Endpoint const &endpoint, Handler &&handler)
Performs an async connection test on the specified endpoint.
Definition Checker.h:178
void remove(basic_async_op &op)
Definition Checker.h:193
void stop()
Stop the service.
Definition Checker.h:155
boost::asio::io_context & io_context_
Definition Checker.h:63
typename boost::intrusive::make_list< basic_async_op, boost::intrusive::constant_time_size< true > >::type list_type
Definition Checker.h:59
Checker(boost::asio::io_context &io_context)
Definition Checker.h:143
std::condition_variable cond_
Definition Checker.h:62
void wait()
Block until all pending I/O completes.
Definition Checker.h:168
~Checker()
Destroy the service.
Definition Checker.h:148
T is_same_v
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
static boost::asio::ip::tcp::endpoint to_asio_endpoint(IP::Endpoint const &address)
typename Protocol::endpoint endpoint_type
Definition Checker.h:39
void operator()(error_code const &ec) override
Definition Checker.h:135
typename Protocol::socket socket_type
Definition Checker.h:38
virtual void operator()(error_code const &ec)=0