CppServer 1.0.5.0
C++ Server Library
Loading...
Searching...
No Matches
ssl_session.h
Go to the documentation of this file.
1
9#ifndef CPPSERVER_ASIO_SSL_SESSION_H
10#define CPPSERVER_ASIO_SSL_SESSION_H
11
12#include "service.h"
13
14#include "system/uuid.h"
15
16namespace CppServer {
17namespace Asio {
18
19class SSLServer;
20
22
27class SSLSession : public std::enable_shared_from_this<SSLSession>
28{
29 friend class SSLServer;
30
31public:
33
36 explicit SSLSession(const std::shared_ptr<SSLServer>& server);
37 SSLSession(const SSLSession&) = delete;
39 virtual ~SSLSession() = default;
40
41 SSLSession& operator=(const SSLSession&) = delete;
43
45 const CppCommon::UUID& id() const noexcept { return _id; }
46
48 std::shared_ptr<SSLServer>& server() noexcept { return _server; }
50 std::shared_ptr<asio::io_service>& io_service() noexcept { return _io_service; }
52 asio::io_service::strand& strand() noexcept { return _strand; }
54 asio::ssl::stream<asio::ip::tcp::socket>& stream() noexcept { return _stream; }
56 asio::ssl::stream<asio::ip::tcp::socket>::next_layer_type& socket() noexcept { return _stream.next_layer(); }
57
59 uint64_t bytes_pending() const noexcept { return _bytes_pending + _bytes_sending; }
61 uint64_t bytes_sent() const noexcept { return _bytes_sent; }
63 uint64_t bytes_received() const noexcept { return _bytes_received; }
64
66 size_t option_receive_buffer_limit() const noexcept { return _receive_buffer_limit; }
68 size_t option_receive_buffer_size() const;
70 size_t option_send_buffer_limit() const noexcept { return _send_buffer_limit; }
72 size_t option_send_buffer_size() const;
73
75 bool IsConnected() const noexcept { return _connected; }
77 bool IsHandshaked() const noexcept { return _handshaked; }
78
80
83 virtual bool Disconnect() { return DisconnectAsync(false); }
84
86
91 virtual size_t Send(const void* buffer, size_t size);
93
97 virtual size_t Send(std::string_view text) { return Send(text.data(), text.size()); }
98
100
106 virtual size_t Send(const void* buffer, size_t size, const CppCommon::Timespan& timeout);
108
113 virtual size_t Send(std::string_view text, const CppCommon::Timespan& timeout) { return Send(text.data(), text.size(), timeout); }
114
116
121 virtual bool SendAsync(const void* buffer, size_t size);
123
127 virtual bool SendAsync(std::string_view text) { return SendAsync(text.data(), text.size()); }
128
130
135 virtual size_t Receive(void* buffer, size_t size);
137
141 virtual std::string Receive(size_t size);
142
144
150 virtual size_t Receive(void* buffer, size_t size, const CppCommon::Timespan& timeout);
152
157 virtual std::string Receive(size_t size, const CppCommon::Timespan& timeout);
158
160 virtual void ReceiveAsync();
161
163
169 void SetupReceiveBufferLimit(size_t limit) noexcept { _receive_buffer_limit = limit; }
171
176 void SetupReceiveBufferSize(size_t size);
178
184 void SetupSendBufferLimit(size_t limit) noexcept { _send_buffer_limit = limit; }
186
191 void SetupSendBufferSize(size_t size);
192
193protected:
195 virtual void onConnected() {}
197 virtual void onHandshaked() {}
199 virtual void onDisconnected() {}
200
202
209 virtual void onReceived(const void* buffer, size_t size) {}
211
221 virtual void onSent(size_t sent, size_t pending) {}
222
224
230 virtual void onEmpty() {}
231
233
238 virtual void onError(int error, const std::string& category, const std::string& message) {}
239
240private:
241 // Session Id
242 CppCommon::UUID _id;
243 // Server & session
244 std::shared_ptr<SSLServer> _server;
245 // Asio IO service
246 std::shared_ptr<asio::io_service> _io_service;
247 // Asio service strand for serialized handler execution
248 asio::io_service::strand _strand;
249 bool _strand_required;
250 // Session stream
251 asio::ssl::stream<asio::ip::tcp::socket> _stream;
252 std::atomic<bool> _connected;
253 std::atomic<bool> _handshaked;
254 // Session statistic
255 uint64_t _bytes_pending;
256 uint64_t _bytes_sending;
257 uint64_t _bytes_sent;
258 uint64_t _bytes_received;
259 // Receive buffer
260 bool _receiving;
261 size_t _receive_buffer_limit{0};
262 std::vector<uint8_t> _receive_buffer;
263 HandlerStorage _receive_storage;
264 // Send buffer
265 bool _sending;
266 std::mutex _send_lock;
267 size_t _send_buffer_limit{0};
268 std::vector<uint8_t> _send_buffer_main;
269 std::vector<uint8_t> _send_buffer_flush;
270 size_t _send_buffer_flush_offset;
271 HandlerStorage _send_storage;
272
274 void Connect();
276 void Disconnect(std::error_code ec);
278
282 bool DisconnectAsync(bool dispatch);
283
285 void TryReceive();
287 void TrySend();
288
290 void ClearBuffers();
292 void ResetServer();
293
295 void SendError(std::error_code ec);
296};
297
298} // namespace Asio
299} // namespace CppServer
300
301#endif // CPPSERVER_ASIO_SSL_SESSION_H
Asio allocate handler wrapper.
Definition memory.h:133
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the session.
Definition ssl_session.h:59
virtual void onSent(size_t sent, size_t pending)
Handle buffer sent notification.
SSLSession & operator=(SSLSession &&)=delete
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition ssl_session.h:52
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the client (asynchronous)
asio::ssl::stream< asio::ip::tcp::socket >::next_layer_type & socket() noexcept
Get the session socket.
Definition ssl_session.h:56
size_t option_send_buffer_limit() const noexcept
Get the option: send buffer limit.
Definition ssl_session.h:70
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
void SetupSendBufferSize(size_t size)
Setup option: send buffer size.
virtual void ReceiveAsync()
Receive data from the client (asynchronous)
asio::ssl::stream< asio::ip::tcp::socket > & stream() noexcept
Get the session SSL stream.
Definition ssl_session.h:54
const CppCommon::UUID & id() const noexcept
Get the session Id.
Definition ssl_session.h:45
virtual ~SSLSession()=default
SSLSession(SSLSession &&)=delete
virtual bool Disconnect()
Disconnect the session.
Definition ssl_session.h:83
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
Definition ssl_session.h:50
SSLSession(const SSLSession &)=delete
virtual void onConnected()
Handle session connected notification.
void SetupSendBufferLimit(size_t limit) noexcept
Setup option: send buffer limit.
virtual bool SendAsync(std::string_view text)
Send text to the client (asynchronous)
virtual void onReceived(const void *buffer, size_t size)
Handle buffer received notification.
uint64_t bytes_received() const noexcept
Get the number of bytes received by the session.
Definition ssl_session.h:63
std::shared_ptr< SSLServer > & server() noexcept
Get the server.
Definition ssl_session.h:48
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
virtual size_t Send(std::string_view text)
Send text to the client (synchronous)
Definition ssl_session.h:97
size_t option_send_buffer_size() const
Get the option: send buffer size.
virtual size_t Send(const void *buffer, size_t size)
Send data to the client (synchronous)
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the session.
Definition ssl_session.h:61
virtual void onEmpty()
Handle empty send buffer notification.
bool IsConnected() const noexcept
Is the session connected?
Definition ssl_session.h:75
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
bool IsHandshaked() const noexcept
Is the session handshaked?
Definition ssl_session.h:77
virtual size_t Send(std::string_view text, const CppCommon::Timespan &timeout)
Send text to the client with timeout (synchronous)
virtual size_t Receive(void *buffer, size_t size)
Receive data from the client (synchronous)
SSLSession & operator=(const SSLSession &)=delete
virtual void onHandshaked()
Handle session handshaked notification.
size_t option_receive_buffer_limit() const noexcept
Get the option: receive buffer limit.
Definition ssl_session.h:66
virtual void onDisconnected()
Handle session disconnected notification.
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
C++ Server project definitions.
Definition asio.h:56
Asio service definition.