CppServer 1.0.5.0
C++ Server Library
Loading...
Searching...
No Matches
ssl_server.h
Go to the documentation of this file.
1
9#ifndef CPPSERVER_ASIO_SSL_SERVER_H
10#define CPPSERVER_ASIO_SSL_SERVER_H
11
12#include "ssl_context.h"
13#include "ssl_session.h"
14
15#include "system/uuid.h"
16
17#include <map>
18#include <mutex>
19#include <shared_mutex>
20#include <vector>
21
22namespace CppServer {
23namespace Asio {
24
26
31class SSLServer : public std::enable_shared_from_this<SSLServer>
32{
33 friend class SSLSession;
34
35public:
37
43 SSLServer(const std::shared_ptr<Service>& service, const std::shared_ptr<SSLContext>& context, int port, InternetProtocol protocol = InternetProtocol::IPv4);
45
51 SSLServer(const std::shared_ptr<Service>& service, const std::shared_ptr<SSLContext>& context, const std::string& address, int port);
53
58 SSLServer(const std::shared_ptr<Service>& service, const std::shared_ptr<SSLContext>& context, const asio::ip::tcp::endpoint& endpoint);
59 SSLServer(const SSLServer&) = delete;
60 SSLServer(SSLServer&&) = delete;
61 virtual ~SSLServer() = default;
62
63 SSLServer& operator=(const SSLServer&) = delete;
65
67 const CppCommon::UUID& id() const noexcept { return _id; }
68
70 std::shared_ptr<Service>& service() noexcept { return _service; }
72 std::shared_ptr<asio::io_service>& io_service() noexcept { return _io_service; }
74 asio::io_service::strand& strand() noexcept { return _strand; }
76 std::shared_ptr<SSLContext>& context() noexcept { return _context; }
78 asio::ip::tcp::endpoint& endpoint() noexcept { return _endpoint; }
80 asio::ip::tcp::acceptor& acceptor() noexcept { return _acceptor; }
81
83 const std::string& address() const noexcept { return _address; }
85 int port() const noexcept { return _port; }
86
90 uint64_t bytes_pending() const noexcept { return _bytes_pending; }
92 uint64_t bytes_sent() const noexcept { return _bytes_sent; }
94 uint64_t bytes_received() const noexcept { return _bytes_received; }
95
97 bool option_keep_alive() const noexcept { return _option_keep_alive; }
99 bool option_no_delay() const noexcept { return _option_no_delay; }
101 bool option_reuse_address() const noexcept { return _option_reuse_address; }
103 bool option_reuse_port() const noexcept { return _option_reuse_port; }
104
106 bool IsStarted() const noexcept { return _started; }
107
109
112 virtual bool Start();
114
117 virtual bool Stop();
119
122 virtual bool Restart();
123
125
130 virtual bool Multicast(const void* buffer, size_t size);
132
136 virtual bool Multicast(std::string_view text) { return Multicast(text.data(), text.size()); }
137
139
142 virtual bool DisconnectAll();
143
145
149 std::shared_ptr<SSLSession> FindSession(const CppCommon::UUID& id);
150
152
157 void SetupKeepAlive(bool enable) noexcept { _option_keep_alive = enable; }
159
166 void SetupNoDelay(bool enable) noexcept { _option_no_delay = enable; }
168
173 void SetupReuseAddress(bool enable) noexcept { _option_reuse_address = enable; }
175
180 void SetupReusePort(bool enable) noexcept { _option_reuse_port = enable; }
181
182protected:
184
188 virtual std::shared_ptr<SSLSession> CreateSession(const std::shared_ptr<SSLServer>& server) { return std::make_shared<SSLSession>(server); }
189
190protected:
192 virtual void onStarted() {}
194 virtual void onStopped() {}
195
197
200 virtual void onConnected(std::shared_ptr<SSLSession>& session) {}
202
205 virtual void onHandshaked(std::shared_ptr<SSLSession>& session) {}
207
210 virtual void onDisconnected(std::shared_ptr<SSLSession>& session) {}
211
213
218 virtual void onError(int error, const std::string& category, const std::string& message) {}
219
220protected:
221 // Server sessions
222 std::shared_mutex _sessions_lock;
223 std::map<CppCommon::UUID, std::shared_ptr<SSLSession>> _sessions;
224
225private:
226 // Server Id
227 CppCommon::UUID _id;
228 // Asio service
229 std::shared_ptr<Service> _service;
230 // Asio IO service
231 std::shared_ptr<asio::io_service> _io_service;
232 // Asio service strand for serialized handler execution
233 asio::io_service::strand _strand;
234 bool _strand_required;
235 // Server address, scheme & port
236 std::string _address;
237 int _port;
238 // Server SSL context, endpoint, acceptor and socket
239 std::shared_ptr<SSLContext> _context;
240 std::shared_ptr<SSLSession> _session;
241 asio::ip::tcp::endpoint _endpoint;
242 asio::ip::tcp::acceptor _acceptor;
243 std::atomic<bool> _started;
244 HandlerStorage _acceptor_storage;
245 // Server statistic
246 uint64_t _bytes_pending;
247 uint64_t _bytes_sent;
248 uint64_t _bytes_received;
249 // Options
250 bool _option_keep_alive;
251 bool _option_no_delay;
252 bool _option_reuse_address;
253 bool _option_reuse_port;
254
256 void Accept();
257
259 void RegisterSession();
261
264 void UnregisterSession(const CppCommon::UUID& id);
265
267 void ClearBuffers();
268
270 void SendError(std::error_code ec);
271};
272
275} // namespace Asio
276} // namespace CppServer
277
278#endif // CPPSERVER_ASIO_SSL_SERVER_H
Asio allocate handler wrapper.
Definition memory.h:133
Asio handler storage.
Definition memory.h:27
bool IsStarted() const noexcept
Is the server started?
Definition ssl_server.h:106
std::shared_mutex _sessions_lock
Definition ssl_server.h:222
void SetupReuseAddress(bool enable) noexcept
Setup option: reuse address.
Definition ssl_server.h:173
bool option_no_delay() const noexcept
Get the option: no delay.
Definition ssl_server.h:99
std::shared_ptr< SSLSession > FindSession(const CppCommon::UUID &id)
Find a session with a given Id.
virtual void onStopped()
Handle server stopped notification.
Definition ssl_server.h:194
virtual bool Multicast(std::string_view text)
Multicast text to all connected sessions.
Definition ssl_server.h:136
uint64_t bytes_received() const noexcept
Get the number of bytes received by the server.
Definition ssl_server.h:94
SSLServer(SSLServer &&)=delete
SSLServer & operator=(SSLServer &&)=delete
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
Definition ssl_server.h:72
SSLServer & operator=(const SSLServer &)=delete
virtual bool Multicast(const void *buffer, size_t size)
Multicast data to all connected sessions.
virtual bool Stop()
Stop the server.
int port() const noexcept
Get the server port number.
Definition ssl_server.h:85
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
Definition ssl_server.h:70
std::map< CppCommon::UUID, std::shared_ptr< SSLSession > > _sessions
Definition ssl_server.h:223
virtual void onHandshaked(std::shared_ptr< SSLSession > &session)
Handle session handshaked notification.
Definition ssl_server.h:205
virtual bool DisconnectAll()
Disconnect all connected sessions.
const std::string & address() const noexcept
Get the server address.
Definition ssl_server.h:83
virtual void onStarted()
Handle server started notification.
Definition ssl_server.h:192
virtual void onConnected(std::shared_ptr< SSLSession > &session)
Handle session connected notification.
Definition ssl_server.h:200
bool option_reuse_port() const noexcept
Get the option: reuse port.
Definition ssl_server.h:103
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition ssl_server.h:74
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the server.
Definition ssl_server.h:90
bool option_keep_alive() const noexcept
Get the option: keep alive.
Definition ssl_server.h:97
uint64_t connected_sessions() const noexcept
Get the number of sessions connected to the server.
Definition ssl_server.h:88
std::shared_ptr< SSLContext > & context() noexcept
Get the server SSL context.
Definition ssl_server.h:76
void SetupNoDelay(bool enable) noexcept
Setup option: no delay.
Definition ssl_server.h:166
const CppCommon::UUID & id() const noexcept
Get the server Id.
Definition ssl_server.h:67
virtual ~SSLServer()=default
asio::ip::tcp::acceptor & acceptor() noexcept
Get the server acceptor.
Definition ssl_server.h:80
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition ssl_server.h:218
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the server.
Definition ssl_server.h:92
void SetupReusePort(bool enable) noexcept
Setup option: reuse port.
Definition ssl_server.h:180
virtual void onDisconnected(std::shared_ptr< SSLSession > &session)
Handle session disconnected notification.
Definition ssl_server.h:210
void SetupKeepAlive(bool enable) noexcept
Setup option: keep alive.
Definition ssl_server.h:157
SSLServer(const SSLServer &)=delete
virtual bool Restart()
Restart the server.
virtual bool Start()
Start the server.
asio::ip::tcp::endpoint & endpoint() noexcept
Get the server endpoint.
Definition ssl_server.h:78
bool option_reuse_address() const noexcept
Get the option: reuse address.
Definition ssl_server.h:101
virtual std::shared_ptr< SSLSession > CreateSession(const std::shared_ptr< SSLServer > &server)
Create SSL session factory method.
Definition ssl_server.h:188
InternetProtocol
Internet protocol.
Definition asio.h:66
@ IPv4
Internet Protocol version 4.
C++ Server project definitions.
Definition asio.h:56
SSL context definition.
SSL session definition.