rippled
Loading...
Searching...
No Matches
BasePeer.h
1#ifndef XRPL_SERVER_BASEPEER_H_INCLUDED
2#define XRPL_SERVER_BASEPEER_H_INCLUDED
3
4#include <xrpl/beast/utility/WrappedSink.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/server/Port.h>
7#include <xrpl/server/detail/LowestLayer.h>
8#include <xrpl/server/detail/io_list.h>
9
10#include <boost/asio.hpp>
11
12#include <atomic>
13#include <functional>
14#include <string>
15
16namespace ripple {
17
18// Common part of all peers
19template <class Handler, class Impl>
20class BasePeer : public io_list::work
21{
22protected:
24 using error_code = boost::system::error_code;
25 using endpoint_type = boost::asio::ip::tcp::endpoint;
26 using waitable_timer = boost::asio::basic_waitable_timer<clock_type>;
27
28 Port const& port_;
29 Handler& handler_;
33
34 boost::asio::executor_work_guard<boost::asio::executor> work_;
35 boost::asio::strand<boost::asio::executor> strand_;
36
37public:
39 Port const& port,
40 Handler& handler,
41 boost::asio::executor const& executor,
42 endpoint_type remote_address,
43 beast::Journal journal);
44
45 void
46 close() override;
47
48private:
49 Impl&
51 {
52 return *static_cast<Impl*>(this);
53 }
54};
55
56//------------------------------------------------------------------------------
57
58template <class Handler, class Impl>
60 Port const& port,
61 Handler& handler,
62 boost::asio::executor const& executor,
63 endpoint_type remote_address,
64 beast::Journal journal)
65 : port_(port)
66 , handler_(handler)
67 , remote_address_(remote_address)
68 , sink_(
69 journal.sink(),
70 [] {
71 static std::atomic<unsigned> id{0};
72 return "##" + std::to_string(++id) + " ";
73 }())
74 , j_(sink_)
75 , work_(boost::asio::make_work_guard(executor))
76 , strand_(boost::asio::make_strand(executor))
77{
78}
79
80template <class Handler, class Impl>
81void
83{
84 if (!strand_.running_in_this_thread())
85 return post(
86 strand_, std::bind(&BasePeer::close, impl().shared_from_this()));
87 error_code ec;
88 ripple::get_lowest_layer(impl().ws_).socket().close(ec);
89}
90
91} // namespace ripple
92
93#endif
T bind(T... args)
A generic endpoint for log messages.
Definition Journal.h:41
Wraps a Journal::Sink to prefix its output with a string.
Definition WrappedSink.h:15
Handler & handler_
Definition BasePeer.h:29
Impl & impl()
Definition BasePeer.h:50
boost::asio::ip::tcp::endpoint endpoint_type
Definition BasePeer.h:25
void close() override
Definition BasePeer.h:82
Port const & port_
Definition BasePeer.h:28
beast::WrappedSink sink_
Definition BasePeer.h:31
boost::asio::basic_waitable_timer< clock_type > waitable_timer
Definition BasePeer.h:26
endpoint_type remote_address_
Definition BasePeer.h:30
boost::system::error_code error_code
Definition BasePeer.h:24
BasePeer(Port const &port, Handler &handler, boost::asio::executor const &executor, endpoint_type remote_address, beast::Journal journal)
Definition BasePeer.h:59
beast::Journal const j_
Definition BasePeer.h:32
boost::asio::executor_work_guard< boost::asio::executor > work_
Definition BasePeer.h:34
boost::asio::strand< boost::asio::executor > strand_
Definition BasePeer.h:35
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
decltype(auto) get_lowest_layer(T &t) noexcept
Definition LowestLayer.h:15
Configuration information for a Server listening port.
Definition Port.h:31
T to_string(T... args)