9 #ifndef CPPSERVER_ASIO_SSL_SERVER_H
10 #define CPPSERVER_ASIO_SSL_SERVER_H
15 #include "system/uuid.h"
19 #include <shared_mutex>
31 class SSLServer :
public std::enable_shared_from_this<SSLServer>
67 const CppCommon::UUID&
id() const noexcept {
return _id; }
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; }
83 const std::string&
address() const noexcept {
return _address; }
85 int port() const noexcept {
return _port; }
92 uint64_t
bytes_sent() const noexcept {
return _bytes_sent; }
112 virtual bool Start();
130 virtual bool Multicast(
const void* buffer,
size_t size);
149 std::shared_ptr<SSLSession>
FindSession(
const CppCommon::UUID&
id);
188 virtual std::shared_ptr<SSLSession>
CreateSession(
const std::shared_ptr<SSLServer>& server) {
return std::make_shared<SSLSession>(server); }
218 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
223 std::map<CppCommon::UUID, std::shared_ptr<SSLSession>>
_sessions;
229 std::shared_ptr<Service> _service;
231 std::shared_ptr<asio::io_service> _io_service;
233 asio::io_service::strand _strand;
234 bool _strand_required;
236 std::string _address;
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;
246 uint64_t _bytes_pending;
247 uint64_t _bytes_sent;
248 uint64_t _bytes_received;
250 bool _option_keep_alive;
251 bool _option_no_delay;
252 bool _option_reuse_address;
253 bool _option_reuse_port;
259 void RegisterSession();
264 void UnregisterSession(
const CppCommon::UUID&
id);
270 void SendError(std::error_code ec);
bool IsStarted() const noexcept
Is the server started?
std::shared_mutex _sessions_lock
void SetupReuseAddress(bool enable) noexcept
Setup option: reuse address.
bool option_no_delay() const noexcept
Get the option: no delay.
std::shared_ptr< SSLSession > FindSession(const CppCommon::UUID &id)
Find a session with a given Id.
virtual void onStopped()
Handle server stopped notification.
virtual bool Multicast(std::string_view text)
Multicast text to all connected sessions.
uint64_t bytes_received() const noexcept
Get the number of bytes received by the server.
SSLServer(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.
std::map< CppCommon::UUID, std::shared_ptr< SSLSession > > _sessions
virtual void onHandshaked(std::shared_ptr< SSLSession > &session)
Handle session handshaked notification.
virtual bool DisconnectAll()
Disconnect all connected sessions.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
virtual void onStarted()
Handle server started notification.
virtual void onConnected(std::shared_ptr< SSLSession > &session)
Handle session connected notification.
bool option_reuse_port() const noexcept
Get the option: reuse port.
std::shared_ptr< SSLContext > & context() noexcept
Get the server SSL context.
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the server.
SSLServer & operator=(const SSLServer &)=delete
bool option_keep_alive() const noexcept
Get the option: keep alive.
asio::ip::tcp::endpoint & endpoint() noexcept
Get the server endpoint.
uint64_t connected_sessions() const noexcept
Get the number of sessions connected to the server.
void SetupNoDelay(bool enable) noexcept
Setup option: no delay.
virtual ~SSLServer()=default
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the server.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
void SetupReusePort(bool enable) noexcept
Setup option: reuse port.
asio::ip::tcp::acceptor & acceptor() noexcept
Get the server acceptor.
virtual void onDisconnected(std::shared_ptr< SSLSession > &session)
Handle session disconnected notification.
const std::string & address() const noexcept
Get the server address.
void SetupKeepAlive(bool enable) noexcept
Setup option: keep alive.
SSLServer(const std::shared_ptr< Service > &service, const std::shared_ptr< SSLContext > &context, int port, InternetProtocol protocol=InternetProtocol::IPv4)
Initialize SSL server with a given Asio service, SSL context and port number.
SSLServer(const SSLServer &)=delete
virtual bool Restart()
Restart the server.
virtual std::shared_ptr< SSLSession > CreateSession(const std::shared_ptr< SSLServer > &server)
Create SSL session factory method.
SSLServer & operator=(SSLServer &&)=delete
const CppCommon::UUID & id() const noexcept
Get the server Id.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
virtual bool Start()
Start the server.
bool option_reuse_address() const noexcept
Get the option: reuse address.
InternetProtocol
Internet protocol.
@ IPv4
Internet Protocol version 4.
C++ Server project definitions.