rippled
Loading...
Searching...
No Matches
Checker.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PEERFINDER_CHECKER_H_INCLUDED
21#define RIPPLE_PEERFINDER_CHECKER_H_INCLUDED
22
23#include <xrpl/beast/net/IPAddressConversion.h>
24
25#include <boost/asio/io_context.hpp>
26#include <boost/asio/ip/tcp.hpp>
27#include <boost/intrusive/list.hpp>
28
29#include <condition_variable>
30#include <memory>
31#include <mutex>
32
33namespace ripple {
34namespace PeerFinder {
35
37template <class Protocol = boost::asio::ip::tcp>
39{
40private:
41 using error_code = boost::system::error_code;
42
44 : boost::intrusive::list_base_hook<
45 boost::intrusive::link_mode<boost::intrusive::normal_link>>
46 {
47 virtual ~basic_async_op() = default;
48
49 virtual void
50 stop() = 0;
51
52 virtual void
53 operator()(error_code const& ec) = 0;
54 };
55
56 template <class Handler>
58 {
59 using socket_type = typename Protocol::socket;
60 using endpoint_type = typename Protocol::endpoint;
61
64 Handler handler_;
65
66 async_op(
67 Checker& owner,
68 boost::asio::io_context& io_context,
69 Handler&& handler);
70
72
73 void
74 stop() override;
75
76 void
77 operator()(error_code const& ec) override;
78 };
79
80 //--------------------------------------------------------------------------
81
82 using list_type = typename boost::intrusive::make_list<
84 boost::intrusive::constant_time_size<true>>::type;
85
88 boost::asio::io_context& io_context_;
90 bool stop_ = false;
91
92public:
93 explicit Checker(boost::asio::io_context& io_context);
94
102
109 void
111
113 void
115
120 template <class Handler>
121 void
122 async_connect(beast::IP::Endpoint const& endpoint, Handler&& handler);
123
124private:
125 void
126 remove(basic_async_op& op);
127};
128
129//------------------------------------------------------------------------------
130
131template <class Protocol>
132template <class Handler>
134 Checker& owner,
135 boost::asio::io_context& io_context,
136 Handler&& handler)
137 : checker_(owner)
138 , socket_(io_context)
139 , handler_(std::forward<Handler>(handler))
140{
141}
142
143template <class Protocol>
144template <class Handler>
149
150template <class Protocol>
151template <class Handler>
152void
154{
155 error_code ec;
156 socket_.cancel(ec);
157}
158
159template <class Protocol>
160template <class Handler>
161void
166
167//------------------------------------------------------------------------------
168
169template <class Protocol>
170Checker<Protocol>::Checker(boost::asio::io_context& io_context)
171 : io_context_(io_context)
172{
173}
174
175template <class Protocol>
177{
178 wait();
179}
180
181template <class Protocol>
182void
184{
185 std::lock_guard lock(mutex_);
186 if (!stop_)
187 {
188 stop_ = true;
189 for (auto& c : list_)
190 c.stop();
191 }
192}
193
194template <class Protocol>
195void
197{
198 std::unique_lock<std::mutex> lock(mutex_);
199 while (!list_.empty())
200 cond_.wait(lock);
201}
202
203template <class Protocol>
204template <class Handler>
205void
207 beast::IP::Endpoint const& endpoint,
208 Handler&& handler)
209{
210 auto const op = std::make_shared<async_op<Handler>>(
211 *this, io_context_, std::forward<Handler>(handler));
212 {
213 std::lock_guard lock(mutex_);
214 list_.push_back(*op);
215 }
216 op->socket_.async_connect(
218 std::bind(&basic_async_op::operator(), op, std::placeholders::_1));
219}
220
221template <class Protocol>
222void
224{
225 std::lock_guard lock(mutex_);
226 list_.erase(list_.iterator_to(op));
227 if (list_.size() == 0)
228 cond_.notify_all();
229}
230
231} // namespace PeerFinder
232} // namespace ripple
233
234#endif
T bind(T... args)
A version-independent IP address and port combination.
Definition IPEndpoint.h:38
Tests remote listening sockets to make sure they are connectible.
Definition Checker.h:39
boost::asio::io_context & io_context_
Definition Checker.h:88
boost::system::error_code error_code
Definition Checker.h:41
void remove(basic_async_op &op)
Definition Checker.h:223
std::condition_variable cond_
Definition Checker.h:87
void stop()
Stop the service.
Definition Checker.h:183
~Checker()
Destroy the service.
Definition Checker.h:176
void async_connect(beast::IP::Endpoint const &endpoint, Handler &&handler)
Performs an async connection test on the specified endpoint.
Definition Checker.h:206
typename boost::intrusive::make_list< basic_async_op, boost::intrusive::constant_time_size< true > >::type list_type
Definition Checker.h:84
void wait()
Block until all pending I/O completes.
Definition Checker.h:196
Checker(boost::asio::io_context &io_context)
Definition Checker.h:170
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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:162
typename Protocol::endpoint endpoint_type
Definition Checker.h:60
typename Protocol::socket socket_type
Definition Checker.h:59
virtual void operator()(error_code const &ec)=0