CppServer  1.0.4.0
C++ Server Library
Public Member Functions | Protected Member Functions | Friends | List of all members
CppServer::Asio::TCPSession Class Reference

TCP session. More...

#include <tcp_session.h>

Inheritance diagram for CppServer::Asio::TCPSession:
CppServer::HTTP::HTTPSession CppServer::WS::WSSession

Public Member Functions

 TCPSession (const std::shared_ptr< TCPServer > &server)
 Initialize the session with a given server. More...
 
 TCPSession (const TCPSession &)=delete
 
 TCPSession (TCPSession &&)=delete
 
virtual ~TCPSession ()=default
 
TCPSessionoperator= (const TCPSession &)=delete
 
TCPSessionoperator= (TCPSession &&)=delete
 
const CppCommon::UUID & id () const noexcept
 Get the session Id. More...
 
std::shared_ptr< TCPServer > & server () noexcept
 Get the server. More...
 
std::shared_ptr< asio::io_service > & io_service () noexcept
 Get the Asio IO service. More...
 
asio::io_service::strand & strand () noexcept
 Get the Asio service strand for serialized handler execution. More...
 
asio::ip::tcp::socket & socket () noexcept
 Get the session socket. More...
 
uint64_t bytes_pending () const noexcept
 Get the number of bytes pending sent by the session. More...
 
uint64_t bytes_sent () const noexcept
 Get the number of bytes sent by the session. More...
 
uint64_t bytes_received () const noexcept
 Get the number of bytes received by the session. More...
 
size_t option_receive_buffer_limit () const noexcept
 Get the option: receive buffer limit. More...
 
size_t option_receive_buffer_size () const
 Get the option: receive buffer size. More...
 
size_t option_send_buffer_limit () const noexcept
 Get the option: send buffer limit. More...
 
size_t option_send_buffer_size () const
 Get the option: send buffer size. More...
 
bool IsConnected () const noexcept
 Is the session connected? More...
 
virtual bool Disconnect ()
 Disconnect the session. More...
 
virtual size_t Send (const void *buffer, size_t size)
 Send data to the client (synchronous) More...
 
virtual size_t Send (std::string_view text)
 Send text to the client (synchronous) More...
 
virtual size_t Send (const void *buffer, size_t size, const CppCommon::Timespan &timeout)
 Send data to the client with timeout (synchronous) More...
 
virtual size_t Send (std::string_view text, const CppCommon::Timespan &timeout)
 Send text to the client with timeout (synchronous) More...
 
virtual bool SendAsync (const void *buffer, size_t size)
 Send data to the client (asynchronous) More...
 
virtual bool SendAsync (std::string_view text)
 Send text to the client (asynchronous) More...
 
virtual size_t Receive (void *buffer, size_t size)
 Receive data from the client (synchronous) More...
 
virtual std::string Receive (size_t size)
 Receive text from the client (synchronous) More...
 
virtual size_t Receive (void *buffer, size_t size, const CppCommon::Timespan &timeout)
 Receive data from the client with timeout (synchronous) More...
 
virtual std::string Receive (size_t size, const CppCommon::Timespan &timeout)
 Receive text from the client with timeout (synchronous) More...
 
virtual void ReceiveAsync ()
 Receive data from the client (asynchronous) More...
 
void SetupReceiveBufferLimit (size_t limit) noexcept
 Setup option: receive buffer limit. More...
 
void SetupReceiveBufferSize (size_t size)
 Setup option: receive buffer size. More...
 
void SetupSendBufferLimit (size_t limit) noexcept
 Setup option: send buffer limit. More...
 
void SetupSendBufferSize (size_t size)
 Setup option: send buffer size. More...
 

Protected Member Functions

virtual void onConnected ()
 Handle session connected notification. More...
 
virtual void onDisconnected ()
 Handle session disconnected notification. More...
 
virtual void onReceived (const void *buffer, size_t size)
 Handle buffer received notification. More...
 
virtual void onSent (size_t sent, size_t pending)
 Handle buffer sent notification. More...
 
