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

TCP client. More...

#include <tcp_client.h>

Inheritance diagram for CppServer::Asio::TCPClient:
CppServer::HTTP::HTTPClient CppServer::HTTP::HTTPClientEx CppServer::WS::WSClient

Public Member Functions

 TCPClient (const std::shared_ptr< Service > &service, const std::string &address, int port)
 Initialize TCP client with a given Asio service, server address and port number. More...
 
 TCPClient (const std::shared_ptr< Service > &service, const std::string &address, const std::string &scheme)
 Initialize TCP client with a given Asio service, server address and scheme name. More...
 
 TCPClient (const std::shared_ptr< Service > &service, const asio::ip::tcp::endpoint &endpoint)
 Initialize TCP client with a given Asio service and endpoint. More...
 
 TCPClient (const TCPClient &)=delete
 
 TCPClient (TCPClient &&)=delete
 
virtual ~TCPClient ()=default
 
TCPClientoperator= (const TCPClient &)=delete
 
TCPClientoperator= (TCPClient &&)=delete
 
const CppCommon::UUID & id () const noexcept
 Get the client Id. More...
 
std::shared_ptr< Service > & service () noexcept
 Get the Asio service. 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::endpoint & endpoint () noexcept
 Get the client endpoint. More...
 
asio::ip::tcp::socket & socket () noexcept
 Get the client socket. More...
 
const std::string & address () const noexcept
 Get the server address. More...
 
const std::string & scheme () const noexcept
 Get the scheme name. More...
 
int port () const noexcept
 Get the server port number. More...
 
uint64_t bytes_pending () const noexcept
 Get the number of bytes pending sent by the client. More...
 
uint64_t bytes_sent () const noexcept
 Get the number of bytes sent by the client. More...
 
uint64_t bytes_received () const noexcept
 Get the number of bytes received by the client. More...
 
bool option_keep_alive () const noexcept
 Get the option: keep alive. More...
 
bool option_no_delay () const noexcept
 Get the option: no delay. 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 client connected? More...
 
virtual bool Connect ()
 Connect the client (synchronous) More...
 
virtual bool Connect (const std::shared_ptr< TCPResolver > &resolver)
 Connect the client using the given DNS resolver (synchronous) More...
 
virtual bool Disconnect ()
 Disconnect the client (synchronous) More...
 
virtual bool Reconnect ()
 Reconnect the client (synchronous) More...
 
virtual bool ConnectAsync ()
 Connect the client (asynchronous) More...
 
virtual bool ConnectAsync (const std::shared_ptr< TCPResolver > &resolver)
 Connect the client using the given DNS resolver (asynchronous) More...
 
virtual bool DisconnectAsync ()
 Disconnect the client (asynchronous) More...
 
virtual bool ReconnectAsync ()
 Reconnect the client (asynchronous) More...
 
virtual size_t Send (const void *buffer, size_t size)
 Send data to the server (synchronous) More...
 
virtual size_t Send (std::string_view text)
 Send text to the server (synchronous) More...
 
virtual size_t Send (const void *buffer, size_t size, const CppCommon::Timespan &timeout)
 Send data to the server with timeout (synchronous) More...
 
virtual size_t Send (std::string_view text, const CppCommon::Timespan &timeout)
 Send text to the server with timeout (synchronous) More...
 
virtual bool SendAsync (const void *buffer, size_t size)
 Send data to the server (asynchronous) More...
 
virtual bool SendAsync (std::string_view text)
 Send text to the server (asynchronous) More...
 
virtual size_t Receive (void *buffer, size_t size)
 Receive data from the server (synchronous) More...
 
virtual std::string Receive (size_t size)
 Receive text from the server (synchronous) More...
 
virtual size_t Receive (void *buffer, size_t size, const CppCommon::Timespan &timeout)
 Receive data from the server with timeout (synchronous) More...
 
virtual std::string Receive (size_t size, const CppCommon::Timespan &timeout)
 Receive text from the server with timeout (synchronous) More...
 
