CppServer 1.0.6.0
C++ Server Library
Loading...
Searching...
No Matches
tcp_session.h
Go to the documentation of this file.
1
8
9#ifndef CPPSERVER_ASIO_TCP_SESSION_H
10#define CPPSERVER_ASIO_TCP_SESSION_H
11
12#include "service.h"
13
14#include "system/uuid.h"
15
16namespace CppServer {
17namespace Asio {
18
19class TCPServer;
20
22
27class TCPSession : public std::enable_shared_from_this<TCPSession>
28{
29 friend class TCPServer;
30
31public:
33
36 explicit TCPSession(const std::shared_ptr<TCPServer>& server);
37 TCPSession(const TCPSession&) = delete;
39 virtual ~TCPSession() = default;
40
41 TCPSession& operator=(const TCPSession&) = delete;
43
45 const CppCommon::UUID& id() const noexcept { return _id; }
46
48 std::shared_ptr<TCPServer>& server() noexcept { return _server; }
50 std::shared_ptr<asio::io_context>& io_context() noexcept { return _io_context; }
52 asio::io_context::strand& strand() noexcept { return _strand; }
54 asio::ip::tcp::socket& socket() noexcept { return _socket; }
55
57 uint64_t bytes_pending() const noexcept { return _bytes_pending + _bytes_sending; }
59 uint64_t bytes_sent() const noexcept { return _bytes_sent; }
61 uint64_t bytes_received() const noexcept { return _bytes_received; }
62
64 size_t option_receive_buffer_limit() const noexcept { return _receive_buffer_limit; }
66 size_t option_receive_buffer_size() const;
68 size_t option_send_buffer_limit() const noexcept { return _send_buffer_limit; }
70 size_t option_send_buffer_size() const;
71
73 bool IsConnected() const noexcept { return _connected; }
74
76
79 virtual bool Disconnect() { return Disconnect(false); }
80
82
87 virtual size_t Send(const void* buffer, size_t size);
89
93 virtual size_t Send(std::string_view text) { return Send(text.data(), text.size()); }
94
96
102 virtual size_t Send(const void* buffer, size_t size, const CppCommon::Timespan& timeout);
104
109 virtual size_t Send(std::string_view text, const CppCommon::Timespan& timeout) { return Send(text.data(), text.size(), timeout); }
110
112
117 virtual bool SendAsync(const void* buffer, size_t size);
119
123 virtual bool SendAsync(std::string_view text) { return SendAsync(text.data(), text.size()); }
124
126
131 virtual size_t Receive(void* buffer, size_t size);
133
137 virtual std::string Receive(size_t size);
138
140
146 virtual size_t Receive(void* buffer, size_t size, const CppCommon::Timespan& timeout);
148
153 virtual std::string Receive(size_t size, const CppCommon::Timespan& timeout);
154
156 virtual void ReceiveAsync();
157
159
165 void SetupReceiveBufferLimit(size_t limit) noexcept { _receive_buffer_limit = limit; }
167
172 void SetupReceiveBufferSize(size_t size);
174
180 void SetupSendBufferLimit(size_t limit) noexcept { _send_buffer_limit = limit; }
182
187 void SetupSendBufferSize(size_t size);
188
189protected:
191 virtual void onConnecting() {}
193 virtual void onConnected() {}
195 virtual void onDisconnecting() {}
197 virtual void onDisconnected() {}
198
200
207 virtual void onReceived(const void* buffer, size_t size) {}
209
219 virtual void onSent(size_t sent, size_t pending) {}
220
222
228 virtual void onEmpty() {}
229
231
236 virtual void onError(int error, const std::string& category, const std::string& message) {}
237
238private:
239 // Session Id
240 CppCommon::UUID _id;
241 // Server & session
242 std::shared_ptr<TCPServer> _server;
243 // Asio IO context
244 std::shared_ptr<asio::io_context> _io_context;
245 // Asio service strand for serialized handler execution
246 asio::io_context::strand _strand;
247 bool _strand_required;
248 // Session socket
249 asio::ip::tcp::socket _socket;
250 std::atomic<bool> _connected;
251 // Session statistic
252 uint64_t _bytes_pending;
253 uint64_t _bytes_sending;
254 uint64_t _bytes_sent;
255 uint64_t _bytes_received;
256 // Receive buffer
257 bool _receiving;
258 size_t _receive_buffer_limit{0};
259 std::vector<uint8_t> _receive_buffer;
260 HandlerStorage _receive_storage;
261 // Send buffer
262 bool _sending;
263 std::mutex _send_lock;
264 size_t _send_buffer_limit{0};
265 std::vector<uint8_t> _send_buffer_main;
266 std::vector<uint8_t> _send_buffer_flush;
267 size_t _send_buffer_flush_offset;
268 HandlerStorage _send_storage;
269
271 void Connect();
273
277 bool Disconnect(bool dispatch);
278
280 void TryReceive();
282 void TrySend();
283
285 void ClearBuffers();
287 void ResetServer();
288
290 void SendError(std::error_code ec);
291};
292
293} // namespace Asio
294} // namespace CppServer
295
296#endif // CPPSERVER_ASIO_TCP_SESSION_H
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
TCPSession(const std::shared_ptr< TCPServer > &server)
Initialize the session with a given server.
virtual void onConnected()
Handle session connected notification.
TCPSession & operator=(TCPSession &&)=delete
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the session.
Definition tcp_session.h:57
virtual void onDisconnecting()
Handle session disconnecting notification.
asio::io_context::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition tcp_session.h:52
asio::ip::tcp::socket & socket() noexcept
Get the session socket.
Definition tcp_session.h:54
std::shared_ptr< asio::io_context > & io_context() noexcept
Get the Asio IO context.
Definition tcp_session.h:50
uint64_t bytes_received() const noexcept
Get the number of bytes received by the session.
Definition tcp_session.h:61
std::shared_ptr< TCPServer > & server() noexcept
Get the server.
Definition tcp_session.h:48
void SetupSendBufferSize(size_t size)
Setup option: send buffer size.
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the client (asynchronous).
virtual size_t Send(const void *buffer, size_t size)
Send data to the client (synchronous).
bool IsConnected() const noexcept
Is the session connected?
Definition tcp_session.h:73
size_t option_receive_buffer_limit() const noexcept
Get the option: receive buffer limit.
Definition tcp_session.h:64
virtual size_t Send(std::string_view text)
Send text to the client (synchronous).
Definition tcp_session.h:93
virtual void onConnecting()
Handle session connecting notification.
void SetupSendBufferLimit(size_t limit) noexcept
Setup option: send buffer limit.
virtual void onEmpty()
Handle empty send buffer notification.
virtual void onReceived(const void *buffer, size_t size)
Handle buffer received notification.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the session.
Definition tcp_session.h:59
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
virtual void onDisconnected()
Handle session disconnected notification.
virtual size_t Send(std::string_view text, const CppCommon::Timespan &timeout)
Send text to the client with timeout (synchronous).
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
size_t option_send_buffer_size() const
Get the option: send buffer size.
size_t option_send_buffer_limit() const noexcept
Get the option: send buffer limit.
Definition tcp_session.h:68
TCPSession(TCPSession &&)=delete
TCPSession & operator=(const TCPSession &)=delete
virtual void onSent(size_t sent, size_t pending)
Handle buffer sent notification.
virtual bool SendAsync(std::string_view text)
Send text to the client (asynchronous).
const CppCommon::UUID & id() const noexcept
Get the session Id.
Definition tcp_session.h:45
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
virtual size_t Receive(void *buffer, size_t size)
Receive data from the client (synchronous).
virtual bool Disconnect()
Disconnect the session.
Definition tcp_session.h:79
TCPSession(const TCPSession &)=delete
virtual void ReceiveAsync()
Receive data from the client (asynchronous).
virtual ~TCPSession()=default
Asio definitions.
C++ Server project definitions.
Definition asio.h:56
Asio service definition.