Async 1.8.0
Async::TcpPrioClient< ConT > Class Template Reference

A_brief_class_description. More...

#include <AsyncTcpPrioClient.h>

Inheritance diagram for Async::TcpPrioClient< ConT >:
Async::TcpConnection Async::TcpPrioClientBase Async::TcpClientBase

Public Member Functions

 TcpPrioClient (size_t recv_buf_len=ConT::DEFAULT_RECV_BUF_LEN)
 Constructor.
 
 TcpPrioClient (const TcpPrioClient &)=delete
 Disallow copy construction.
 
TcpPrioClientoperator= (const TcpPrioClient &)=delete
 Disallow copy assignment.
 
virtual ~TcpPrioClient (void)
 Destructor.
 
virtual void disconnect (void)
 Disconnect from the remote host.
 
- Public Member Functions inherited from Async::TcpConnection
 TcpConnection (size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
 Constructor.
 
 TcpConnection (int sock, const IpAddress &remote_addr, uint16_t remote_port, size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
 Constructor.
 
virtual ~TcpConnection (void)
 Destructor.
 
virtual TcpConnectionoperator= (TcpConnection &&other)
 Move assignmnt operator.
 
void setRecvBufLen (size_t recv_buf_len)
 Set a new receive buffer size.
 
size_t recvBufLen (void) const
 
virtual int write (const void *buf, int count)
 Write data to the TCP connection.
 
IpAddress localHost (void) const
 Get the local IP address associated with this connection.
 
uint16_t localPort (void) const
 Get the local TCP port associated with this connection.
 
const IpAddressremoteHost (void) const
 Return the IP-address of the remote host.
 
uint16_t remotePort (void) const
 Return the remote port used.
 
bool isConnected (void) const
 Check if the connection is established or not.
 
bool isIdle (void) const
 Check if the connection is idle.
 
void enableSsl (bool enable)
 Enable or disable TLS for this connection.
 
SslX509 sslPeerCertificate (void)
 Get the peer certificate associated with this connection.
 
Async::SslX509 sslCertificate (void) const
 
long sslVerifyResult (void) const
 Get the result of the certificate verification process.
 
void setSslContext (SslContext &ctx, bool is_server)
 Set the OpenSSL context to use when setting up the connection.
 
SslContextsslContext (void)
 
bool isServer (void) const
 
void freeze (void)
 Stop all communication.
 
void unfreeze (void)
 Reenable all communication.
 
- Public Member Functions inherited from Async::TcpPrioClientBase
 TcpPrioClientBase (TcpConnection *con)
 Constructor.
 
 TcpPrioClientBase (TcpConnection *con, const std::string &remote_host, uint16_t remote_port)=delete
 Deleted constructor not making sense in this context.
 
 TcpPrioClientBase (TcpConnection *con, const IpAddress &remote_ip, uint16_t remote_port)=delete
 Deleted constructor not making sense in this context.
 
virtual ~TcpPrioClientBase (void)
 Destructor.
 
void setReconnectMinTime (unsigned t)
 Minimum time between reconnects.
 
void setReconnectMaxTime (unsigned t)
 Maximum time between reconnects.
 
void setReconnectBackoffPercent (unsigned p)
 Percent to increase reconnect time with each try.
 
void setReconnectRandomizePercent (unsigned p)
 Percent to randomize reconnect time.
 
void setService (const std::string &srv_name, const std::string &srv_proto, const std::string &srv_domain)
 Use a DNS service resource record for connections.
 
void addStaticSRVRecord (DnsResourceRecordSRV::Ttl ttl, DnsResourceRecordSRV::Prio prio, DnsResourceRecordSRV::Weight weight, DnsResourceRecordSRV::Port port, DnsResourceRecordSRV::Target target)
 Add a static service resource record.
 
const std::string & service (void) const
 Get the full service name.
 
void connect (void)
 Connect to the remote host.
 
void connect (const std::string &remote_host, uint16_t remote_port)=delete
 Deleted function not making sense in this context.
 
void connect (const Async::IpAddress &remote_ip, uint16_t remote_port)=delete
 Deleted function not making sense in this context.
 
void markAsEstablished (void)
 Mark connection as established.
 
bool markedAsEstablished (void) const
 Check if a connection has been marked as established.
 
bool isIdle (void) const
 Check if the connection is idle.
 
bool isPrimary (void) const
 Check if connected to the primary server.
 
virtual TcpClientBaseoperator= (TcpClientBase &&other)
 Inherit the assignment operator from TcpClientBase.
 
- Public Member Functions inherited from Async::TcpClientBase
 TcpClientBase (TcpConnection *con)
 Constructor.
 
 TcpClientBase (TcpConnection *con, const std::string &remote_host, uint16_t remote_port)
 Constructor.
 
 TcpClientBase (TcpConnection *con, const IpAddress &remote_ip, uint16_t remote_port)
 Constructor.
 
virtual ~TcpClientBase (void)
 Destructor.
 
std::string remoteHostName (void) const
 Get the name of the remote host as given to connect()
 
void setBindIp (const IpAddress &bind_ip)
 Bind to the interface having the specified IP address.
 
const IpAddressbindIp (void) const
 Get the bind IP address.
 
void connect (const std::string &remote_host, uint16_t remote_port)
 Connect to the remote host.
 
void connect (const Async::IpAddress &remote_ip, uint16_t remote_port)
 Connect to the remote host.
 
void connect (void)
 Connect to the remote host.
 
bool isIdle (void) const
 Check if the connection is idle.
 
TcpConnectionconObj (void)
 Return the connection object for this client connection.
 

Protected Member Functions

virtual void closeConnection (void)
 Disconnect from the remote peer.
 
virtual void onDisconnected (TcpConnection::DisconnectReason reason)
 Called when a connection has been terminated.
 
virtual TcpClientBasenewTcpClient (void) override
 Allocate a new TcpClient object.
 
virtual void emitDisconnected (TcpConnection::DisconnectReason reason)
 Emit the disconnected signal.
 
- Protected Member Functions inherited from Async::TcpConnection
void setSocket (int sock)
 Setup information about the connection.
 
void setRemoteAddr (const IpAddress &remote_addr)
 Setup information about the connection.
 
void setRemotePort (uint16_t remote_port)
 Setup information about the connection.
 
int socket (void) const
 Return the socket file descriptor.
 
virtual int onDataReceived (void *buf, int count)
 Called when data has been received on the connection.
 
virtual int emitVerifyPeer (int preverify_ok, X509_STORE_CTX *store_ctx)
 Emit the verifyPeer signal.
 
- Protected Member Functions inherited from Async::TcpPrioClientBase
virtual void connectionEstablished (void) override
 Called when the connection has been established to the server.
 
- Protected Member Functions inherited from Async::TcpClientBase
virtual void emitConnected (void)
 

Additional Inherited Members

- Public Types inherited from Async::TcpConnection
enum  DisconnectReason {
  DR_HOST_NOT_FOUND , DR_REMOTE_DISCONNECTED , DR_SYSTEM_ERROR , DR_ORDERED_DISCONNECT ,
  DR_PROTOCOL_ERROR , DR_SWITCH_PEER , DR_BAD_STATE
}
 Reason code for disconnects. More...
 
- Static Public Member Functions inherited from Async::TcpConnection
static const char * disconnectReasonStr (DisconnectReason reason)
 Translate disconnect reason to a string.
 
- Public Attributes inherited from Async::TcpConnection
sigc::signal< void, TcpConnection *, DisconnectReasondisconnected
 Get common name for the SSL connection.
 
sigc::signal< int, TcpConnection *, void *, int > dataReceived
 A signal that is emitted when data has been received on the connection.
 
sigc::signal< if_all_true_acc::result_type, TcpConnection *, int, X509_STORE_CTX * >::accumulated< if_all_true_accverifyPeer
 A signal that is emitted on SSL/TLS certificate verification.
 
sigc::signal< void, TcpConnection * > sslConnectionReady
 A signal that is emitted when the SSL connection is ready.
 
- Public Attributes inherited from Async::TcpClientBase
sigc::signal< void > connected
 A signal that is emitted when a connection has been established.
 
- Static Public Attributes inherited from Async::TcpConnection
static const int DEFAULT_RECV_BUF_LEN = 1024
 The default size of the reception buffer.
 

Detailed Description

template<typename ConT = TcpConnection>
class Async::TcpPrioClient< ConT >

A_brief_class_description.

Author
Tobias Blomberg / SM0SVX
Date
2021-06-27

A_detailed_class_description

#include <iostream>
using namespace std;
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
con = new TcpPrioClient<>;
con->connected.connect(mem_fun(*this, &MyClass::onConnected));
con->disconnected.connect(mem_fun(*this, &MyClass::onDisconnected));
con->dataReceived.connect(mem_fun(*this, &MyClass::onDataReceived));
con->setService("svxreflector", "tcp", "test.svxlink.org");
con->addStaticSRVRecord(3600, 1, 100, 5304, "localhost.");
con->setBindIp(Async::IpAddress("127.0.0.1"));
con->connect();
}
~MyClass(void)
{
delete con;
}
private:
void onConnected(void)
{
std::cout << "Connection established to " << con->remoteHost()
<< ":" << con->remotePort() << "..." << std::endl;
//con->write("GET /\n", 6);
}
void onDisconnected(TcpConnection *con, TcpClient<>::DisconnectReason reason)
{
std::cout << "Disconnected from " << con->remoteHost()
<< ":" << con->remotePort()
<< ": " << con->disconnectReasonStr(reason)
<< "..." << std::endl;
}
int onDataReceived(TcpConnection *con, void *buf, int count)
{
char *str = static_cast<char *>(buf);
string html(str, str+count);
cout << html;
std::cout << "### Quitting: count=" << count << std::endl;
Application::app().quit();
return count;
}
};
int main(int argc, char **argv)
{
MyClass my_class;
app.exec();
}
The core class for writing asyncronous cpp applications.
A_brief_description_for_this_file.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
A class for representing an IP address in an OS independent way.
sigc::signal< void > connected
A signal that is emitted when a connection has been established.
A class for handling exiting TCP connections.
static const char * disconnectReasonStr(DisconnectReason reason)
Translate disconnect reason to a string.
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
uint16_t remotePort(void) const
Return the remote port used.
DisconnectReason
Reason code for disconnects.
A_brief_class_description.
Namespace for the asynchronous programming classes.
Examples
AsyncTcpPrioClient_demo.cpp.

