CppServer 1.0.5.0
C++ Server Library
Loading...
Searching...
No Matches
ssl_client.h
Go to the documentation of this file.
1
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;
58 virtual ~SSLClient();
59
60 SSLClient& operator=(const SSLClient&) = 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_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(); }
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 onConnected() {}
291 virtual void onHandshaked() {}
293 virtual void onDisconnected() {}
294
296
303 virtual void onReceived(const void* buffer, size_t size) {}
305
315 virtual void onSent(size_t sent, size_t pending) {}
316
318
324 virtual void onEmpty() {}
325
327
332 virtual void onError(int error, const std::string& category, const std::string& message) {}
333
334private:
335 // Client Id
336 CppCommon::UUID _id;
337 // Asio service
338 std::shared_ptr<Service> _service;
339 // Asio IO service
340 std::shared_ptr<asio::io_service> _io_service;
341 // Asio service strand for serialised handler execution
342 asio::io_service::strand _strand;
343 bool _strand_required;
344 // Server address, scheme & port
345 std::string _address;
346 std::string _scheme;
347 int _port;
348 // Server SSL context, endpoint & client stream
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;
357 HandlerStorage _connect_storage;
358 // Client statistic
359 uint64_t _bytes_pending;
360 uint64_t _bytes_sending;
361 uint64_t _bytes_sent;
362 uint64_t _bytes_received;
363 // Receive buffer
364 bool _receiving;
365 size_t _receive_buffer_limit{0};
366 std::vector<uint8_t> _receive_buffer;
367 HandlerStorage _receive_storage;
368 // Send buffer
369 bool _sending;
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;
376 // Options
377 bool _option_keep_alive;
378 bool _option_no_delay;
379
381 bool DisconnectInternal();
383 bool DisconnectInternalAsync(bool dispatch);
384
386 void TryReceive();
388 void TrySend();
389
391 void ClearBuffers();
392
394 void SendError(std::error_code ec);
395};
396
399} // namespace Asio
400} // namespace CppServer
401
402#endif // CPPSERVER_ASIO_SSL_CLIENT_H
Asio allocate handler wrapper.
Definition memory.h:133
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
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition ssl_client.h:71
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition ssl_client.h:332
virtual void onSent(size_t sent, size_t pending)
Handle buffer sent notification.
Definition ssl_client.h:315
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:291
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:324
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
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:293
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)
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:303
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:289
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.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
Definition ssl_client.h:69
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
C++ Server project definitions.
Definition asio.h:56
SSL context definition.
TCP resolver definition.