trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
trantor::TcpClient Class Reference

This class represents a TCP client. More...

#include <trantor/net/TcpClient.h>

Inheritance diagram for trantor::TcpClient:
Collaboration diagram for trantor::TcpClient:

Public Member Functions

 TcpClient (EventLoop *loop, const InetAddress &serverAddr, const std::string &nameArg)
 Construct a new TCP client instance.
void connect ()
 Connect to the server.
void disconnect ()
 Disconnect from the server.
void stop ()
 Stop connecting to the server.
TcpConnectionPtr connection () const
 Get the TCP connection to the server.
EventLoopgetLoop () const
 Get the event loop.
bool retry () const
 Check whether the client re-connect to the server.
void enableRetry ()
 Enable retrying.
const std::string & name () const
 Get the name of the client.
void setConnectionCallback (const ConnectionCallback &cb)
 Set the connection callback.
void setConnectionCallback (ConnectionCallback &&cb)
void setConnectionErrorCallback (const ConnectionErrorCallback &cb)
 Set the connection error callback.
void setMessageCallback (const RecvMessageCallback &cb)
 Set the message callback.
void setMessageCallback (RecvMessageCallback &&cb)
void setWriteCompleteCallback (const WriteCompleteCallback &cb)
 Set the write complete callback.
void setWriteCompleteCallback (WriteCompleteCallback &&cb)
void setSSLErrorCallback (const SSLErrorCallback &cb)
 Set the callback for errors of SSL.
void setSSLErrorCallback (SSLErrorCallback &&cb)
void setSockOptCallback (const SockOptCallback &cb)
 Set the callback for set socket option.
void setSockOptCallback (SockOptCallback &&cb)
void enableSSL (bool useOldTLS=false, bool validateCert=true, std::string hostname="", const std::vector< std::pair< std::string, std::string > > &sslConfCmds={}, const std::string &certPath="", const std::string &keyPath="", const std::string &caPath="")
 Enable SSL encryption.
void enableSSL (TLSPolicyPtr policy)
 Enable SSL encryption.

Detailed Description

This class represents a TCP client.

Constructor & Destructor Documentation

◆ TcpClient()

trantor::TcpClient::TcpClient ( EventLoop * loop,
const InetAddress & serverAddr,
const std::string & nameArg )

Construct a new TCP client instance.

Parameters
loopThe event loop in which the client runs.
serverAddrThe address of the server.
nameArgThe name of the client.

Member Function Documentation

◆ connection()

TcpConnectionPtr trantor::TcpClient::connection ( ) const
inline

Get the TCP connection to the server.

Returns
TcpConnectionPtr

◆ enableSSL()

void trantor::TcpClient::enableSSL ( bool useOldTLS = false,
bool validateCert = true,
std::string hostname = "",
const std::vector< std::pair< std::string, std::string > > & sslConfCmds = {},
const std::string & certPath = "",
const std::string & keyPath = "",
const std::string & caPath = "" )

Enable SSL encryption.

Parameters
useOldTLSIf true, the TLS 1.0 and 1.1 are supported by the client.
validateCertIf true, we try to validate if the peer's SSL cert is valid.
hostnameThe server hostname for SNI. If it is empty, the SNI is not used.
sslConfCmdsThe commands used to call the SSL_CONF_cmd function in OpenSSL.
certPathThe path of the certificate file.
keyPathThe path of the private key file.
caPathThe path of the certificate authority file.
Note
It's well known that TLS 1.0 and 1.1 are not considered secure in
  1. And it's a good practice to only use TLS 1.2 and above.

◆ getLoop()

EventLoop * trantor::TcpClient::getLoop ( ) const
inline

Get the event loop.

Returns
EventLoop*

◆ name()

const std::string & trantor::TcpClient::name ( ) const
inline

Get the name of the client.

Returns
const std::string&

◆ retry()

bool trantor::TcpClient::retry ( ) const
inline

Check whether the client re-connect to the server.

Returns
true
false

◆ setConnectionCallback()

void trantor::TcpClient::setConnectionCallback ( const ConnectionCallback & cb)
inline

Set the connection callback.

Parameters
cbThe callback is called when the connection to the server is established or closed.

◆ setConnectionErrorCallback()

void trantor::TcpClient::setConnectionErrorCallback ( const ConnectionErrorCallback & cb)
inline

Set the connection error callback.

Parameters
cbThe callback is called when an error occurs during connecting to the server.

◆ setMessageCallback()

void trantor::TcpClient::setMessageCallback ( const RecvMessageCallback & cb)
inline

Set the message callback.

Parameters
cbThe callback is called when some data is received from the server.

◆ setSockOptCallback()

void trantor::TcpClient::setSockOptCallback ( const SockOptCallback & cb)

Set the callback for set socket option.

Parameters
cbThe callback is called, before connect

◆ setSSLErrorCallback()

void trantor::TcpClient::setSSLErrorCallback ( const SSLErrorCallback & cb)
inline

Set the callback for errors of SSL.

Parameters
cbThe callback is called when an SSL error occurs.

◆ setWriteCompleteCallback()

void trantor::TcpClient::setWriteCompleteCallback ( const WriteCompleteCallback & cb)
inline

Set the write complete callback.

Set write complete callback. Not thread safe.

Parameters
cbThe callback is called when data to send is written to the socket.

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