Definition at line 123 of file AsyncTcpPrioClient.h.

Constructor & Destructor Documentation

◆ TcpPrioClient() [1/2]

template<typename ConT = TcpConnection>
Async::TcpPrioClient< ConT >::TcpPrioClient ( size_t recv_buf_len = ConT::DEFAULT_RECV_BUF_LEN)
inlineexplicit

Constructor.

Parameters
recv_buf_lenThe length of the receiver buffer to use

The object will be constructed and variables will be initialized but no connection will be created until the connect function (see TcpClient::connect) is called. When using this variant of the constructor the connect method which take host and port must be used.

Definition at line 136 of file AsyncTcpPrioClient.h.

◆ TcpPrioClient() [2/2]

template<typename ConT = TcpConnection>
Async::TcpPrioClient< ConT >::TcpPrioClient ( const TcpPrioClient< ConT > & )
delete

Disallow copy construction.

◆ ~TcpPrioClient()

template<typename ConT = TcpConnection>
virtual Async::TcpPrioClient< ConT >::~TcpPrioClient ( void )
inlinevirtual

Destructor.

Definition at line 152 of file AsyncTcpPrioClient.h.

Member Function Documentation

◆ closeConnection()

template<typename ConT = TcpConnection>
virtual void Async::TcpPrioClient< ConT >::closeConnection ( void )
inlineprotectedvirtual

