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>
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(); }
129 virtual bool Connect(
const std::shared_ptr<TCPResolver>& resolver);
151 virtual bool ConnectAsync(
const std::shared_ptr<TCPResolver>& resolver);
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); }
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);
338 std::shared_ptr<Service> _service;
340 std::shared_ptr<asio::io_service> _io_service;
342 asio::io_service::strand _strand;
343 bool _strand_required;
345 std::string _address;
349 std::shared_ptr<SSLContext> _context;
350 asio::ip::tcp::endpoint _endpoint;
351 asio::ssl::stream<asio::ip::tcp::socket> _stream;
352 std::atomic<bool> _resolving;
353 std::atomic<bool> _connecting;
354 std::atomic<bool> _connected;
355 std::atomic<bool> _handshaking;
356 std::atomic<bool> _handshaked;
365 size_t _receive_buffer_limit{0};
366 std::vector<uint8_t> _receive_buffer;
367 HandlerStorage _receive_storage;
370 std::mutex _send_lock;
371 size_t _send_buffer_limit{0};
372 std::vector<uint8_t> _send_buffer_main;
373 std::vector<uint8_t> _send_buffer_flush;
374 size_t _send_buffer_flush_offset;
375 HandlerStorage _send_storage;
377 bool _option_keep_alive;
378 bool _option_no_delay;
381 bool DisconnectInternal();
383 bool DisconnectInternalAsync(
bool dispatch);
394 void SendError(std::error_code ec);
Asio allocate handler wrapper.
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.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
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.
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.
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)
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.
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.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
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.