9#ifndef CPPSERVER_ASIO_TCP_SESSION_H
10#define CPPSERVER_ASIO_TCP_SESSION_H
14#include "system/uuid.h"
27class TCPSession :
public std::enable_shared_from_this<TCPSession>
45 const CppCommon::UUID&
id() const noexcept {
return _id; }
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; }
57 uint64_t
bytes_pending() const noexcept {
return _bytes_pending + _bytes_sending; }
59 uint64_t
bytes_sent() const noexcept {
return _bytes_sent; }
87 virtual size_t Send(
const void* buffer,
size_t size);
93 virtual size_t Send(std::string_view text) {
return Send(text.data(), text.size()); }
102 virtual size_t Send(
const void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
109 virtual size_t Send(std::string_view text,
const CppCommon::Timespan& timeout) {
return Send(text.data(), text.size(), timeout); }
117 virtual bool SendAsync(
const void* buffer,
size_t size);
131 virtual size_t Receive(
void* buffer,
size_t size);
137 virtual std::string
Receive(
size_t size);
146 virtual size_t Receive(
void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
153 virtual std::string
Receive(
size_t size,
const CppCommon::Timespan& timeout);
219 virtual void onSent(
size_t sent,
size_t pending) {}
236 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
242 std::shared_ptr<TCPServer> _server;
244 std::shared_ptr<asio::io_context> _io_context;
246 asio::io_context::strand _strand;
247 bool _strand_required;
249 asio::ip::tcp::socket _socket;
250 std::atomic<bool> _connected;
252 uint64_t _bytes_pending;
253 uint64_t _bytes_sending;
254 uint64_t _bytes_sent;
255 uint64_t _bytes_received;
258 size_t _receive_buffer_limit{0};
259 std::vector<uint8_t> _receive_buffer;
260 HandlerStorage _receive_storage;
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;
290 void SendError(std::error_code ec);
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.
virtual void onDisconnecting()
Handle session disconnecting notification.
asio::io_context::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
asio::ip::tcp::socket & socket() noexcept
Get the session socket.
std::shared_ptr< asio::io_context > & io_context() noexcept
Get the Asio IO context.
uint64_t bytes_received() const noexcept
Get the number of bytes received by the session.
std::shared_ptr< TCPServer > & server() noexcept
Get the server.
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?
size_t option_receive_buffer_limit() const noexcept
Get the option: receive buffer limit.
virtual size_t Send(std::string_view text)
Send text to the client (synchronous).
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.
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.
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.
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.
TCPSession(const TCPSession &)=delete
virtual void ReceiveAsync()
Receive data from the client (asynchronous).
virtual ~TCPSession()=default
C++ Server project definitions.