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>
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(); }
83 virtual bool Disconnect() {
return DisconnectAsync(
false); }
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); }
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);
244 std::shared_ptr<SSLServer> _server;
246 std::shared_ptr<asio::io_service> _io_service;
248 asio::io_service::strand _strand;
249 bool _strand_required;
251 asio::ssl::stream<asio::ip::tcp::socket> _stream;
252 std::atomic<bool> _connected;
253 std::atomic<bool> _handshaked;
261 size_t _receive_buffer_limit{0};
262 std::vector<uint8_t> _receive_buffer;
263 HandlerStorage _receive_storage;
266 std::mutex _send_lock;
267 size_t _send_buffer_limit{0};
268 std::vector<uint8_t> _send_buffer_main;
269 std::vector<uint8_t> _send_buffer_flush;
270 size_t _send_buffer_flush_offset;
271 HandlerStorage _send_storage;
282 bool DisconnectAsync(
bool dispatch);
295 void SendError(std::error_code ec);
Asio allocate handler wrapper.
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
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
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.
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 bool Disconnect()
Disconnect the session.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
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)
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)
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.