rippled
Loading...
Searching...
No Matches
ServerImpl.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_SERVERIMPL_H_INCLUDED
21#define RIPPLE_SERVER_SERVERIMPL_H_INCLUDED
22
23#include <xrpl/basics/chrono.h>
24#include <xrpl/beast/core/List.h>
25#include <xrpl/server/detail/Door.h>
26#include <xrpl/server/detail/io_list.h>
27
28#include <boost/asio.hpp>
29#include <boost/asio/executor_work_guard.hpp>
30#include <boost/asio/io_context.hpp>
31
32#include <array>
33#include <chrono>
34#include <mutex>
35#include <optional>
36#include <unordered_map>
37
38namespace ripple {
39
40using Endpoints =
42
49class Server
50{
51public:
56 virtual ~Server() = default;
57
59 virtual beast::Journal
60 journal() = 0;
61
65 virtual Endpoints
66 ports(std::vector<Port> const& v) = 0;
67
76 virtual void
77 close() = 0;
78};
79
80template <class Handler>
81class ServerImpl : public Server
82{
83private:
85
86 enum { historySize = 100 };
87
88 Handler& handler_;
90 boost::asio::io_context& io_context_;
91 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
92 std::optional<boost::asio::executor_work_guard<
93 boost::asio::io_context::executor_type>>
95
99 int high_ = 0;
101
103
104public:
106 Handler& handler,
107 boost::asio::io_context& io_context,
109
110 ~ServerImpl();
111
113 journal() override
114 {
115 return j_;
116 }
117
119 ports(std::vector<Port> const& ports) override;
120
121 void
122 close() override;
123
124 io_list&
126 {
127 return ios_;
128 }
129
130 boost::asio::io_context&
132 {
133 return io_context_;
134 }
135
136 bool
137 closed();
138
139private:
140 static int
141 ceil_log2(unsigned long long x);
142};
143
144template <class Handler>
146 Handler& handler,
147 boost::asio::io_context& io_context,
148 beast::Journal journal)
149 : handler_(handler)
150 , j_(journal)
151 , io_context_(io_context)
152 , strand_(boost::asio::make_strand(io_context_))
153 , work_(std::in_place, boost::asio::make_work_guard(io_context_))
154{
155}
156
157template <class Handler>
159{
160 // Handler::onStopped will not be called
161 work_ = std::nullopt;
162 ios_.close();
163 ios_.join();
164}
165
166template <class Handler>
169{
170 if (closed())
171 Throw<std::logic_error>("ports() on closed Server");
172 ports_.reserve(ports.size());
173 Endpoints eps;
174 eps.reserve(ports.size());
175 for (auto const& port : ports)
176 {
177 ports_.push_back(port);
178 auto& internalPort = ports_.back();
179 if (auto sp = ios_.emplace<Door<Handler>>(
180 handler_, io_context_, internalPort, j_))
181 {
182 list_.push_back(sp);
183
184 auto ep = sp->get_endpoint();
185 if (!internalPort.port)
186 internalPort.port = ep.port();
187 eps.emplace(port.name, std::move(ep));
188
189 sp->run();
190 }
191 }
192 return eps;
193}
194
195template <class Handler>
196void
198{
199 ios_.close([&] {
200 work_ = std::nullopt;
201 handler_.onStopped(*this);
202 });
203}
204
205template <class Handler>
206bool
208{
209 return ios_.closed();
210}
211} // namespace ripple
212
213#endif
A generic endpoint for log messages.
Definition Journal.h:60
A listening socket.
Definition Door.h:63
beast::Journal const j_
Definition ServerImpl.h:89
Handler & handler_
Definition ServerImpl.h:88
std::vector< std::weak_ptr< Door< Handler > > > list_
Definition ServerImpl.h:98
std::vector< Port > ports_
Definition ServerImpl.h:97
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Definition ServerImpl.h:91
void close() override
Close the server.
Definition ServerImpl.h:197
std::array< std::size_t, 64 > hist_
Definition ServerImpl.h:100
boost::asio::io_context & get_io_context()
Definition ServerImpl.h:131
beast::Journal journal() override
Returns the Journal associated with the server.
Definition ServerImpl.h:113
std::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
Definition ServerImpl.h:94
static int ceil_log2(unsigned long long x)
io_list & ios()
Definition ServerImpl.h:125
boost::asio::io_context & io_context_
Definition ServerImpl.h:90
Endpoints ports(std::vector< Port > const &ports) override
Set the listening port settings.
Definition ServerImpl.h:168
ServerImpl(Handler &handler, boost::asio::io_context &io_context, beast::Journal journal)
Definition ServerImpl.h:145
A multi-protocol server.
Definition ServerImpl.h:50
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:36
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:25
STL namespace.
T reserve(T... args)
T size(T... args)