rippled
Loading...
Searching...
No Matches
ServerImpl.h
1#ifndef XRPL_SERVER_SERVERIMPL_H_INCLUDED
2#define XRPL_SERVER_SERVERIMPL_H_INCLUDED
3
4#include <xrpl/basics/chrono.h>
5#include <xrpl/beast/core/List.h>
6#include <xrpl/server/detail/Door.h>
7#include <xrpl/server/detail/io_list.h>
8
9#include <boost/asio.hpp>
10#include <boost/asio/executor_work_guard.hpp>
11#include <boost/asio/io_context.hpp>
12
13#include <array>
14#include <chrono>
15#include <mutex>
16#include <optional>
17#include <unordered_map>
18
19namespace ripple {
20
21using Endpoints =
23
30class Server
31{
32public:
37 virtual ~Server() = default;
38
40 virtual beast::Journal
41 journal() = 0;
42
46 virtual Endpoints
47 ports(std::vector<Port> const& v) = 0;
48
57 virtual void
58 close() = 0;
59};
60
61template <class Handler>
62class ServerImpl : public Server
63{
64private:
66
67 enum { historySize = 100 };
68
69 Handler& handler_;
71 boost::asio::io_context& io_context_;
72 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
73 std::optional<boost::asio::executor_work_guard<
74 boost::asio::io_context::executor_type>>
76
80 int high_ = 0;
82
84
85public:
87 Handler& handler,
88 boost::asio::io_context& io_context,
90
92
94 journal() override
95 {
96 return j_;
97 }
98
100 ports(std::vector<Port> const& ports) override;
101
102 void
103 close() override;
104
105 io_list&
107 {
108 return ios_;
109 }
110
111 boost::asio::io_context&
113 {
114 return io_context_;
115 }
116
117 bool
118 closed();
119
120private:
121 static int
122 ceil_log2(unsigned long long x);
123};
124
125template <class Handler>
127 Handler& handler,
128 boost::asio::io_context& io_context,
129 beast::Journal journal)
130 : handler_(handler)
131 , j_(journal)
132 , io_context_(io_context)
133 , strand_(boost::asio::make_strand(io_context_))
134 , work_(std::in_place, boost::asio::make_work_guard(io_context_))
135{
136}
137
138template <class Handler>
140{
141 // Handler::onStopped will not be called
142 work_ = std::nullopt;
143 ios_.close();
144 ios_.join();
145}
146
147template <class Handler>
150{
151 if (closed())
152 Throw<std::logic_error>("ports() on closed Server");
153 ports_.reserve(ports.size());
154 Endpoints eps;
155 eps.reserve(ports.size());
156 for (auto const& port : ports)
157 {
158 ports_.push_back(port);
159 auto& internalPort = ports_.back();
160 if (auto sp = ios_.emplace<Door<Handler>>(
161 handler_, io_context_, internalPort, j_))
162 {
163 list_.push_back(sp);
164
165 auto ep = sp->get_endpoint();
166 if (!internalPort.port)
167 internalPort.port = ep.port();
168 eps.emplace(port.name, std::move(ep));
169
170 sp->run();
171 }
172 }
173 return eps;
174}
175
176template <class Handler>
177void
179{
180 ios_.close([&] {
181 work_ = std::nullopt;
182 handler_.onStopped(*this);
183 });
184}
185
186template <class Handler>
187bool
189{
190 return ios_.closed();
191}
192} // namespace ripple
193
194#endif
A generic endpoint for log messages.
Definition Journal.h:41
A listening socket.
Definition Door.h:44
beast::Journal const j_
Definition ServerImpl.h:70
Handler & handler_
Definition ServerImpl.h:69
std::vector< std::weak_ptr< Door< Handler > > > list_
Definition ServerImpl.h:79
std::vector< Port > ports_
Definition ServerImpl.h:78
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Definition ServerImpl.h:72
void close() override
Close the server.
Definition ServerImpl.h:178
std::array< std::size_t, 64 > hist_
Definition ServerImpl.h:81
boost::asio::io_context & get_io_context()
Definition ServerImpl.h:112
beast::Journal journal() override
Returns the Journal associated with the server.
Definition ServerImpl.h:94
std::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
Definition ServerImpl.h:75
static int ceil_log2(unsigned long long x)
io_list & ios()
Definition ServerImpl.h:106
boost::asio::io_context & io_context_
Definition ServerImpl.h:71
Endpoints ports(std::vector< Port > const &ports) override
Set the listening port settings.
Definition ServerImpl.h:149
ServerImpl(Handler &handler, boost::asio::io_context &io_context, beast::Journal journal)
Definition ServerImpl.h:126
A multi-protocol server.
Definition ServerImpl.h:31
virtual void close()=0
Close the server.
virtual Endpoints ports(std::vector< Port > const &v)=0
Set the listening port settings.
virtual ~Server()=default
Destroy the server.
virtual beast::Journal journal()=0
Returns the Journal associated with the server.
Manages a set of objects performing asynchronous I/O.
Definition io_list.h:17
T emplace(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
STL namespace.
T reserve(T... args)
T size(T... args)