virtual void onEmpty ()
 Handle empty send buffer notification. More...
 
virtual void onError (int error, const std::string &category, const std::string &message)
 Handle error notification. More...
 

Friends

class TCPServer
 

Detailed Description

TCP session.

TCP session is used to read and write data from the connected TCP client.

Thread-safe.

Examples
tcp_chat_server.cpp.

Definition at line 27 of file tcp_session.h.

Constructor & Destructor Documentation

◆ TCPSession() [1/3]

CppServer::Asio::TCPSession::TCPSession ( const std::shared_ptr< TCPServer > &  server)
explicit

Initialize the session with a given server.

Parameters
server- Connected server
Examples
tcp_chat_server.cpp.

Definition at line 15 of file tcp_session.cpp.

◆ TCPSession() [2/3]

CppServer::Asio::TCPSession::TCPSession ( const TCPSession )
delete

◆ TCPSession() [3/3]

CppServer::Asio::TCPSession::TCPSession ( TCPSession &&  )
delete

◆ ~TCPSession()

virtual CppServer::Asio::TCPSession::~TCPSession ( )
virtualdefault

Member Function Documentation

◆ bytes_pending()

uint64_t CppServer::Asio::TCPSession::bytes_pending ( ) const
inlinenoexcept

Get the number of bytes pending sent by the session.

Definition at line 57 of file tcp_session.h.

◆ bytes_received()

uint64_t CppServer::Asio::TCPSession::bytes_received ( ) const
inlinenoexcept

Get the number of bytes received by the session.

Definition at line 61 of file tcp_session.h.

◆ bytes_sent()

uint64_t CppServer::Asio::TCPSession::bytes_sent ( ) const
inlinenoexcept

Get the number of bytes sent by the session.

Definition at line 59 of file tcp_session.h.

◆ Disconnect()

virtual bool CppServer::Asio::TCPSession::Disconnect ( )
inlinevirtual

Disconnect the session.

Returns
'true' if the section was successfully disconnected, 'false' if the section is already disconnected
Examples
tcp_chat_server.cpp.

Definition at line 79 of file tcp_session.h.

◆ id()

const CppCommon::UUID& CppServer::Asio::TCPSession::id ( ) const
inlinenoexcept

Get the session Id.

Examples
tcp_chat_server.cpp, and ws_chat_server.cpp.

Definition at line 45 of file tcp_session.h.

◆ io_service()

std::shared_ptr<asio::io_service>& CppServer::Asio::TCPSession::io_service ( )
inlinenoexcept

Get the Asio IO service.

Definition at line 50 of file tcp_session.h.

◆ IsConnected()

bool CppServer::Asio::TCPSession::IsConnected ( ) const
inlinenoexcept

Is the session connected?

Definition at line 73 of file tcp_session.h.

◆ onConnected()

virtual void CppServer::Asio::TCPSession::onConnected ( )
inlineprotectedvirtual

Handle session connected notification.

Examples
tcp_chat_server.cpp.

Definition at line 191 of file tcp_session.h.

◆ onDisconnected()

virtual void CppServer::Asio::TCPSession::onDisconnected ( )
inlineprotectedvirtual

Handle session disconnected notification.

Reimplemented in CppServer::WS::WSSession, and CppServer::HTTP::HTTPSession.

Examples
tcp_chat_server.cpp.

Definition at line 193 of file tcp_session.h.

◆ onEmpty()

virtual void CppServer::Asio::TCPSession::onEmpty ( )
inlineprotectedvirtual

Handle empty send buffer notification.

Notification is called when the send buffer is empty and ready for a new data to send.

This handler could be used to send another buffer to the client.

Definition at line 224 of file tcp_session.h.

◆ onError()

virtual void CppServer::Asio::TCPSession::onError ( int  error,
const std::string &  category,
const std::string &  message 
)
inlineprotectedvirtual

Handle error notification.

Parameters
error- Error code
category- Error category
message- Error message
Examples
http_server.cpp, tcp_chat_server.cpp, and ws_chat_server.cpp.

