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/Server.h>
26#include <xrpl/server/detail/Door.h>
27#include <xrpl/server/detail/io_list.h>
28#include <boost/asio.hpp>
29#include <array>
30#include <chrono>
31#include <mutex>
32#include <optional>
33
34namespace ripple {
35
37
44class Server
45{
46public:
51 virtual ~Server() = default;
52
54 virtual beast::Journal
55 journal() = 0;
56
60 virtual Endpoints
61 ports(std::vector<Port> const& v) = 0;
62
71 virtual void
72 close() = 0;
73};
74
75template <class Handler>
76class ServerImpl : public Server
77{
78private:
80
81 enum { historySize = 100 };
82
83 Handler& handler_;
85 boost::asio::io_service& io_service_;
86 boost::asio::io_service::strand strand_;
88
92 int high_ = 0;
94
96
97public:
99 Handler& handler,
100 boost::asio::io_service& io_service,
102
103 ~ServerImpl();
104
106 journal() override
107 {
108 return j_;
109 }
110
112 ports(std::vector<Port> const& ports) override;
113
114 void
115 close() override;
116
117 io_list&
119 {
120 return ios_;
121 }
122
123 boost::asio::io_service&
125 {
126 return io_service_;
127 }
128
129 bool
130 closed();
131
132private:
133 static int
134 ceil_log2(unsigned long long x);
135};
136
137template <class Handler>
139 Handler& handler,
140 boost::asio::io_service& io_service,
141 beast::Journal journal)
142 : handler_(handler)
143 , j_(journal)
144 , io_service_(io_service)
145 , strand_(io_service_)
146 , work_(io_service_)
147{
148}
149
150template <class Handler>
152{
153 // Handler::onStopped will not be called
154 work_ = std::nullopt;
155 ios_.close();
156 ios_.join();
157}
158
159template <class Handler>
162{
163 if (closed())
164 Throw<std::logic_error>("ports() on closed Server");
165 ports_.reserve(ports.size());
166 Endpoints eps;
167 eps.reserve(ports.size());
168 for (auto const& port : ports)
169 {
170 ports_.push_back(port);
171 if (auto sp = ios_.emplace<Door<Handler>>(
172 handler_, io_service_, ports_.back(), j_))
173 {
174 list_.push_back(sp);
175 eps.push_back(sp->get_endpoint());
176 sp->run();
177 }
178 }
179 return eps;
180}
181
182template <class Handler>
183void
185{
186 ios_.close([&] {
187 work_ = std::nullopt;
188 handler_.onStopped(*this);
189 });
190}
191
192template <class Handler>
193bool
195{
196 return ios_.closed();
197}
198} // namespace ripple
199
200#endif
A generic endpoint for log messages.
Definition: Journal.h:59
A listening socket.
Definition: Door.h:49
beast::Journal const j_
Definition: ServerImpl.h:84
Handler & handler_
Definition: ServerImpl.h:83
std::vector< std::weak_ptr< Door< Handler > > > list_
Definition: ServerImpl.h:91
boost::asio::io_service::strand strand_
Definition: ServerImpl.h:86
std::vector< Port > ports_
Definition: ServerImpl.h:90
std::mutex m_
Definition: ServerImpl.h:89
void close() override
Close the server.
Definition: ServerImpl.h:184
std::array< std::size_t, 64 > hist_
Definition: ServerImpl.h:93
std::optional< boost::asio::io_service::work > work_
Definition: ServerImpl.h:87
beast::Journal journal() override
Returns the Journal associated with the server.
Definition: ServerImpl.h:106
boost::asio::io_service & io_service_
Definition: ServerImpl.h:85
boost::asio::io_service & get_io_service()
Definition: ServerImpl.h:124
static int ceil_log2(unsigned long long x)
io_list & ios()
Definition: ServerImpl.h:118
ServerImpl(Handler &handler, boost::asio::io_service &io_service, beast::Journal journal)
Definition: ServerImpl.h:138
Endpoints ports(std::vector< Port > const &ports) override
Set the listening port settings.
Definition: ServerImpl.h:161
A multi-protocol server.
Definition: ServerImpl.h:45
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
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
T push_back(T... args)
T reserve(T... args)
T size(T... args)