Async 1.7.0
Async::TcpClient< ConT > Class Template Reference

A class for creating a TCP client connection. More...

#include <AsyncTcpClient.h>

Inheritance diagram for Async::TcpClient< ConT >:
Async::TcpConnection Async::TcpClientBase

Public Member Functions

 TcpClient (size_t recv_buf_len=ConT::DEFAULT_RECV_BUF_LEN)
 Constructor.
 
 TcpClient (const std::string &remote_host, uint16_t remote_port, size_t recv_buf_len=ConT::DEFAULT_RECV_BUF_LEN)
 Constructor.
 
 TcpClient (const IpAddress &remote_ip, uint16_t remote_port, size_t recv_buf_len=ConT::DEFAULT_RECV_BUF_LEN)
 Constructor.
 
 ~TcpClient (void)
 Destructor.
 
virtual void disconnect (void) override
 Disconnect from the remote host.
 
bool isIdle (void) const
 Check if the connection is idle.
 
- 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.
 
virtual int write (const void *buf, int count)
 Write data to the TCP 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.
 
- 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) override
 Disconnect from the remote peer.
 
virtual TcpClientBaseoperator= (TcpClientBase &&other)
 Move assignmnt operator.
 
- 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 void onDisconnected (DisconnectReason reason)
 Called when a connection has been terminated.
 
virtual int onDataReceived (void *buf, int count)
 Called when data has been received on the connection.
 
virtual void emitDisconnected (DisconnectReason reason)
 Emit the disconnected signal.
 
- Protected Member Functions inherited from Async::TcpClientBase
virtual void connectionEstablished (void)
 Called when the connection has been established to the server.
 
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_RECV_BUFFER_OVERFLOW ,
  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
 A signal that is emitted when a connection has been terminated.
 
sigc::signal< int, TcpConnection *, void *, int > dataReceived
 A signal that is emitted when data has been received on the connection.
 
sigc::signal< void, bool > sendBufferFull
 A signal that is emitted when the send buffer status changes.
 
- 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 length of the reception buffer.
 

Detailed Description

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

A class for creating a TCP client connection.

Author
Tobias Blomberg
Date
2003-04-12

This class is used to create a TCP client connection. All details of how to create the connection is hidden inside the class. This make it very easy to create and use the connections. An example usage is shown below.

#include <iostream>
#include <AsyncTcpClient.h>
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
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.connect("www.svxlink.org", 80);
}
private:
void onConnected(void)
{
std::cout << "--- Connection established to " << con.remoteHost()
<< std::endl;
std::string req(
"GET / HTTP/1.0\r\n"
"Host: " + con.remoteHostName() + "\r\n"
"\r\n");
std::cout << "--- Sending request:\n" << req << std::endl;
con.write(req.data(), req.size());
}
void onDisconnected(TcpConnection *, TcpClient<>::DisconnectReason reason)
{
std::cout << "--- Disconnected from " << con.remoteHost() << ": "
<< std::endl;
Application::app().quit();
}
int onDataReceived(TcpConnection *, void *buf, int count)
{
std::cout << "--- Data received:" << std::endl;
const char *str = static_cast<char *>(buf);
std::string html(str, str+count);
std::cout << html;
return count;
}
};
int main(int argc, char **argv)
{
MyClass my_class;
app.exec();
}
The core class for writing asyncronous cpp applications.
Contains a class for creating TCP client connections.
static Application & app(void)
Get the one and only application instance.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
std::string remoteHostName(void) const
Get the name of the remote host as given to connect()
A class for creating a TCP client connection.
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.
DisconnectReason
Reason code for disconnects.
virtual int write(const void *buf, int count)
Write data to the TCP connection.
Namespace for the asynchronous programming classes.
Examples
AsyncFramedTcpClient_demo.cpp, and AsyncTcpClient_demo.cpp.

Definition at line 131 of file AsyncTcpClient.h.

Constructor & Destructor Documentation

◆ TcpClient() [1/3]

template<typename ConT = TcpConnection>
Async::TcpClient< ConT >::TcpClient ( 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 144 of file AsyncTcpClient.h.

References Async::TcpConnection::TcpClientBase.

◆ TcpClient() [2/3]

template<typename ConT = TcpConnection>
Async::TcpClient< ConT >::TcpClient ( const std::string & remote_host,
uint16_t remote_port,
size_t recv_buf_len = ConT::DEFAULT_RECV_BUF_LEN )
inline

Constructor.

Parameters
remote_hostThe hostname of the remote host
remote_portThe port on the remote host to connect to
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.

Definition at line 159 of file AsyncTcpClient.h.

References Async::TcpConnection::TcpClientBase.

◆ TcpClient() [3/3]

template<typename ConT = TcpConnection>
Async::TcpClient< ConT >::TcpClient ( const IpAddress & remote_ip,
uint16_t remote_port,
size_t recv_buf_len = ConT::DEFAULT_RECV_BUF_LEN )
inline

Constructor.

Parameters
remote_ipThe IP address of the remote host
remote_portThe port on the remote host to connect to
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.

Definition at line 175 of file AsyncTcpClient.h.

References Async::TcpConnection::TcpClientBase.

◆ ~TcpClient()

template<typename ConT = TcpConnection>
Async::TcpClient< ConT >::~TcpClient ( void )
inline

Destructor.

Definition at line 184 of file AsyncTcpClient.h.

Member Function Documentation

◆ closeConnection()

template<typename ConT = TcpConnection>
virtual void Async::TcpClient< ConT >::closeConnection ( void )
inlineoverrideprotectedvirtual

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 213 of file AsyncTcpClient.h.

References Async::TcpClientBase::closeConnection().

Referenced by disconnect().

◆ disconnect()

template<typename ConT = TcpConnection>
virtual void Async::TcpClient< ConT >::disconnect ( void )
inlineoverridevirtual

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 193 of file AsyncTcpClient.h.

References closeConnection().

◆ isIdle()

template<typename ConT = TcpConnection>
bool Async::TcpClient< ConT >::isIdle ( void ) const
inline

Check if the connection is idle.

Returns
Returns true if the connection is idle

A connection being idle means that it is not connected nor connecting.

Definition at line 201 of file AsyncTcpClient.h.

References Async::TcpClientBase::isIdle().

◆ operator=()

template<typename ConT = TcpConnection>
virtual TcpClientBase & Async::TcpClientBase::operator= ( TcpClientBase && other)
protectedvirtual

Move assignmnt operator.

Parameters
otherThe object to move from
Returns
Returns this object

Reimplemented from Async::TcpClientBase.


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