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