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