Disconnect from the remote peer.

This function is used internally to close the connection to the remote peer.

Reimplemented from Async::TcpConnection.

Definition at line 177 of file AsyncTcpPrioClient.h.

References Async::TcpClientBase::closeConnection().

◆ disconnect()

template<typename ConT = TcpConnection>
virtual void Async::TcpPrioClient< ConT >::disconnect ( void )
inlinevirtual

Disconnect from the remote host.

Call this function to disconnect from the remote host. If already disconnected, nothing will be done. The disconnected signal is not emitted when this function is called

Reimplemented from Async::TcpConnection.

Definition at line 161 of file AsyncTcpPrioClient.h.

References Async::TcpPrioClientBase::disconnect().

◆ emitDisconnected()

template<typename ConT = TcpConnection>
virtual void Async::TcpPrioClient< ConT >::emitDisconnected ( TcpConnection::DisconnectReason reason)
inlineprotectedvirtual

Emit the disconnected signal.

Parameters
reasonThe reason for the disconnection

Reimplemented from Async::TcpConnection.

Definition at line 211 of file AsyncTcpPrioClient.h.

◆ newTcpClient()

template<typename ConT = TcpConnection>
virtual TcpClientBase * Async::TcpPrioClient< ConT >::newTcpClient ( void )
inlineoverrideprotectedvirtual

Allocate a new TcpClient object.

Returns
Returns a new TcpClient object

This function is used to allocate a new TcpClient object. That object is used when in the background trying to connect to a higher prioritized server. Note that the object should be a "normal" TcpClient and not a TcpPrioClient.

Implements Async::TcpPrioClientBase.

Definition at line 206 of file AsyncTcpPrioClient.h.

◆ onDisconnected()

template<typename ConT = TcpConnection>
virtual void Async::TcpPrioClient< ConT >::onDisconnected ( TcpConnection::DisconnectReason reason)
inlineprotectedvirtual

Called when a connection has been terminated.

Parameters
reasonThe reason for the disconnect

This function will be called when the connection has been terminated.

Reimplemented from Async::TcpConnection.

Definition at line 189 of file AsyncTcpPrioClient.h.

References Async::TcpPrioClientBase::onDisconnected().

◆ operator=()

template<typename ConT = TcpConnection>
TcpPrioClient & Async::TcpPrioClient< ConT >::operator= ( const TcpPrioClient< ConT > & )
delete

Disallow copy assignment.


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