CppServer
1.0.4.0
C++ Server Library
|
TCP client. More...
#include <tcp_client.h>
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 |
TCPClient & | operator= (const TCPClient &)=delete |
TCPClient & | operator= (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... | |
TCP client.
TCP client is used to read/write data from/into the connected TCP server.
Thread-safe.
Definition at line 29 of file tcp_client.h.
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.
service | - Asio service |
address | - Server address |
port | - Server port number |
Definition at line 14 of file tcp_client.cpp.
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.
service | - Asio service |
address | - Server address |
scheme | - Scheme name |
Definition at line 41 of file tcp_client.cpp.
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.
service | - Asio service |
endpoint | - Server TCP endpoint |
Definition at line 69 of file tcp_client.cpp.
|
delete |
|
delete |
|
virtualdefault |
|
inlinenoexcept |
Get the server address.
Definition at line 74 of file tcp_client.h.
|
inlinenoexcept |
Get the number of bytes pending sent by the client.
Definition at line 81 of file tcp_client.h.
|
inlinenoexcept |
Get the number of bytes received by the client.
Definition at line 85 of file tcp_client.h.
|
inlinenoexcept |
Get the number of bytes sent by the client.
Definition at line 83 of file tcp_client.h.
|
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.
Reimplemented in CppServer::WS::WSClient.
Definition at line 123 of file tcp_client.cpp.
|
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.
resolver | - DNS resolver |
Definition at line 178 of file tcp_client.cpp.
|
virtual |
Connect the client (asynchronous)
Reimplemented in CppServer::WS::WSClient.
Definition at line 279 of file tcp_client.cpp.
|
virtual |
Connect the client using the given DNS resolver (asynchronous)
resolver | - DNS resolver |
Definition at line 358 of file tcp_client.cpp.
|
inlinevirtual |
Disconnect the client (synchronous)
Definition at line 124 of file tcp_client.h.
|
inlinevirtual |
Disconnect the client (asynchronous)
Definition at line 146 of file tcp_client.h.
|
inlinenoexcept |
Get the client endpoint.
Definition at line 69 of file tcp_client.h.
|
inlinenoexcept |
Get the client Id.
Definition at line 60 of file tcp_client.h.
|
inlinenoexcept |
Get the Asio IO service.
Definition at line 65 of file tcp_client.h.
|
inlinenoexcept |
Is the client connected?
Definition at line 101 of file tcp_client.h.
|
inlineprotectedvirtual |
Handle client connected notification.
Reimplemented in CppServer::WS::WSClient, and CppServer::HTTP::HTTPClientEx.
Definition at line 279 of file tcp_client.h.
|
inlineprotectedvirtual |
Handle client disconnected notification.
Reimplemented in CppServer::WS::WSClient, CppServer::HTTP::HTTPClientEx, and CppServer::HTTP::HTTPClient.
Definition at line 281 of file tcp_client.h.
|
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.
|
inlineprotectedvirtual |
Handle error notification.
error | - Error code |
category | - Error category |
message | - Error message |
Definition at line 320 of file tcp_client.h.
|
inlineprotectedvirtual |
Handle buffer received notification.
Notification is called when another part of buffer was received from the server.
buffer | - Received buffer |
size | - Received buffer size |
Reimplemented in CppServer::WS::WSClient, and CppServer::HTTP::HTTPClient.
Definition at line 291 of file tcp_client.h.
|
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.
sent | - Size of sent buffer |
pending | - Size of pending buffer |
Definition at line 303 of file tcp_client.h.
|
inlinenoexcept |
Get the option: keep alive.
Definition at line 88 of file tcp_client.h.
|
inlinenoexcept |
Get the option: no delay.
Definition at line 90 of file tcp_client.h.
|
inlinenoexcept |
Get the option: receive buffer limit.
Definition at line 92 of file tcp_client.h.
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.
|
inlinenoexcept |
Get the option: send buffer limit.
Definition at line 96 of file tcp_client.h.
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.
|
inlinenoexcept |
Get the server port number.
Definition at line 78 of file tcp_client.h.
|
virtual |
Receive text from the server (synchronous)
size | - Text size to receive |
Definition at line 690 of file tcp_client.cpp.
|
virtual |
Receive text from the server with timeout (synchronous)
size | - Text size to receive |
timeout | - Timeout |
Definition at line 760 of file tcp_client.cpp.
|
virtual |
Receive data from the server (synchronous)
buffer | - Buffer to receive |
size | - Buffer size to receive |
Definition at line 655 of file tcp_client.cpp.
|
virtual |
Receive data from the server with timeout (synchronous)
buffer | - Buffer to receive |
size | - Buffer size to receive |
timeout | - Timeout |
Definition at line 697 of file tcp_client.cpp.
|
virtual |
Receive data from the server (asynchronous)
Definition at line 767 of file tcp_client.cpp.
|
virtual |
Reconnect the client (synchronous)
Definition at line 271 of file tcp_client.cpp.
|
virtual |
Reconnect the client (asynchronous)
Definition at line 494 of file tcp_client.cpp.
|
inlinenoexcept |
Get the scheme name.
Definition at line 76 of file tcp_client.h.
|
virtual |
Send data to the server (synchronous)
buffer | - Buffer to send |
size | - Buffer size |
Definition at line 505 of file tcp_client.cpp.
|
virtual |
Send data to the server with timeout (synchronous)
buffer | - Buffer to send |
size | - Buffer size |
timeout | - Timeout |
Definition at line 540 of file tcp_client.cpp.
|
inlinevirtual |
Send text to the server (synchronous)
text | - Text to send |
Definition at line 165 of file tcp_client.h.
|
inlinevirtual |
Send text to the server with timeout (synchronous)
text | - Text to send |
timeout | - Timeout |
Definition at line 181 of file tcp_client.h.
|
virtual |
Send data to the server (asynchronous)
buffer | - Buffer to send |
size | - Buffer size |
Definition at line 603 of file tcp_client.cpp.
|
inlinevirtual |
Send text to the server (asynchronous)
text | - Text to send |
Definition at line 195 of file tcp_client.h.
|
inlinenoexcept |
Get the Asio service.
Definition at line 63 of file tcp_client.h.
|
inlinenoexcept |
Setup option: keep alive.
This option will setup SO_KEEPALIVE if the OS support this feature.
enable | - Enable/disable option |
Definition at line 236 of file tcp_client.h.
|
inlinenoexcept |
Setup option: no delay.
This option will enable/disable Nagle's algorithm for TCP protocol.
https://en.wikipedia.org/wiki/Nagle%27s_algorithm
enable | - Enable/disable option |
Definition at line 245 of file tcp_client.h.
|
inlinenoexcept |
Setup option: receive buffer limit.
The client will be disconnected if the receive buffer limit is met. Default is unlimited.
limit | - Receive buffer limit |
Definition at line 253 of file tcp_client.h.
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.
size | - Receive buffer size |
Definition at line 111 of file tcp_client.cpp.
|
inlinenoexcept |
Setup option: send buffer limit.
The client will be disconnected if the send buffer limit is met. Default is unlimited.
limit | - Send buffer limit |
Definition at line 268 of file tcp_client.h.
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.
size | - Send buffer size |
Definition at line 117 of file tcp_client.cpp.
|
inlinenoexcept |
Get the client socket.
Definition at line 71 of file tcp_client.h.
|
inlinenoexcept |
Get the Asio service strand for serialized handler execution.
Definition at line 67 of file tcp_client.h.