xrpld
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 static constexpr auto kHistorySize = 100;
66
67 Handler& handler_;
69 boost::asio::io_context& ioContext_;
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& ioContext, beast::Journal journal);
83
84 ~ServerImpl() override;
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 IOList&
100 {
101 return ios_;
102 }
103
104 boost::asio::io_context&
106 {
107 return ioContext_;
108 }
109
110 bool
111 closed();
112
113private:
114 static int
115 ceilLog2(unsigned long long x);
116};
117
118template <class Handler>
120 Handler& handler,
121 boost::asio::io_context& ioContext,
123 : handler_(handler)
124 , j_(journal)
125 , ioContext_(ioContext)
126 , strand_(boost::asio::make_strand(ioContext_))
127 , work_(std::in_place, boost::asio::make_work_guard(ioContext_))
128{
129}
130
131template <class Handler>
133{
134 // Handler::onStopped will not be called
135 work_ = std::nullopt;
136 ios_.close();
137 ios_.join();
138}
139
140template <class Handler>
143{
144 if (closed())
145 Throw<std::logic_error>("ports() on closed Server");
146 ports_.reserve(ports.size());
147 Endpoints eps;
148 eps.reserve(ports.size());
149 for (auto const& port : ports)
150 {
151 ports_.push_back(port);
152 auto& internalPort = ports_.back();
153 if (auto sp = ios_.emplace<Door<Handler>>(handler_, ioContext_, internalPort, j_))
154 {
155 list_.push_back(sp);
156
157 auto ep = sp->getEndpoint();
158 if (internalPort.port == 0u)
159 internalPort.port = ep.port();
160 eps.emplace(port.name, std::move(ep));
161
162 sp->run();
163 }
164 }
165 return eps;
166}
167
168template <class Handler>
169void
171{
172 ios_.close([&] {
173 work_ = std::nullopt;
174 handler_.onStopped(*this);
175 });
176}
177
178template <class Handler>
179bool
181{
182 return ios_.closed();
183}
184} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
A listening socket.
Definition Door.h:42
Manages a set of objects performing asynchronous I/O.
Definition io_list.h:16
std::vector< Port > ports_
Definition ServerImpl.h:74
ServerImpl(Handler &handler, boost::asio::io_context &ioContext, beast::Journal journal)
Definition ServerImpl.h:119
boost::asio::io_context & getIoContext()
Definition ServerImpl.h:105
Endpoints ports(std::vector< Port > const &ports) override
Set the listening port settings.
Definition ServerImpl.h:142
void close() override
Close the server.
Definition ServerImpl.h:170
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
~ServerImpl() override
Definition ServerImpl.h:132
IOList & ios()
Definition ServerImpl.h:99
std::chrono::system_clock clock_type
Definition ServerImpl.h:63
boost::asio::io_context & ioContext_
Definition ServerImpl.h:69
static constexpr auto kHistorySize
Definition ServerImpl.h:65
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
static int ceilLog2(unsigned long long x)
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.
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:20
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
T reserve(T... args)