xrpld
Loading...
Searching...
No Matches
ServerImpl.h
1#pragma once
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/server/Port.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 <cstddef>
16#include <memory>
17#include <mutex>
18#include <optional>
19#include <stdexcept>
20#include <string>
21#include <unordered_map>
22#include <utility>
23#include <vector>
24
25namespace xrpl {
26
28
36class Server
37{
38public:
44 virtual ~Server() = default;
45
49 virtual beast::Journal
50 journal() = 0;
51
56 virtual Endpoints
57 ports(std::vector<Port> const& v) = 0;
58
68 virtual void
69 close() = 0;
70};
71
72template <class Handler>
73class ServerImpl : public Server
74{
75private:
77
78 static constexpr auto kHistorySize = 100;
79
80 Handler& handler_;
82 boost::asio::io_context& ioContext_;
83 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
85
89 int high_ = 0;
91
93
94public:
95 ServerImpl(Handler& handler, boost::asio::io_context& ioContext, beast::Journal journal);
96
97 ~ServerImpl() override;
98
100 journal() override
101 {
102 return j_;
103 }
104
106 ports(std::vector<Port> const& ports) override;
107
108 void
109 close() override;
110
111 IOList&
113 {
114 return ios_;
115 }
116
117 boost::asio::io_context&
119 {
120 return ioContext_;
121 }
122
123 bool
124 closed();
125
126private:
127 static int
128 ceilLog2(unsigned long long x);
129};
130
131template <class Handler>
133 Handler& handler,
134 boost::asio::io_context& ioContext,
136 : handler_(handler)
137 , j_(journal)
138 , ioContext_(ioContext)
139 , strand_(boost::asio::make_strand(ioContext_))
140 , work_(std::in_place, boost::asio::make_work_guard(ioContext_))
141{
142}
143
144template <class Handler>
146{
147 // Handler::onStopped will not be called
148 work_ = std::nullopt;
149 ios_.close();
150 ios_.join();
151}
152
153template <class Handler>
156{
157 if (closed())
158 Throw<std::logic_error>("ports() on closed Server");
159 ports_.reserve(ports.size());
160 Endpoints eps;
161 eps.reserve(ports.size());
162 for (auto const& port : ports)
163 {
164 ports_.push_back(port);
165 auto& internalPort = ports_.back();
166 if (auto sp = ios_.emplace<Door<Handler>>(handler_, ioContext_, internalPort, j_))
167 {
168 list_.push_back(sp);
169
170 auto ep = sp->getEndpoint();
171 if (internalPort.port == 0u)
172 internalPort.port = ep.port();
173 eps.emplace(port.name, std::move(ep));
174
175 sp->run();
176 }
177 }
178 return eps;
179}
180
181template <class Handler>
182void
184{
185 ios_.close([&] {
186 work_ = std::nullopt;
187 handler_.onStopped(*this);
188 });
189}
190
191template <class Handler>
192bool
194{
195 return ios_.closed();
196}
197} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
A listening socket.
Definition Door.h:48
Manages a set of objects performing asynchronous I/O.
Definition io_list.h:19
std::vector< Port > ports_
Definition ServerImpl.h:87
ServerImpl(Handler &handler, boost::asio::io_context &ioContext, beast::Journal journal)
Definition ServerImpl.h:132
boost::asio::io_context & getIoContext()
Definition ServerImpl.h:118
Endpoints ports(std::vector< Port > const &ports) override
Set the listening port settings.
Definition ServerImpl.h:155
void close() override
Close the server.
Definition ServerImpl.h:183
beast::Journal const j_
Definition ServerImpl.h:81
std::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
Definition ServerImpl.h:84
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Definition ServerImpl.h:83
~ServerImpl() override
Definition ServerImpl.h:145
IOList & ios()
Definition ServerImpl.h:112
std::chrono::system_clock clock_type
Definition ServerImpl.h:76
boost::asio::io_context & ioContext_
Definition ServerImpl.h:82
static constexpr auto kHistorySize
Definition ServerImpl.h:78
std::array< std::size_t, 64 > hist_
Definition ServerImpl.h:90
std::mutex m_
Definition ServerImpl.h:86
std::vector< std::weak_ptr< Door< Handler > > > list_
Definition ServerImpl.h:88
Handler & handler_
Definition ServerImpl.h:80
static int ceilLog2(unsigned long long x)
beast::Journal journal() override
Returns the Journal associated with the server.
Definition ServerImpl.h:100
A multi-protocol server.
Definition ServerImpl.h:37
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.
T emplace(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::unordered_map< std::string, boost::asio::ip::tcp::endpoint > Endpoints
Definition ServerImpl.h:27
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T reserve(T... args)