Definition at line 232 of file tcp_session.h.

◆ onReceived()

virtual void CppServer::Asio::TCPSession::onReceived ( const void *  buffer,
size_t  size 
)
inlineprotectedvirtual

Handle buffer received notification.

Notification is called when another part of buffer was received from the client.

Parameters
buffer- Received buffer
size- Received buffer size

Reimplemented in CppServer::WS::WSSession, and CppServer::HTTP::HTTPSession.

Examples
tcp_chat_server.cpp.

Definition at line 203 of file tcp_session.h.

◆ onSent()

virtual void CppServer::Asio::TCPSession::onSent ( size_t  sent,
size_t  pending 
)
inlineprotectedvirtual

Handle buffer sent notification.

Notification is called when another part of buffer was sent to the client.

This handler could be used to send another buffer to the client for instance when the pending size is zero.

Parameters
sent- Size of sent buffer
pending- Size of pending buffer

Definition at line 215 of file tcp_session.h.

◆ operator=() [1/2]

TCPSession& CppServer::Asio::TCPSession::operator= ( const TCPSession )
delete

◆ operator=() [2/2]

TCPSession& CppServer::Asio::TCPSession::operator= ( TCPSession &&  )
delete

◆ option_receive_buffer_limit()

size_t CppServer::Asio::TCPSession::option_receive_buffer_limit ( ) const
inlinenoexcept

Get the option: receive buffer limit.

Definition at line 64 of file tcp_session.h.

◆ option_receive_buffer_size()

size_t CppServer::Asio::TCPSession::option_receive_buffer_size ( ) const

Get the option: receive buffer size.

Definition at line 33 of file tcp_session.cpp.

◆ option_send_buffer_limit()

size_t CppServer::Asio::TCPSession::option_send_buffer_limit ( ) const
inlinenoexcept

Get the option: send buffer limit.

Definition at line 68 of file tcp_session.h.

◆ option_send_buffer_size()

size_t CppServer::Asio::TCPSession::option_send_buffer_size ( ) const

Get the option: send buffer size.

Definition at line 40 of file tcp_session.cpp.

◆ Receive() [1/4]

std::string CppServer::Asio::TCPSession::Receive ( size_t  size)
virtual

Receive text from the client (synchronous)

Parameters
size- Text size to receive
Returns
Received text

Definition at line 345 of file tcp_session.cpp.

◆ Receive() [2/4]

std::string CppServer::Asio::TCPSession::Receive ( size_t  size,
const CppCommon::Timespan &  timeout 
)
virtual

Receive text from the client with timeout (synchronous)

Parameters
size- Text size to receive
timeout- Timeout
Returns
Received text

Definition at line 416 of file tcp_session.cpp.

◆ Receive() [3/4]

size_t CppServer::Asio::TCPSession::Receive ( void *  buffer,
size_t  size 
)
virtual

Receive data from the client (synchronous)

Parameters
buffer- Buffer to receive
size- Buffer size to receive
Returns
Size of received data

Definition at line 309 of file tcp_session.cpp.

◆ Receive() [4/4]

size_t CppServer::Asio::TCPSession::Receive ( void *  buffer,
size_t  size,
const CppCommon::Timespan &  timeout 
)
virtual

Receive data from the client with timeout (synchronous)

Parameters
buffer- Buffer to receive
size- Buffer size to receive
timeout- Timeout
Returns
Size of received data

Definition at line 352 of file tcp_session.cpp.

◆ ReceiveAsync()

void CppServer::Asio::TCPSession::ReceiveAsync ( )
virtual

Receive data from the client (asynchronous)

Definition at line 423 of file tcp_session.cpp.

◆ Send() [1/4]

size_t CppServer::Asio::TCPSession::Send ( const void *  buffer,
size_t  size 
)
virtual

Send data to the client (synchronous)

Parameters
buffer- Buffer to send
size- Buffer size
Returns
Size of sent data

Definition at line 157 of file tcp_session.cpp.

