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