9#ifndef CPPSERVER_ASIO_SSL_CLIENT_H
10#define CPPSERVER_ASIO_SSL_CLIENT_H
15#include "system/uuid.h"
16#include "time/timespan.h"
30class SSLClient :
public std::enable_shared_from_this<SSLClient>
64 const CppCommon::UUID&
id() const noexcept {
return _id; }
67 std::shared_ptr<Service>&
service() noexcept {
return _service; }
69 std::shared_ptr<asio::io_context>&
io_context() noexcept {
return _io_context; }
71 asio::io_context::strand&
strand() noexcept {
return _strand; }
73 std::shared_ptr<SSLContext>&
context() noexcept {
return _context; }
75 asio::ip::tcp::endpoint&
endpoint() noexcept {
return _endpoint; }
77 asio::ssl::stream<asio::ip::tcp::socket>&
stream() noexcept {
return _stream; }
79 asio::ssl::stream<asio::ip::tcp::socket>::next_layer_type&
socket() noexcept {
return _stream.next_layer(); }
82 const std::string&
address() const noexcept {
return _address; }
84 const std::string&
scheme() const noexcept {
return _scheme; }
86 int port() const noexcept {
return _port; }
89 uint64_t
bytes_pending() const noexcept {
return _bytes_pending + _bytes_sending; }
91 uint64_t
bytes_sent() const noexcept {
return _bytes_sent; }
129 virtual bool Connect(
const std::shared_ptr<TCPResolver>& resolver);
151 virtual bool ConnectAsync(
const std::shared_ptr<TCPResolver>& resolver);
169 virtual size_t Send(
const void* buffer,
size_t size);
175 virtual size_t Send(std::string_view text) {
return Send(text.data(), text.size()); }
184 virtual size_t Send(
const void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
191 virtual size_t Send(std::string_view text,
const CppCommon::Timespan& timeout) {
return Send(text.data(), text.size(), timeout); }
199 virtual bool SendAsync(
const void* buffer,
size_t size);
213 virtual size_t Receive(
void* buffer,
size_t size);
219 virtual std::string
Receive(
size_t size);
228 virtual size_t Receive(
void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
235 virtual std::string
Receive(
size_t size,
const CppCommon::Timespan& timeout);
321 virtual void onSent(
size_t sent,
size_t pending) {}
338 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
344 std::shared_ptr<Service> _service;
346 std::shared_ptr<asio::io_context> _io_context;
348 asio::io_context::strand _strand;
349 bool _strand_required;
351 std::string _address;
355 std::shared_ptr<SSLContext> _context;
356 asio::ip::tcp::endpoint _endpoint;
357 asio::ssl::stream<asio::ip::tcp::socket> _stream;
358 std::atomic<bool> _resolving;
359 std::atomic<bool> _connecting;
360 std::atomic<bool> _connected;
361 std::atomic<bool> _handshaking;
362 std::atomic<bool> _handshaked;
365 uint64_t _bytes_pending;
366 uint64_t _bytes_sending;
367 uint64_t _bytes_sent;
368 uint64_t _bytes_received;
371 size_t _receive_buffer_limit{0};
372 std::vector<uint8_t> _receive_buffer;
373 HandlerStorage _receive_storage;
376 std::mutex _send_lock;
377 size_t _send_buffer_limit{0};
378 std::vector<uint8_t> _send_buffer_main;
379 std::vector<uint8_t> _send_buffer_flush;
380 size_t _send_buffer_flush_offset;
381 HandlerStorage _send_storage;
383 bool _option_keep_alive;
384 bool _option_no_delay;
387 bool DisconnectInternal();
389 bool DisconnectInternalAsync(
bool dispatch);
400 void SendError(std::error_code ec);
uint64_t bytes_received() const noexcept
Get the number of bytes received by the client.
bool option_keep_alive() const noexcept
Get the option: keep alive.
virtual size_t Send(const void *buffer, size_t size)
Send data to the server (synchronous).
asio::ssl::stream< asio::ip::tcp::socket >::next_layer_type & socket() noexcept
Get the client socket.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
SSLClient(const std::shared_ptr< Service > &service, const std::shared_ptr< SSLContext > &context, const std::string &address, int port)
Initialize SSL client with a given Asio service, SSL context, server address and port number.
virtual void onSent(size_t sent, size_t pending)
Handle buffer sent notification.
const std::string & scheme() const noexcept
Get the scheme name.
virtual void ReceiveAsync()
Receive data from the server (asynchronous).
void SetupReceiveBufferLimit(size_t limit)
Setup option: receive buffer limit.
bool IsHandshaked() const noexcept
Is the session handshaked?
virtual void onHandshaked()
Handle session handshaked notification.
bool IsConnected() const noexcept
Is the client connected?
virtual void onEmpty()
Handle empty send buffer notification.
std::shared_ptr< asio::io_context > & io_context() noexcept
Get the Asio IO context.
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the server (asynchronous).
virtual bool ConnectAsync()
Connect the client (asynchronous).
void SetupKeepAlive(bool enable) noexcept
Setup option: keep alive.
virtual void onConnecting()
Handle client connecting notification.
asio::ssl::stream< asio::ip::tcp::socket > & stream() noexcept
Get the client SSL stream.
virtual bool SendAsync(std::string_view text)
Send text to the server (asynchronous).
virtual bool DisconnectAsync()
Disconnect the client (asynchronous).
virtual void onDisconnected()
Handle client disconnected notification.
void SetupSendBufferLimit(size_t limit)
Setup option: send buffer limit.
virtual size_t Receive(void *buffer, size_t size)
Receive data from the server (synchronous).
virtual void onHandshaking()
Handle session handshaking notification.
asio::io_context::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
void SetupSendBufferSize(size_t size)
Setup option: send buffer size.
virtual bool ReconnectAsync()
Reconnect the client (asynchronous).
virtual bool Disconnect()
Disconnect the client (synchronous).
bool option_no_delay() const noexcept
Get the option: no delay.
void SetupNoDelay(bool enable) noexcept
Setup option: no delay.
virtual size_t Send(std::string_view text)
Send text to the server (synchronous).
const CppCommon::UUID & id() const noexcept
Get the client Id.
const std::string & address() const noexcept
Get the server address.
asio::ip::tcp::endpoint & endpoint() noexcept
Get the client endpoint.
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
SSLClient(const SSLClient &)=delete
virtual bool Reconnect()
Reconnect the client (synchronous).
virtual void onReceived(const void *buffer, size_t size)
Handle buffer received notification.
virtual void onDisconnecting()
Handle client disconnecting notification.
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
virtual bool Connect()
Connect the client (synchronous).
SSLClient & operator=(SSLClient &&client)=delete
virtual void onConnected()
Handle client connected notification.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the client.
SSLClient(SSLClient &&client)=delete
SSLClient & operator=(const SSLClient &)=delete
size_t option_send_buffer_limit() const
Get the option: send buffer limit.
virtual size_t Send(std::string_view text, const CppCommon::Timespan &timeout)
Send text to the server with timeout (synchronous).
std::shared_ptr< SSLContext > & context() noexcept
Get the client SSL context.
size_t option_send_buffer_size() const
Get the option: send buffer size.
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the client.
size_t option_receive_buffer_limit() const
Get the option: receive buffer limit.
int port() const noexcept
Get the server port number.
C++ Server project definitions.