◆ Send() [2/4]

size_t CppServer::Asio::TCPSession::Send ( const void *  buffer,
size_t  size,
const CppCommon::Timespan &  timeout 
)
virtual

Send data to the client with timeout (synchronous)

Parameters
buffer- Buffer to send
size- Buffer size
timeout- Timeout
Returns
Size of sent data

Definition at line 193 of file tcp_session.cpp.

◆ Send() [3/4]

virtual size_t CppServer::Asio::TCPSession::Send ( std::string_view  text)
inlinevirtual

Send text to the client (synchronous)

Parameters
text- Text to send
Returns
Size of sent text

Definition at line 93 of file tcp_session.h.

◆ Send() [4/4]

virtual size_t CppServer::Asio::TCPSession::Send ( std::string_view  text,
const CppCommon::Timespan &  timeout 
)
inlinevirtual

Send text to the client with timeout (synchronous)

Parameters
text- Text to send
timeout- Timeout
Returns
Size of sent text

Definition at line 109 of file tcp_session.h.

◆ SendAsync() [1/2]

bool CppServer::Asio::TCPSession::SendAsync ( const void *  buffer,
size_t  size 
)
virtual

Send data to the client (asynchronous)

Parameters
buffer- Buffer to send
size- Buffer size
Returns
'true' if the data was successfully sent, 'false' if the session is not connected
Examples
tcp_chat_server.cpp.

Definition at line 257 of file tcp_session.cpp.

◆ SendAsync() [2/2]

virtual bool CppServer::Asio::TCPSession::SendAsync ( std::string_view  text)
inlinevirtual

Send text to the client (asynchronous)

Parameters
text- Text to send
Returns
'true' if the text was successfully sent, 'false' if the session is not connected

Definition at line 123 of file tcp_session.h.

◆ server()

std::shared_ptr<TCPServer>& CppServer::Asio::TCPSession::server ( )
inlinenoexcept

Get the server.

Examples
tcp_chat_server.cpp, and ws_chat_server.cpp.

Definition at line 48 of file tcp_session.h.

◆ SetupReceiveBufferLimit()

void CppServer::Asio::TCPSession::SetupReceiveBufferLimit ( size_t  limit)
inlinenoexcept

Setup option: receive buffer limit.

The session will be disconnected if the receive buffer limit is met. Default is unlimited.

Parameters
limit- Receive buffer limit

Definition at line 165 of file tcp_session.h.

◆ SetupReceiveBufferSize()

void CppServer::Asio::TCPSession::SetupReceiveBufferSize ( size_t  size)

Setup option: receive buffer size.

This option will setup SO_RCVBUF if the OS support this feature.

Parameters
size- Receive buffer size

Definition at line 47 of file tcp_session.cpp.

◆ SetupSendBufferLimit()

void CppServer::Asio::TCPSession::SetupSendBufferLimit ( size_t  limit)
inlinenoexcept

Setup option: send buffer limit.

The session will be disconnected if the send buffer limit is met. Default is unlimited.

Parameters
limit- Send buffer limit

Definition at line 180 of file tcp_session.h.

◆ SetupSendBufferSize()

void CppServer::Asio::TCPSession::SetupSendBufferSize ( size_t  size)

Setup option: send buffer size.

This option will setup SO_SNDBUF if the OS support this feature.

Parameters
size- Send buffer size

Definition at line 53 of file tcp_session.cpp.

◆ socket()

asio::ip::tcp::socket& CppServer::Asio::TCPSession::socket ( )
inlinenoexcept

Get the session socket.

Definition at line 54 of file tcp_session.h.

◆ strand()

asio::io_service::strand& CppServer::Asio::TCPSession::strand ( )
inlinenoexcept

Get the Asio service strand for serialized handler execution.

Definition at line 52 of file tcp_session.h.

Friends And Related Function Documentation

◆ TCPServer

friend class TCPServer
friend

Definition at line 29 of file tcp_session.h.


The documentation for this class was generated from the following files: