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<boost::intrusive::link_mode<boost::intrusive::normal_link>>
24 {
25 virtual ~basic_async_op() = 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 = typename Protocol::socket;
38 using endpoint_type = typename Protocol::endpoint;
39
42 Handler handler_;
43
44 async_op(Checker& owner, boost::asio::io_context& io_context, Handler&& handler);
45
47
48 void
49 stop() override;
50
51 void
52 operator()(error_code const& ec) override;
53 };
54
55 //--------------------------------------------------------------------------
56
57 using list_type =
58 typename boost::intrusive::make_list<basic_async_op, boost::intrusive::constant_time_size<true>>::type;
59
62 boost::asio::io_context& io_context_;
64 bool stop_ = false;
65
66public:
67 explicit Checker(boost::asio::io_context& io_context);
68
76
83 void
85
87 void
89
94 template <class Handler>
95 void
96 async_connect(beast::IP::Endpoint const& endpoint, Handler&& handler);
97
98private:
99 void
100 remove(basic_async_op& op);
101};
102
103//------------------------------------------------------------------------------
104
105template <class Protocol>
106template <class Handler>
107Checker<Protocol>::async_op<Handler>::async_op(Checker& owner, boost::asio::io_context& io_context, Handler&& handler)
108 : checker_(owner), socket_(io_context), handler_(std::forward<Handler>(handler))
109{
110}
111
112template <class Protocol>
113template <class Handler>
118
119template <class Protocol>
120template <class Handler>
121void
123{
124 error_code ec;
125 socket_.cancel(ec);
126}
127
128template <class Protocol>
129template <class Handler>
130void
135
136//------------------------------------------------------------------------------
137
138template <class Protocol>
139Checker<Protocol>::Checker(boost::asio::io_context& io_context) : io_context_(io_context)
140{
141}
142
143template <class Protocol>
145{
146 wait();
147}
148
149template <class Protocol>
150void
152{
153 std::lock_guard 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{
166 std::unique_lock<std::mutex> lock(mutex_);
167 while (!list_.empty())
168 cond_.wait(lock);
169}
170
171template <class Protocol>
172template <class Handler>
173void
174Checker<Protocol>::async_connect(beast::IP::Endpoint const& endpoint, Handler&& handler)
175{
176 auto const op = std::make_shared<async_op<Handler>>(*this, io_context_, std::forward<Handler>(handler));
177 {
178 std::lock_guard lock(mutex_);
179 list_.push_back(*op);
180 }
181 op->socket_.async_connect(
183 std::bind(&basic_async_op::operator(), op, std::placeholders::_1));
184}
185
186template <class Protocol>
187void
189{
190 std::lock_guard lock(mutex_);
191 list_.erase(list_.iterator_to(op));
192 if (list_.size() == 0)
193 cond_.notify_all();
194}
195
196} // namespace PeerFinder
197} // 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
typename boost::intrusive::make_list< basic_async_op, boost::intrusive::constant_time_size< true > >::type list_type
Definition Checker.h:58
void async_connect(beast::IP::Endpoint const &endpoint, Handler &&handler)
Performs an async connection test on the specified endpoint.
Definition Checker.h:174
void remove(basic_async_op &op)
Definition Checker.h:188
void stop()
Stop the service.
Definition Checker.h:151
boost::asio::io_context & io_context_
Definition Checker.h:62
Checker(boost::asio::io_context &io_context)
Definition Checker.h:139
std::condition_variable cond_
Definition Checker.h:61
void wait()
Block until all pending I/O completes.
Definition Checker.h:164
~Checker()
Destroy the service.
Definition Checker.h:144
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:38
void operator()(error_code const &ec) override
Definition Checker.h:131
typename Protocol::socket socket_type
Definition Checker.h:37
virtual void operator()(error_code const &ec)=0