virtual void ReceiveAsync ()
 Receive data from the server (asynchronous) More...
 
void SetupKeepAlive (bool enable) noexcept
 Setup option: keep alive. More...
 
void SetupNoDelay (bool enable) noexcept
 Setup option: no delay. 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 client connected notification. More...
 
virtual void onDisconnected ()
 Handle client 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...
 

Detailed Description

TCP client.

TCP client is used to read/write data from/into the connected TCP server.

Thread-safe.

Examples
tcp_chat_client.cpp.

Definition at line 29 of file tcp_client.h.

Constructor & Destructor Documentation

◆ TCPClient() [1/5]

CppServer::Asio::TCPClient::TCPClient ( const std::shared_ptr< Service > &  service,
const std::string &  address,
int  port 
)

Initialize TCP client with a given Asio service, server address and port number.

Parameters
service- Asio service
address- Server address
port- Server port number
Examples
tcp_chat_client.cpp.

Definition at line 14 of file tcp_client.cpp.

◆ TCPClient() [2/5]

CppServer::Asio::TCPClient::TCPClient ( const std::shared_ptr< Service > &  service,
const std::string &  address,
const std::string &  scheme 
)

Initialize TCP client with a given Asio service, server address and scheme name.

Parameters
service- Asio service
address- Server address
scheme- Scheme name

Definition at line 41 of file tcp_client.cpp.

◆ TCPClient() [3/5]

CppServer::Asio::TCPClient::TCPClient ( const std::shared_ptr< Service > &  service,
const asio::ip::tcp::endpoint &  endpoint 
)

Initialize TCP client with a given Asio service and endpoint.

Parameters
service- Asio service
endpoint- Server TCP endpoint

Definition at line 69 of file tcp_client.cpp.

◆ TCPClient() [4/5]

CppServer::Asio::TCPClient::TCPClient ( const TCPClient )
delete

◆ TCPClient() [5/5]

CppServer::Asio::TCPClient::TCPClient ( TCPClient &&  )
delete

◆ ~TCPClient()

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

Member Function Documentation

◆ address()

const std::string& CppServer::Asio::TCPClient::address ( ) const
inlinenoexcept

Get the server address.

Definition at line 74 of file tcp_client.h.

◆ bytes_pending()

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

Get the number of bytes pending sent by the client.

Definition at line 81 of file tcp_client.h.

◆ bytes_received()

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

Get the number of bytes received by the client.

Definition at line 85 of file tcp_client.h.

◆ bytes_sent()

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

Get the number of bytes sent by the client.

Definition at line 83 of file tcp_client.h.

◆ Connect() [1/2]

bool CppServer::Asio::TCPClient::Connect ( )
virtual

Connect the client (synchronous)

Please note that synchronous connect will not receive data automatically! You should use Receive() or ReceiveAsync() method manually after successful connection.

Returns
'true' if the client was successfully connected, 'false' if the client failed to connect

Reimplemented in CppServer::WS::WSClient.

Definition at line 123 of file tcp_client.cpp.

◆ Connect() [2/2]

bool CppServer::Asio::TCPClient::Connect ( const std::shared_ptr< TCPResolver > &  resolver)
virtual

Connect the client using the given DNS resolver (synchronous)

Please note that synchronous connect will not receive data automatically! You should use Receive() or ReceiveAsync() method manually after successful connection.

Parameters
resolver- DNS resolver
Returns
'true' if the client was successfully connected, 'false' if the client failed to connect

Definition at line 178 of file tcp_client.cpp.

◆ ConnectAsync() [1/2]

bool CppServer::Asio::TCPClient::ConnectAsync ( )
virtual

Connect the client (asynchronous)

Returns
'true' if the client was successfully connected, 'false' if the client failed to connect

Reimplemented in CppServer::WS::WSClient.

Examples
tcp_chat_client.cpp.

Definition at line 279 of file tcp_client.cpp.

◆ ConnectAsync() [2/2]

