rippled
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 <ripple/beast/utility/WrappedSink.h>
24 #include <ripple/server/Port.h>
25 #include <ripple/server/impl/LowestLayer.h>
26 #include <ripple/server/impl/io_list.h>
27 #include <boost/asio.hpp>
28 #include <atomic>
29 #include <cassert>
30 #include <functional>
31 #include <string>
32 
33 namespace ripple {
34 
35 // Common part of all peers
36 template <class Handler, class Impl>
37 class BasePeer : public io_list::work
38 {
39 protected:
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 
54 public:
55  BasePeer(
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 
65 private:
66  Impl&
67  impl()
68  {
69  return *static_cast<Impl*>(this);
70  }
71 };
72 
73 //------------------------------------------------------------------------------
74 
75 template <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 
97 template <class Handler, class Impl>
98 void
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
std::chrono::system_clock
ripple::BasePeer::work_
boost::asio::executor_work_guard< boost::asio::executor > work_
Definition: BasePeer.h:51
ripple::BasePeer::handler_
Handler & handler_
Definition: BasePeer.h:46
std::bind
T bind(T... args)
ripple::BasePeer::sink_
beast::WrappedSink sink_
Definition: BasePeer.h:48
functional
ripple::BasePeer
Definition: BasePeer.h:37
ripple::BasePeer::BasePeer
BasePeer(Port const &port, Handler &handler, boost::asio::executor const &executor, endpoint_type remote_address, beast::Journal journal)
Definition: BasePeer.h:76
ripple::BasePeer< Handler, PlainWSPeer< Handler > >::waitable_timer
boost::asio::basic_waitable_timer< clock_type > waitable_timer
Definition: BasePeer.h:43
ripple::BasePeer::strand_
boost::asio::strand< boost::asio::executor > strand_
Definition: BasePeer.h:52
ripple::BasePeer< Handler, PlainWSPeer< Handler > >::error_code
boost::system::error_code error_code
Definition: BasePeer.h:41
ripple::BasePeer::close
void close() override
Definition: BasePeer.h:99
std::to_string
T to_string(T... args)
ripple::BasePeer::port_
Port const & port_
Definition: BasePeer.h:45
ripple::BasePeer< Handler, PlainWSPeer< Handler > >::endpoint_type
boost::asio::ip::tcp::endpoint endpoint_type
Definition: BasePeer.h:42
ripple::io_list::work
Definition: io_list.h:38
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
atomic
ripple::Port
Configuration information for a Server listening port.
Definition: Port.h:48
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
cassert
beast::WrappedSink
Wraps a Journal::Sink to prefix its output with a string.
Definition: WrappedSink.h:33
ripple::BasePeer::impl
Impl & impl()
Definition: BasePeer.h:67
ripple::BasePeer::j_
const beast::Journal j_
Definition: BasePeer.h:49
ripple::BasePeer::remote_address_
endpoint_type remote_address_
Definition: BasePeer.h:47
ripple::get_lowest_layer
decltype(auto) get_lowest_layer(T &t) noexcept
Definition: LowestLayer.h:35
string