9 #ifndef CPPSERVER_ASIO_SSL_CLIENT_H
10 #define CPPSERVER_ASIO_SSL_CLIENT_H
15 #include "system/uuid.h"
16 #include "time/timespan.h"
30 class 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_service>&
io_service() noexcept {
return _io_service; }
71 asio::io_service::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);
315 virtual void onSent(
size_t sent,
size_t pending) {}
332 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
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;
359 uint64_t _bytes_pending;
360 uint64_t _bytes_sending;
361 uint64_t _bytes_sent;
362 uint64_t _bytes_received;
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);
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
const std::string & scheme() const noexcept
Get the scheme name.
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.
const std::string & address() const noexcept
Get the server address.
virtual size_t Send(const void *buffer, size_t size)
Send data to the server (synchronous)
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.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
SSLClient & operator=(SSLClient &&client)=delete
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.
asio::ssl::stream< asio::ip::tcp::socket > & stream() noexcept
Get the client SSL stream.
SSLClient & operator=(const SSLClient &)=delete
std::shared_ptr< SSLContext > & context() noexcept
Get the client SSL context.
bool IsConnected() const noexcept
Is the client connected?
virtual void onEmpty()
Handle empty send buffer notification.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
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 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)
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
const CppCommon::UUID & id() const noexcept
Get the client Id.
asio::ip::tcp::endpoint & endpoint() noexcept
Get the client endpoint.
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)
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
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)
asio::ssl::stream< asio::ip::tcp::socket >::next_layer_type & socket() noexcept
Get the client socket.
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.