CppServer 1.0.5.0
C++ Server Library
Loading...
Searching...
No Matches
tcp_session.h
Go to the documentation of this file.
1
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_service>& io_service() noexcept { return _io_service; }
52 asio::io_service::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 onConnected() {}
193 virtual void onDisconnected() {}
194
196
203 virtual void onReceived(const void* buffer, size_t size) {}
205
215 virtual void onSent(size_t sent, size_t pending) {}
216
218
224 virtual void onEmpty() {}
225
227
232 virtual void onError(int error, const std::string& category, const std::string& message) {}
233
234private:
235 // Session Id
236 CppCommon::UUID _id;
237 // Server & session
238 std::shared_ptr<TCPServer> _server;
239 // Asio IO service
240 std::shared_ptr<asio::io_service> _io_service;
241 // Asio service strand for serialized handler execution
242 asio::io_service::strand _strand;
243 bool _strand_required;
244 // Session socket
245 asio::ip::tcp::socket _socket;
246 std::atomic<bool> _connected;
247 // Session statistic
248 uint64_t _bytes_pending;
249 uint64_t _bytes_sending;
250 uint64_t _bytes_sent;
251 uint64_t _bytes_received;
252 // Receive buffer
253 bool _receiving;
254 size_t _receive_buffer_limit{0};
255 std::vector<uint8_t> _receive_buffer;
256 HandlerStorage _receive_storage;
257 // Send buffer
258 bool _sending;
259 std::mutex _send_lock;
260 size_t _send_buffer_limit{0};
261 std::vector<uint8_t> _send_buffer_main;
262 std::vector<uint8_t> _send_buffer_flush;
263 size_t _send_buffer_flush_offset;
264 HandlerStorage _send_storage;
265
267 void Connect();
269
273 bool Disconnect(bool dispatch);
274
276 void TryReceive();
278 void TrySend();
279
281 void ClearBuffers();
283 void ResetServer();
284
286 void SendError(std::error_code ec);
287};
288
289} // namespace Asio
290} // namespace CppServer
291
292#endif // CPPSERVER_ASIO_TCP_SESSION_H
Asio allocate handler wrapper.
Definition memory.h:133
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
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
asio::ip::tcp::socket & socket() noexcept
Get the session socket.
Definition tcp_session.h:54
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
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
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition tcp_session.h:52
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)
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
Definition tcp_session.h:50
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
C++ Server project definitions.
Definition asio.h:56
Asio service definition.