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