xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
server
detail
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
25
namespace
xrpl
{
26
27
using
Endpoints
=
std::unordered_map<std::string, boost::asio::ip::tcp::endpoint>
;
28
36
class
Server
37
{
38
public
:
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
72
template
<
class
Handler>
73
class
ServerImpl
:
public
Server
74
{
75
private
:
76
using
ClockType
=
std::chrono::system_clock
;
77
78
static
constexpr
auto
kHistorySize
= 100;
79
80
Handler&
handler_
;
81
beast::Journal
const
j_
;
82
boost::asio::io_context&
ioContext_
;
83
boost::asio::strand<boost::asio::io_context::executor_type>
strand_
;
84
std::optional<boost::asio::executor_work_guard<boost::asio::io_context::executor_type>
>
work_
;
85
86
std::mutex
m_
;
87
std::vector<Port>
ports_
;
88
std::vector<std::weak_ptr<Door<Handler>
>>
list_
;
89
int
high_
= 0;
90
std::array<std::size_t, 64>
hist_
{};
91
92
IOList
ios_
;
93
94
public
:
95
ServerImpl
(Handler& handler, boost::asio::io_context& ioContext,
beast::Journal
journal
);
96
97
~ServerImpl
()
override
;
98
99
beast::Journal
100
journal
()
override
101
{
102
return
j_
;
103
}
104
105
Endpoints
106
ports
(
std::vector<Port>
const
&
ports
)
override
;
107
108
void
109
close
()
override
;
110
111
IOList
&
112
ios
()
113
{
114
return
ios_
;
115
}
116
117
boost::asio::io_context&
118
getIoContext
()
119
{
120
return
ioContext_
;
121
}
122
123
bool
124
closed
();
125
126
private
:
127
static
int
128
ceilLog2
(
unsigned
long
long
x);
129
};
130
131
template
<
class
Handler>
132
ServerImpl<Handler>::ServerImpl
(
133
Handler& handler,
134
boost::asio::io_context& ioContext,
135
beast::Journal
journal
)
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
144
template
<
class
Handler>
145
ServerImpl<Handler>::~ServerImpl
()
146
{
147
// Handler::onStopped will not be called
148
work_
= std::nullopt;
149
ios_
.close();
150
ios_
.join();
151
}
152
153
template
<
class
Handler>
154
Endpoints
155
ServerImpl<Handler>::ports
(
std::vector<Port>
const
&
ports
)
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
181
template
<
class
Handler>
182
void
183
ServerImpl<Handler>::close
()
184
{
185
ios_
.close([&] {
186
work_
= std::nullopt;
187
handler_
.onStopped(*
this
);
188
});
189
}
190
191
template
<
class
Handler>
192
bool
193
ServerImpl<Handler>::closed
()
194
{
195
return
ios_
.closed();
196
}
197
}
// namespace xrpl
array
chrono
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::Door
A listening socket.
Definition
Door.h:48
xrpl::IOList
Manages a set of objects performing asynchronous I/O.
Definition
io_list.h:19
xrpl::ServerImpl::ports_
std::vector< Port > ports_
Definition
ServerImpl.h:87
xrpl::ServerImpl::ClockType
std::chrono::system_clock ClockType
Definition
ServerImpl.h:76
xrpl::ServerImpl::ServerImpl
ServerImpl(Handler &handler, boost::asio::io_context &ioContext, beast::Journal journal)
Definition
ServerImpl.h:132
xrpl::ServerImpl::ios_
IOList ios_
Definition
ServerImpl.h:92
xrpl::ServerImpl::getIoContext
boost::asio::io_context & getIoContext()
Definition
ServerImpl.h:118
xrpl::ServerImpl::ports
Endpoints ports(std::vector< Port > const &ports) override
Set the listening port settings.
Definition
ServerImpl.h:155
xrpl::ServerImpl::close
void close() override
Close the server.
Definition
ServerImpl.h:183
xrpl::ServerImpl::j_
beast::Journal const j_
Definition
ServerImpl.h:81
xrpl::ServerImpl::work_
std::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
Definition
ServerImpl.h:84
xrpl::ServerImpl::strand_
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Definition
ServerImpl.h:83
xrpl::ServerImpl::~ServerImpl
~ServerImpl() override
Definition
ServerImpl.h:145
xrpl::ServerImpl::high_
int high_
Definition
ServerImpl.h:89
xrpl::ServerImpl::ios
IOList & ios()
Definition
ServerImpl.h:112
xrpl::ServerImpl::closed
bool closed()
Definition
ServerImpl.h:193
xrpl::ServerImpl::ioContext_
boost::asio::io_context & ioContext_
Definition
ServerImpl.h:82
xrpl::ServerImpl::kHistorySize
static constexpr auto kHistorySize
Definition
ServerImpl.h:78
xrpl::ServerImpl::hist_
std::array< std::size_t, 64 > hist_
Definition
ServerImpl.h:90
xrpl::ServerImpl::m_
std::mutex m_
Definition
ServerImpl.h:86
xrpl::ServerImpl::list_
std::vector< std::weak_ptr< Door< Handler > > > list_
Definition
ServerImpl.h:88
xrpl::ServerImpl::handler_
Handler & handler_
Definition
ServerImpl.h:80
xrpl::ServerImpl::ceilLog2
static int ceilLog2(unsigned long long x)
xrpl::ServerImpl::journal
beast::Journal journal() override
Returns the Journal associated with the server.
Definition
ServerImpl.h:100
xrpl::Server
A multi-protocol server.
Definition
ServerImpl.h:37
xrpl::Server::close
virtual void close()=0
Close the server.
xrpl::Server::~Server
virtual ~Server()=default
Destroy the server.
xrpl::Server::journal
virtual beast::Journal journal()=0
Returns the Journal associated with the server.
xrpl::Server::ports
virtual Endpoints ports(std::vector< Port > const &v)=0
Set the listening port settings.
cstddef
std::unordered_map::emplace
T emplace(T... args)
memory
mutex
boost
Definition
IPAddress.h:102
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::Endpoints
std::unordered_map< std::string, boost::asio::ip::tcp::endpoint > Endpoints
Definition
ServerImpl.h:27
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
optional
std::unordered_map::reserve
T reserve(T... args)
stdexcept
string
std::chrono::system_clock
unordered_map
utility
vector
Generated by
1.17.0