9 #ifndef CPPSERVER_ASIO_SSL_SESSION_H
10 #define CPPSERVER_ASIO_SSL_SESSION_H
14 #include "system/uuid.h"
27 class 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_service>&
io_service() noexcept {
return _io_service; }
52 asio::io_service::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);
221 virtual void onSent(
size_t sent,
size_t pending) {}
238 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
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;
255 uint64_t _bytes_pending;
256 uint64_t _bytes_sending;
257 uint64_t _bytes_sent;
258 uint64_t _bytes_received;
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);
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the session.
SSLSession & operator=(SSLSession &&)=delete
virtual void onSent(size_t sent, size_t pending)
Handle buffer sent notification.
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the client (asynchronous)
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)
const CppCommon::UUID & id() const noexcept
Get the session Id.
virtual ~SSLSession()=default
SSLSession(SSLSession &&)=delete
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.
std::shared_ptr< SSLServer > & server() noexcept
Get the server.
uint64_t bytes_received() const noexcept
Get the number of bytes received by the session.
asio::ssl::stream< asio::ip::tcp::socket >::next_layer_type & socket() noexcept
Get the session socket.
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)
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
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.
asio::ssl::stream< asio::ip::tcp::socket > & stream() noexcept
Get the session SSL stream.
SSLSession & operator=(const SSLSession &)=delete
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)
virtual void onHandshaked()
Handle session handshaked notification.
size_t option_receive_buffer_limit() const noexcept
Get the option: receive buffer limit.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
virtual void onDisconnected()
Handle session disconnected notification.
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
C++ Server project definitions.