rippled
Loading...
Searching...
No Matches
Checker.h
1#ifndef XRPL_PEERFINDER_CHECKER_H_INCLUDED
2#define XRPL_PEERFINDER_CHECKER_H_INCLUDED
3
4#include <xrpl/beast/net/IPAddressConversion.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 ripple {
15namespace PeerFinder {
16
18template <class Protocol = boost::asio::ip::tcp>
20{
21private:
22 using error_code = boost::system::error_code;
23
25 : boost::intrusive::list_base_hook<
26 boost::intrusive::link_mode<boost::intrusive::normal_link>>
27 {
28 virtual ~basic_async_op() = 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 = typename Protocol::socket;
41 using endpoint_type = typename Protocol::endpoint;
42
45 Handler handler_;
46
47 async_op(
48 Checker& owner,
49 boost::asio::io_context& io_context,
50 Handler&& handler);
51
53
54 void
55 stop() override;
56
57 void
58 operator()(error_code const& ec) override;
59 };
60
61 //--------------------------------------------------------------------------
62
63 using list_type = typename boost::intrusive::make_list<
65 boost::intrusive::constant_time_size<true>>::type;
66
69 boost::asio::io_context& io_context_;
71 bool stop_ = false;
72
73public:
74 explicit Checker(boost::asio::io_context& io_context);
75
83
90 void
92
94 void
96
101 template <class Handler>
102 void
103 async_connect(beast::IP::Endpoint const& endpoint, Handler&& handler);
104
105private:
106 void
107 remove(basic_async_op& op);
108};
109
110//------------------------------------------------------------------------------
111
112template <class Protocol>
113template <class Handler>
115 Checker& owner,
116 boost::asio::io_context& io_context,
117 Handler&& handler)
118 : checker_(owner)
119 , socket_(io_context)
120 , handler_(std::forward<Handler>(handler))
121{
122}
123
124template <class Protocol>
125template <class Handler>
130
131template <class Protocol>
132template <class Handler>
133void
135{
136 error_code ec;
137 socket_.cancel(ec);
138}
139
140template <class Protocol>
141template <class Handler>
142void
147
148//------------------------------------------------------------------------------
149
150template <class Protocol>
151Checker<Protocol>::Checker(boost::asio::io_context& io_context)
152 : io_context_(io_context)
153{
154}
155
156template <class Protocol>
158{
159 wait();
160}
161
162template <class Protocol>
163void
165{
166 std::lock_guard lock(mutex_);
167 if (!stop_)
168 {
169 stop_ = true;
170 for (auto& c : list_)
171 c.stop();
172 }
173}
174
175template <class Protocol>
176void
178{
179 std::unique_lock<std::mutex> lock(mutex_);
180 while (!list_.empty())
181 cond_.wait(lock);
182}
183
184template <class Protocol>
185template <class Handler>
186void
188 beast::IP::Endpoint const& endpoint,
189 Handler&& handler)
190{
191 auto const op = std::make_shared<async_op<Handler>>(
192 *this, io_context_, std::forward<Handler>(handler));
193 {
194 std::lock_guard lock(mutex_);
195 list_.push_back(*op);
196 }
197 op->socket_.async_connect(
199 std::bind(&basic_async_op::operator(), op, std::placeholders::_1));
200}
201
202template <class Protocol>
203void
205{
206 std::lock_guard lock(mutex_);
207 list_.erase(list_.iterator_to(op));
208 if (list_.size() == 0)
209 cond_.notify_all();
210}
211
212} // namespace PeerFinder
213} // namespace ripple
214
215#endif
T bind(T... args)
A version-independent IP address and port combination.
Definition IPEndpoint.h:19
Tests remote listening sockets to make sure they are connectible.
Definition Checker.h:20
boost::asio::io_context & io_context_
Definition Checker.h:69
boost::system::error_code error_code
Definition Checker.h:22
void remove(basic_async_op &op)
Definition Checker.h:204
std::condition_variable cond_
Definition Checker.h:68
void stop()
Stop the service.
Definition Checker.h:164
~Checker()
Destroy the service.
Definition Checker.h:157
void async_connect(beast::IP::Endpoint const &endpoint, Handler &&handler)
Performs an async connection test on the specified endpoint.
Definition Checker.h:187
typename boost::intrusive::make_list< basic_async_op, boost::intrusive::constant_time_size< true > >::type list_type
Definition Checker.h:65
void wait()
Block until all pending I/O completes.
Definition Checker.h:177
Checker(boost::asio::io_context &io_context)
Definition Checker.h:151
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
STL namespace.
static boost::asio::ip::tcp::endpoint to_asio_endpoint(IP::Endpoint const &address)
void operator()(error_code const &ec) override
Definition Checker.h:143
typename Protocol::endpoint endpoint_type
Definition Checker.h:41
typename Protocol::socket socket_type
Definition Checker.h:40
virtual void operator()(error_code const &ec)=0