CppServer 1.0.6.0
C++ Server Library
Loading...
Searching...
No Matches
ssl_client.h
Go to the documentation of this file.
1
8
9#ifndef CPPSERVER_ASIO_SSL_CLIENT_H
10#define CPPSERVER_ASIO_SSL_CLIENT_H
11
12#include "ssl_context.h"
13#include "tcp_resolver.h"
14
15#include "system/uuid.h"
16#include "time/timespan.h"
17
18#include <mutex>
19#include <vector>
20
21namespace CppServer {
22namespace Asio {
23
25
30class SSLClient : public std::enable_shared_from_this<SSLClient>
31{
32public:
34
40 SSLClient(const std::shared_ptr<Service>& service, const std::shared_ptr<SSLContext>& context, const std::string& address, int port);
42
48 SSLClient(const std::shared_ptr<Service>& service, const std::shared_ptr<SSLContext>& context, const std::string& address, const std::string& scheme);
50
55 SSLClient(const std::shared_ptr<Service>& service, const std::shared_ptr<SSLContext>& context, const asio::ip::tcp::endpoint& endpoint);
56 SSLClient(const SSLClient&) = delete;
57 SSLClient(SSLClient&& client) = delete;
58 virtual ~SSLClient();
59
60 SSLClient& operator=(const SSLClient&) = delete;
61 SSLClient& operator=(SSLClient&& client) = delete;
62
64 const CppCommon::UUID& id() const noexcept { return _id; }
65
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(); }
80
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; }
87
89 uint64_t bytes_pending() const noexcept { return _bytes_pending + _bytes_sending; }
91 uint64_t bytes_sent() const noexcept { return _bytes_sent; }
93 uint64_t bytes_received() const noexcept { return _bytes_received; }
94
96 bool option_keep_alive() const noexcept { return _option_keep_alive; }
98 bool option_no_delay() const noexcept { return _option_no_delay; }
100 size_t option_receive_buffer_limit() const { return _receive_buffer_limit; }
102 size_t option_receive_buffer_size() const;
104 size_t option_send_buffer_limit() const { return _send_buffer_limit; }
106 size_t option_send_buffer_size() const;
107
109 bool IsConnected() const noexcept { return _connected; }
111 bool IsHandshaked() const noexcept { return _handshaked; }
112
114
120 virtual bool Connect();
122
129 virtual bool Connect(const std::shared_ptr<TCPResolver>& resolver);
131
134 virtual bool Disconnect() { return DisconnectInternal(); }
136
139 virtual bool Reconnect();
140
142
145 virtual bool ConnectAsync();
147
151 virtual bool ConnectAsync(const std::shared_ptr<TCPResolver>& resolver);
153
156 virtual bool DisconnectAsync() { return DisconnectInternalAsync(false); }
158
161 virtual bool ReconnectAsync();
162
164
169 virtual size_t Send(const void* buffer, size_t size);
171
175 virtual size_t Send(std::string_view text) { return Send(text.data(), text.size()); }
176
178
184 virtual size_t Send(const void* buffer, size_t size, const CppCommon::Timespan& timeout);
186
191 virtual size_t Send(std::string_view text, const CppCommon::Timespan& timeout) { return Send(text.data(), text.size(), timeout); }
192
194
199 virtual bool SendAsync(const void* buffer, size_t size);
201
205 virtual bool SendAsync(std::string_view text) { return SendAsync(text.data(), text.size()); }
206
208
213 virtual size_t Receive(void* buffer, size_t size);
215
219 virtual std::string Receive(size_t size);
220
222
228 virtual size_t Receive(void* buffer, size_t size, const CppCommon::Timespan& timeout);
230
235 virtual std::string Receive(size_t size, const CppCommon::Timespan& timeout);
236
238 virtual void ReceiveAsync();
239
241
246 void SetupKeepAlive(bool enable) noexcept { _option_keep_alive = enable; }
248
255 void SetupNoDelay(bool enable) noexcept { _option_no_delay = enable; }
257
263 void SetupReceiveBufferLimit(size_t limit) { _receive_buffer_limit = limit; }
265
270 void SetupReceiveBufferSize(size_t size);
272
278 void SetupSendBufferLimit(size_t limit) { _send_buffer_limit = limit; }
280
285 void SetupSendBufferSize(size_t size);
286
287protected:
289 virtual void onConnecting() {}
291 virtual void onConnected() {}
293 virtual void onHandshaking() {}
295 virtual void onHandshaked() {}
297 virtual void onDisconnecting() {}
299 virtual void onDisconnected() {}
300
302
309 virtual void onReceived(const void* buffer, size_t size) {}
311
321 virtual void onSent(size_t sent, size_t pending) {}
322
324
330 virtual void onEmpty() {}
331
333
338 virtual void onError(int error, const std::string& category, const std::string& message) {}
339
340private:
341 // Client Id
342 CppCommon::UUID _id;
343 // Asio service
344 std::shared_ptr<Service> _service;
345 // Asio IO context
346 std::shared_ptr<asio::io_context> _io_context;
347 // Asio service strand for serialised handler execution
348 asio::io_context::strand _strand;
349 bool _strand_required;
350 // Server address, scheme & port
351 std::string _address;
352 std::string _scheme;
353 int _port;
354 // Server SSL context, endpoint & client stream
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;
363 HandlerStorage _connect_storage;
364 // Client statistic
365 uint64_t _bytes_pending;
366 uint64_t _bytes_sending;
367 uint64_t _bytes_sent;
368 uint64_t _bytes_received;
369 // Receive buffer
370 bool _receiving;
371 size_t _receive_buffer_limit{0};
372 std::vector<uint8_t> _receive_buffer;
373 HandlerStorage _receive_storage;
374 // Send buffer
375 bool _sending;
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;
382 // Options
383 bool _option_keep_alive;
384 bool _option_no_delay;
385
387 bool DisconnectInternal();
389 bool DisconnectInternalAsync(bool dispatch);
390
392 void TryReceive();
394 void TrySend();
395
397 void ClearBuffers();
398
400 void SendError(std::error_code ec);
401};
402
404
405} // namespace Asio
406} // namespace CppServer
407
408#endif // CPPSERVER_ASIO_SSL_CLIENT_H
Asio handler storage.
Definition memory.h:27
uint64_t bytes_received() const noexcept
Get the number of bytes received by the client.
Definition ssl_client.h:93
bool option_keep_alive() const noexcept
Get the option: keep alive.
Definition ssl_client.h:96
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.
Definition ssl_client.h:79
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition ssl_client.h:338
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.
Definition ssl_client.h:321
const std::string & scheme() const noexcept
Get the scheme name.
Definition ssl_client.h:84
virtual void ReceiveAsync()
Receive data from the server (asynchronous).
void SetupReceiveBufferLimit(size_t limit)
Setup option: receive buffer limit.
Definition ssl_client.h:263
bool IsHandshaked() const noexcept
Is the session handshaked?
Definition ssl_client.h:111
virtual void onHandshaked()
Handle session handshaked notification.
Definition ssl_client.h:295
bool IsConnected() const noexcept
Is the client connected?
Definition ssl_client.h:109
virtual void onEmpty()
Handle empty send buffer notification.
Definition ssl_client.h:330
std::shared_ptr< asio::io_context > & io_context() noexcept
Get the Asio IO context.
Definition ssl_client.h:69
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.
Definition ssl_client.h:246
virtual void onConnecting()
Handle client connecting notification.
Definition ssl_client.h:289
asio::ssl::stream< asio::ip::tcp::socket > & stream() noexcept
Get the client SSL stream.
Definition ssl_client.h:77
virtual bool SendAsync(std::string_view text)
Send text to the server (asynchronous).
Definition ssl_client.h:205
virtual bool DisconnectAsync()
Disconnect the client (asynchronous).
Definition ssl_client.h:156
virtual void onDisconnected()
Handle client disconnected notification.
Definition ssl_client.h:299
void SetupSendBufferLimit(size_t limit)
Setup option: send buffer limit.
Definition ssl_client.h:278
virtual size_t Receive(void *buffer, size_t size)
Receive data from the server (synchronous).
virtual void onHandshaking()
Handle session handshaking notification.
Definition ssl_client.h:293
asio::io_context::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition ssl_client.h:71
void SetupSendBufferSize(size_t size)
Setup option: send buffer size.
virtual bool ReconnectAsync()
Reconnect the client (asynchronous).
virtual bool Disconnect()
Disconnect the client (synchronous).
Definition ssl_client.h:134
bool option_no_delay() const noexcept
Get the option: no delay.
Definition ssl_client.h:98
void SetupNoDelay(bool enable) noexcept
Setup option: no delay.
Definition ssl_client.h:255
virtual size_t Send(std::string_view text)
Send text to the server (synchronous).
Definition ssl_client.h:175
const CppCommon::UUID & id() const noexcept
Get the client Id.
Definition ssl_client.h:64
const std::string & address() const noexcept
Get the server address.
Definition ssl_client.h:82
asio::ip::tcp::endpoint & endpoint() noexcept
Get the client endpoint.
Definition ssl_client.h:75
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
Definition ssl_client.h:67
SSLClient(const SSLClient &)=delete
virtual bool Reconnect()
Reconnect the client (synchronous).
virtual void onReceived(const void *buffer, size_t size)
Handle buffer received notification.
Definition ssl_client.h:309
virtual void onDisconnecting()
Handle client disconnecting notification.
Definition ssl_client.h:297
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.
Definition ssl_client.h:291
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the client.
Definition ssl_client.h:91
SSLClient(SSLClient &&client)=delete
SSLClient & operator=(const SSLClient &)=delete
size_t option_send_buffer_limit() const
Get the option: send buffer limit.
Definition ssl_client.h:104
virtual size_t Send(std::string_view text, const CppCommon::Timespan &timeout)
Send text to the server with timeout (synchronous).
Definition ssl_client.h:191
std::shared_ptr< SSLContext > & context() noexcept
Get the client SSL context.
Definition ssl_client.h:73
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.
Definition ssl_client.h:89
size_t option_receive_buffer_limit() const
Get the option: receive buffer limit.
Definition ssl_client.h:100
int port() const noexcept
Get the server port number.
Definition ssl_client.h:86
Asio definitions.
C++ Server project definitions.
Definition asio.h:56
SSL context definition.
TCP resolver definition.