bool CppServer::Asio::TCPClient::ConnectAsync ( const std::shared_ptr< TCPResolver > &  resolver)
virtual

Connect the client using the given DNS resolver (asynchronous)

Parameters
resolver- DNS resolver
Returns
'true' if the client was successfully connected, 'false' if the client failed to connect

Definition at line 358 of file tcp_client.cpp.

◆ Disconnect()

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

Disconnect the client (synchronous)

Returns
'true' if the client was successfully disconnected, 'false' if the client is already disconnected

Definition at line 124 of file tcp_client.h.

◆ DisconnectAsync()

virtual bool CppServer::Asio::TCPClient::DisconnectAsync ( )
inlinevirtual

Disconnect the client (asynchronous)

Returns
'true' if the client was successfully disconnected, 'false' if the client is already disconnected
Examples
tcp_chat_client.cpp.

Definition at line 146 of file tcp_client.h.

◆ endpoint()

asio::ip::tcp::endpoint& CppServer::Asio::TCPClient::endpoint ( )
inlinenoexcept

Get the client endpoint.

Definition at line 69 of file tcp_client.h.

◆ id()

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

Get the client Id.

Examples
tcp_chat_client.cpp, and ws_chat_client.cpp.

Definition at line 60 of file tcp_client.h.

◆ io_service()

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

Get the Asio IO service.

Definition at line 65 of file tcp_client.h.

◆ IsConnected()

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

Is the client connected?

Examples
tcp_chat_client.cpp, and ws_chat_client.cpp.

Definition at line 101 of file tcp_client.h.

◆ onConnected()

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

Handle client connected notification.

Reimplemented in CppServer::WS::WSClient, and CppServer::HTTP::HTTPClientEx.

Examples
tcp_chat_client.cpp.

Definition at line 279 of file tcp_client.h.

◆ onDisconnected()

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

Handle client disconnected notification.

Reimplemented in CppServer::WS::WSClient, CppServer::HTTP::HTTPClientEx, and CppServer::HTTP::HTTPClient.

Examples
tcp_chat_client.cpp.

Definition at line 281 of file tcp_client.h.

◆ onEmpty()

virtual void CppServer::Asio::TCPClient::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 server.

Definition at line 312 of file tcp_client.h.

◆ onError()

virtual void CppServer::Asio::TCPClient::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
tcp_chat_client.cpp, and ws_chat_client.cpp.

Definition at line 320 of file tcp_client.h.

◆ onReceived()

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

Handle buffer received notification.

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

Parameters
buffer- Received buffer
size- Received buffer size

Reimplemented in CppServer::WS::WSClient, and CppServer::HTTP::HTTPClient.

Examples
tcp_chat_client.cpp.

Definition at line 291 of file tcp_client.h.

◆ onSent()

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

Handle buffer sent notification.

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

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

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

Definition at line 303 of file tcp_client.h.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ option_keep_alive()

bool CppServer::Asio::TCPClient::option_keep_alive ( ) const
inlinenoexcept

Get the option: keep alive.

Definition at line 88 of file tcp_client.h.

◆ option_no_delay()

bool CppServer::Asio::TCPClient::option_no_delay ( ) const
inlinenoexcept

Get the option: no delay.

Definition at line 90 of file tcp_client.h.

◆ option_receive_buffer_limit()

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

Get the option: receive buffer limit.

Definition at line 92 of file tcp_client.h.

◆ option_receive_buffer_size()

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

Get the option: receive buffer size.

Definition at line 97 of file tcp_client.cpp.

◆ option_send_buffer_limit()

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

Get the option: send buffer limit.

Definition at line 96 of file tcp_client.h.

◆ option_send_buffer_size()

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

Get the option: send buffer size.

Definition at line 104 of file tcp_client.cpp.

◆ port()

int CppServer::Asio::TCPClient::port ( ) const
inlinenoexcept

Get the server port number.

Definition at line 78 of file tcp_client.h.

◆ Receive() [1/4]

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

Receive text from the server (synchronous)

Parameters
size- Text size to receive
Returns
Received text

