9 #ifndef CPPSERVER_ASIO_TCP_SESSION_H
10 #define CPPSERVER_ASIO_TCP_SESSION_H
14 #include "system/uuid.h"
27 class 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_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; }
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);
215 virtual void onSent(
size_t sent,
size_t pending) {}
232 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
238 std::shared_ptr<TCPServer> _server;
240 std::shared_ptr<asio::io_service> _io_service;
242 asio::io_service::strand _strand;
243 bool _strand_required;
245 asio::ip::tcp::socket _socket;
246 std::atomic<bool> _connected;
248 uint64_t _bytes_pending;
249 uint64_t _bytes_sending;
250 uint64_t _bytes_sent;
251 uint64_t _bytes_received;
254 size_t _receive_buffer_limit{0};
255 std::vector<uint8_t> _receive_buffer;
256 HandlerStorage _receive_storage;
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;
286 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.
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the session.
asio::ip::tcp::socket & socket() noexcept
Get the session socket.
uint64_t bytes_received() const noexcept
Get the number of bytes received by the session.
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)
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.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
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)
TCPSession & operator=(const TCPSession &)=delete
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.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
TCPSession(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< TCPServer > & server() noexcept
Get the server.
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.
const CppCommon::UUID & id() const noexcept
Get the session Id.
TCPSession(const TCPSession &)=delete
TCPSession & operator=(TCPSession &&)=delete
virtual void ReceiveAsync()
Receive data from the client (asynchronous)
virtual ~TCPSession()=default
C++ Server project definitions.