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