Definition at line 690 of file tcp_client.cpp.

◆ Receive() [2/4]

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

Receive text from the server with timeout (synchronous)

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

Definition at line 760 of file tcp_client.cpp.

◆ Receive() [3/4]

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

Receive data from the server (synchronous)

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

Definition at line 655 of file tcp_client.cpp.

◆ Receive() [4/4]

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

Receive data from the server with timeout (synchronous)

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

Definition at line 697 of file tcp_client.cpp.

◆ ReceiveAsync()

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

Receive data from the server (asynchronous)

Definition at line 767 of file tcp_client.cpp.

◆ Reconnect()

bool CppServer::Asio::TCPClient::Reconnect ( )
virtual

Reconnect the client (synchronous)

Returns
'true' if the client was successfully reconnected, 'false' if the client is already reconnected

Definition at line 271 of file tcp_client.cpp.

◆ ReconnectAsync()

bool CppServer::Asio::TCPClient::ReconnectAsync ( )
virtual

Reconnect the client (asynchronous)

Returns
'true' if the client was successfully reconnected, 'false' if the client is already reconnected

Definition at line 494 of file tcp_client.cpp.

◆ scheme()

const std::string& CppServer::Asio::TCPClient::scheme ( ) const
inlinenoexcept

Get the scheme name.

Definition at line 76 of file tcp_client.h.

◆ Send() [1/4]

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

Send data to the server (synchronous)

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

Definition at line 505 of file tcp_client.cpp.

◆ Send() [2/4]

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

Send data to the server with timeout (synchronous)

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

Definition at line 540 of file tcp_client.cpp.

◆ Send() [3/4]

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

Send text to the server (synchronous)

Parameters
text- Text to send
Returns
Size of sent text

Definition at line 165 of file tcp_client.h.

◆ Send() [4/4]

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

Send text to the server with timeout (synchronous)

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

Definition at line 181 of file tcp_client.h.

◆ SendAsync() [1/2]

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

Send data to the server (asynchronous)

Parameters
buffer- Buffer to send
size- Buffer size
Returns
'true' if the data was successfully sent, 'false' if the client is not connected

Definition at line 603 of file tcp_client.cpp.

◆ SendAsync() [2/2]

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

Send text to the server (asynchronous)

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

Definition at line 195 of file tcp_client.h.

◆ service()

std::shared_ptr<Service>& CppServer::Asio::TCPClient::service ( )
inlinenoexcept

Get the Asio service.

Definition at line 63 of file tcp_client.h.

◆ SetupKeepAlive()

void CppServer::Asio::TCPClient::SetupKeepAlive ( bool  enable)
inlinenoexcept

Setup option: keep alive.

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

Parameters
enable- Enable/disable option

Definition at line 236 of file tcp_client.h.

◆ SetupNoDelay()

void CppServer::Asio::TCPClient::SetupNoDelay ( bool  enable)
inlinenoexcept

Setup option: no delay.

This option will enable/disable Nagle's algorithm for TCP protocol.

https://en.wikipedia.org/wiki/Nagle%27s_algorithm

Parameters
enable- Enable/disable option

Definition at line 245 of file tcp_client.h.

◆ SetupReceiveBufferLimit()

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

Setup option: receive buffer limit.

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

Parameters
limit- Receive buffer limit

Definition at line 253 of file tcp_client.h.

◆ SetupReceiveBufferSize()

void CppServer::Asio::TCPClient::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 111 of file tcp_client.cpp.

◆ SetupSendBufferLimit()

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

Setup option: send buffer limit.

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

Parameters
limit- Send buffer limit

Definition at line 268 of file tcp_client.h.

◆ SetupSendBufferSize()

void CppServer::Asio::TCPClient::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 117 of file tcp_client.cpp.

◆ socket()

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

Get the client socket.

Definition at line 71 of file tcp_client.h.

◆ strand()

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

Get the Asio service strand for serialized handler execution.

Definition at line 67 of file tcp_client.h.


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