9#ifndef CPPSERVER_ASIO_SSL_SESSION_H
10#define CPPSERVER_ASIO_SSL_SESSION_H
14#include "system/uuid.h"
27class SSLSession :
public std::enable_shared_from_this<SSLSession>
45 const CppCommon::UUID&
id() const noexcept {
return _id; }
48 std::shared_ptr<SSLServer>&
server() noexcept {
return _server; }
50 std::shared_ptr<asio::io_context>&
io_context() noexcept {
return _io_context; }
52 asio::io_context::strand&
strand() noexcept {
return _strand; }
54 asio::ssl::stream<asio::ip::tcp::socket>&
stream() noexcept {
return _stream; }
56 asio::ssl::stream<asio::ip::tcp::socket>::next_layer_type&
socket() noexcept {
return _stream.next_layer(); }
59 uint64_t
bytes_pending() const noexcept {
return _bytes_pending + _bytes_sending; }
61 uint64_t
bytes_sent() const noexcept {
return _bytes_sent; }
83 virtual bool Disconnect() {
return DisconnectAsync(
false); }
91 virtual size_t Send(
const void* buffer,
size_t size);
97 virtual size_t Send(std::string_view text) {
return Send(text.data(), text.size()); }
106 virtual size_t Send(
const void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
113 virtual size_t Send(std::string_view text,
const CppCommon::Timespan& timeout) {
return Send(text.data(), text.size(), timeout); }
121 virtual bool SendAsync(
const void* buffer,
size_t size);
135 virtual size_t Receive(
void* buffer,
size_t size);
141 virtual std::string
Receive(
size_t size);
150 virtual size_t Receive(
void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
157 virtual std::string
Receive(
size_t size,
const CppCommon::Timespan& timeout);
227 virtual void onSent(
size_t sent,
size_t pending) {}
244 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
250 std::shared_ptr<SSLServer> _server;
252 std::shared_ptr<asio::io_context> _io_context;
254 asio::io_context::strand _strand;
255 bool _strand_required;
257 asio::ssl::stream<asio::ip::tcp::socket> _stream;
258 std::atomic<bool> _connected;
259 std::atomic<bool> _handshaked;
261 uint64_t _bytes_pending;
262 uint64_t _bytes_sending;
263 uint64_t _bytes_sent;
264 uint64_t _bytes_received;
267 size_t _receive_buffer_limit{0};
268 std::vector<uint8_t> _receive_buffer;
269 HandlerStorage _receive_storage;
272 std::mutex _send_lock;
273 size_t _send_buffer_limit{0};
274 std::vector<uint8_t> _send_buffer_main;
275 std::vector<uint8_t> _send_buffer_flush;
276 size_t _send_buffer_flush_offset;
277 HandlerStorage _send_storage;
288 bool DisconnectAsync(
bool dispatch);
301 void SendError(std::error_code ec);
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the session.
virtual void onSent(size_t sent, size_t pending)
Handle buffer sent notification.
SSLSession & operator=(SSLSession &&)=delete
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the client (asynchronous).
asio::ssl::stream< asio::ip::tcp::socket >::next_layer_type & socket() noexcept
Get the session socket.
size_t option_send_buffer_limit() const noexcept
Get the option: send buffer limit.
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
void SetupSendBufferSize(size_t size)
Setup option: send buffer size.
std::shared_ptr< asio::io_context > & io_context() noexcept
Get the Asio IO context.
asio::io_context::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
virtual void ReceiveAsync()
Receive data from the client (asynchronous).
asio::ssl::stream< asio::ip::tcp::socket > & stream() noexcept
Get the session SSL stream.
const CppCommon::UUID & id() const noexcept
Get the session Id.
virtual ~SSLSession()=default
SSLSession(SSLSession &&)=delete
virtual void onDisconnecting()
Handle session disconnecting notification.
virtual bool Disconnect()
Disconnect the session.
SSLSession(const SSLSession &)=delete
virtual void onConnected()
Handle session connected notification.
void SetupSendBufferLimit(size_t limit) noexcept
Setup option: send buffer limit.
virtual bool SendAsync(std::string_view text)
Send text to the client (asynchronous).
virtual void onReceived(const void *buffer, size_t size)
Handle buffer received notification.
uint64_t bytes_received() const noexcept
Get the number of bytes received by the session.
std::shared_ptr< SSLServer > & server() noexcept
Get the server.
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
virtual size_t Send(std::string_view text)
Send text to the client (synchronous).
virtual void onConnecting()
Handle session connecting notification.
virtual void onHandshaking()
Handle session handshaking notification.
size_t option_send_buffer_size() const
Get the option: send buffer size.
virtual size_t Send(const void *buffer, size_t size)
Send data to the client (synchronous).
SSLSession(const std::shared_ptr< SSLServer > &server)
Initialize the session with a given server.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the session.
virtual void onEmpty()
Handle empty send buffer notification.
bool IsConnected() const noexcept
Is the session connected?
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
bool IsHandshaked() const noexcept
Is the session handshaked?
virtual size_t Send(std::string_view text, const CppCommon::Timespan &timeout)
Send text to the client with timeout (synchronous).
virtual size_t Receive(void *buffer, size_t size)
Receive data from the client (synchronous).
SSLSession & operator=(const SSLSession &)=delete
virtual void onHandshaked()
Handle session handshaked notification.
size_t option_receive_buffer_limit() const noexcept
Get the option: receive buffer limit.
virtual void onDisconnected()
Handle session disconnected notification.
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
C++ Server project definitions.