Async 1.8.0
Async::TcpConnection Class Reference

A class for handling exiting TCP connections. More...

#include <AsyncTcpConnection.h>

Inheritance diagram for Async::TcpConnection:
Async::FramedTcpConnection Async::HttpServerConnection Async::TcpClient< ConT > Async::TcpPrioClient< ConT >

Classes

struct  if_all_true_acc
 A sigc return value accumulator for signals returning bool. More...
 

Public Types

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...
 

Public Member Functions

 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 void disconnect (void)
 Disconnect from the remote host.
 
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.
 

Static Public Member Functions

static const char * disconnectReasonStr (DisconnectReason reason)
 Translate disconnect reason to a string.
 

Public Attributes

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.
 

Static Public Attributes

static const int DEFAULT_RECV_BUF_LEN = 1024
 The default size of the reception buffer.
 

Protected Member Functions

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 closeConnection (void)
 Disconnect from the remote peer.
 
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.
 
virtual int emitVerifyPeer (int preverify_ok, X509_STORE_CTX *store_ctx)
 Emit the verifyPeer signal.
 

Friends

class TcpClientBase
 

Detailed Description

A class for handling exiting TCP connections.

Author
Tobias Blomberg
Date
2003-12-07

This class is used to handle an existing TCP connection. It is not meant to be used directly but could be. It it mainly created to handle connections for Async::TcpClient and Async::TcpServer.

It can also handle SSL/TLS connections. A raw TCP connection can be switched to be encrypted using the setSslContext() and enableSsl() functions.

The reception buffer size given at construction time or using the setRecvBufLen() function is an initial value. If during the connection a larger buffer is needed the size will be automatically increased.

Examples
AsyncTcpClient_demo.cpp, AsyncTcpPrioClient_demo.cpp, and AsyncTcpServer_demo.cpp.

Definition at line 138 of file AsyncTcpConnection.h.

Member Enumeration Documentation

◆ DisconnectReason

Reason code for disconnects.

Enumerator
DR_HOST_NOT_FOUND 

The specified host was not found in the DNS.

DR_REMOTE_DISCONNECTED 

The remote host disconnected.

DR_SYSTEM_ERROR 

A system error occured (check errno)

DR_ORDERED_DISCONNECT 

Disconnect ordered locally.

DR_PROTOCOL_ERROR 

Protocol error.

DR_SWITCH_PEER 

A better peer was found so reconnecting.

DR_BAD_STATE 

The connection ended up in a bad state.

Definition at line 144 of file AsyncTcpConnection.h.

Constructor & Destructor Documentation

◆ TcpConnection() [1/2]

Async::TcpConnection::TcpConnection ( size_t recv_buf_len = DEFAULT_RECV_BUF_LEN)
explicit

Constructor.

Parameters
recv_buf_lenThe initial size of the receive buffer

◆ TcpConnection() [2/2]

Async::TcpConnection::TcpConnection ( int sock,
const IpAddress & remote_addr,
uint16_t remote_port,
size_t recv_buf_len = DEFAULT_RECV_BUF_LEN )

Constructor.

Parameters
sockThe socket for the connection to handle
remote_addrThe remote IP-address of the connection
remote_portThe remote TCP-port of the connection
recv_buf_lenThe initial size of the receive buffer

◆ ~TcpConnection()

virtual Async::TcpConnection::~TcpConnection ( void )
virtual

Destructor.

Member Function Documentation

◆ closeConnection()

virtual void Async::TcpConnection::closeConnection ( void )
protectedvirtual

Disconnect from the remote peer.

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

Reimplemented in Async::FramedTcpConnection, Async::HttpServerConnection, Async::TcpClient< ConT >, and Async::TcpPrioClient< ConT >.

Referenced by disconnect().

◆ disconnect()

virtual void Async::TcpConnection::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 in Async::TcpClient< ConT >, and Async::TcpPrioClient< ConT >.

Definition at line 238 of file AsyncTcpConnection.h.

References closeConnection().

◆ disconnectReasonStr()

static const char * Async::TcpConnection::disconnectReasonStr ( DisconnectReason reason)
static

Translate disconnect reason to a string.

Examples
AsyncHttpServer_demo.cpp, and AsyncTcpPrioClient_demo.cpp.

◆ emitDisconnected()

virtual void Async::TcpConnection::emitDisconnected ( DisconnectReason reason)
inlineprotectedvirtual

Emit the disconnected signal.

Parameters
reasonThe reason for the disconnection

Reimplemented in Async::FramedTcpConnection, Async::HttpServerConnection, and Async::TcpPrioClient< ConT >.

Definition at line 478 of file AsyncTcpConnection.h.

References disconnected.

Referenced by Async::FramedTcpConnection::emitDisconnected(), Async::HttpServerConnection::emitDisconnected(), and onDisconnected().

◆ emitVerifyPeer()

virtual int Async::TcpConnection::emitVerifyPeer ( int preverify_ok,
X509_STORE_CTX * store_ctx )
inlineprotectedvirtual

Emit the verifyPeer signal.

Parameters
preverify_okThe basic certificate verifications passed
store_ctxThe OpenSSL X509 store context
Returns
Returns 1 on success or 0 if verification fail

Definition at line 489 of file AsyncTcpConnection.h.

References verifyPeer.

◆ enableSsl()

void Async::TcpConnection::enableSsl ( bool enable)

Enable or disable TLS for this connection.

Parameters
enableSet to true to enable
Examples
AsyncHttpServer_demo.cpp.

◆ freeze()

void Async::TcpConnection::freeze ( void )

Stop all communication.

When a connection is freezed incoming data will be buffered but the dataReceived signal will not be emitted. Written data is also buffered.

◆ isConnected()

bool Async::TcpConnection::isConnected ( void ) const
inline

Check if the connection is established or not.

Returns
Returns true if the connection is established or false if the connection is not established

Definition at line 279 of file AsyncTcpConnection.h.

◆ isIdle()

bool Async::TcpConnection::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

Definition at line 287 of file AsyncTcpConnection.h.

◆ isServer()

bool Async::TcpConnection::isServer ( void ) const
inline

Definition at line 328 of file AsyncTcpConnection.h.

◆ localHost()

IpAddress Async::TcpConnection::localHost ( void ) const

Get the local IP address associated with this connection.

Returns
Returns an IP address

◆ localPort()

uint16_t Async::TcpConnection::localPort ( void ) const

Get the local TCP port associated with this connection.

Returns
Returns a port number

◆ onDataReceived()

virtual int Async::TcpConnection::onDataReceived ( void * buf,
int count )
inlineprotectedvirtual

Called when data has been received on the connection.

Parameters
bufA buffer containg the read data
countThe number of bytes in the buffer
Returns
Return the number of processed bytes

This function is called when data has been received on this connection. The buffer will contain the bytes read from the operating system. The function will return the number of bytes that has been processed. The bytes not processed will be stored in the receive buffer for this class and presented again to the slot when more data arrives. The new data will be appended to the old data. The default action for this function is to emit the dataReceived signal.

Reimplemented in Async::FramedTcpConnection, and Async::HttpServerConnection.

Definition at line 469 of file AsyncTcpConnection.h.

References dataReceived.

◆ onDisconnected()

virtual void Async::TcpConnection::onDisconnected ( 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. The default action for this function is to emit the disconnected signal.

Reimplemented in Async::FramedTcpConnection, Async::HttpServerConnection, and Async::TcpPrioClient< ConT >.

Definition at line 450 of file AsyncTcpConnection.h.

References emitDisconnected().

◆ operator=()

virtual TcpConnection & Async::TcpConnection::operator= ( TcpConnection && other)
virtual

Move assignmnt operator.

Parameters
otherThe object to move from
Returns
Returns this object

The move operator move the state of a specified TcpConnection object into this object. After the move, the state of the other object will be the same as if it had just been default constructed.

Reimplemented in Async::FramedTcpConnection, and Async::HttpServerConnection.

◆ recvBufLen()

size_t Async::TcpConnection::recvBufLen ( void ) const
inline

Definition at line 229 of file AsyncTcpConnection.h.

◆ remoteHost()

const IpAddress & Async::TcpConnection::remoteHost ( void ) const
inline

Return the IP-address of the remote host.

Returns
Returns the IP-address of the remote host

This function returns the IP-address of the remote host.

Examples
AsyncFramedTcpClient_demo.cpp, AsyncFramedTcpServer_demo.cpp, AsyncHttpServer_demo.cpp, AsyncTcpClient_demo.cpp, AsyncTcpPrioClient_demo.cpp, and AsyncTcpServer_demo.cpp.

Definition at line 266 of file AsyncTcpConnection.h.

Referenced by Async::TcpClientBase::remoteHostName().

◆ remotePort()

uint16_t Async::TcpConnection::remotePort ( void ) const
inline

Return the remote port used.

Returns
Returns the remote port
Examples
AsyncFramedTcpServer_demo.cpp, AsyncHttpServer_demo.cpp, AsyncTcpPrioClient_demo.cpp, and AsyncTcpServer_demo.cpp.

Definition at line 272 of file AsyncTcpConnection.h.

◆ setRecvBufLen()

void Async::TcpConnection::setRecvBufLen ( size_t recv_buf_len)

Set a new receive buffer size.

Parameters
recv_buf_lenThe new receive buffer size in bytes

This function will resize the receive buffer to the specified size. If the buffer size is reduced and there are more bytes in the current buffer than can be fitted into the new buffer, the buffer resize request vill be silently ignored.

◆ setRemoteAddr()

void Async::TcpConnection::setRemoteAddr ( const IpAddress & remote_addr)
protected

Setup information about the connection.

Parameters
remote_addrThe remote IP-address of the connection

Use this function to set up the remote IP-address for the connection.

◆ setRemotePort()

void Async::TcpConnection::setRemotePort ( uint16_t remote_port)
protected

Setup information about the connection.

Parameters
remote_portThe remote TCP-port of the connection

Use this function to set up the remote port for the connection.

◆ setSocket()

void Async::TcpConnection::setSocket ( int sock)
protected

Setup information about the connection.

Parameters
sockThe socket for the connection to handle

Use this function to set up the socket for the connection.

◆ setSslContext()

void Async::TcpConnection::setSslContext ( SslContext & ctx,
bool is_server )

Set the OpenSSL context to use when setting up the connection.

Parameters
ctxThe context object to use
is_serverSet to true if this is a server side connection

This function should be called prior to calling enableSsl in order to set up a TLS context to use when setting up the connection.

◆ socket()

int Async::TcpConnection::socket ( void ) const
inlineprotected

Return the socket file descriptor.

Returns
Returns the currently used socket file descriptor

Use this function to get the socket file descriptor that is currently in use. If it is -1 it has not been set.

Definition at line 433 of file AsyncTcpConnection.h.

◆ sslCertificate()

Async::SslX509 Async::TcpConnection::sslCertificate ( void ) const

◆ sslContext()

SslContext * Async::TcpConnection::sslContext ( void )
inline

Definition at line 326 of file AsyncTcpConnection.h.

◆ sslPeerCertificate()

SslX509 Async::TcpConnection::sslPeerCertificate ( void )

Get the peer certificate associated with this connection.

Returns
Returns the X509 certificate associated with the peer

This function is used to retrieve the peer certificate that the peer has sent to us during the setup phase. If no certificate was sent this function will return a null object so checking for that condition can be done by comparing the returned object with nullptr.

◆ sslVerifyResult()

long Async::TcpConnection::sslVerifyResult ( void ) const

Get the result of the certificate verification process.

Returns
Returns the verification result (e.g. X509_V_OK for ok)

◆ unfreeze()

void Async::TcpConnection::unfreeze ( void )

Reenable all communication.

When a frozen connection is unfreezed the dataReceived signal will be emitted for any received and buffered data. If there are any written data that has been buffered it will be transmitted.

◆ write()

virtual int Async::TcpConnection::write ( const void * buf,
int count )
virtual

Write data to the TCP connection.

Parameters
bufThe buffer containing the data to send
countThe number of bytes to send from the buffer
Returns
Returns the number of bytes written or -1 on failure

Reimplemented in Async::FramedTcpConnection.

Examples
AsyncFramedTcpClient_demo.cpp, AsyncTcpClient_demo.cpp, and AsyncTcpServer_demo.cpp.

Friends And Related Symbol Documentation

◆ TcpClientBase

friend class TcpClientBase
friend

Definition at line 499 of file AsyncTcpConnection.h.

Member Data Documentation

◆ dataReceived

sigc::signal<int, TcpConnection *, void *, int> Async::TcpConnection::dataReceived

A signal that is emitted when data has been received on the connection.

Parameters
bufA buffer containg the read data
countThe number of bytes in the buffer
Returns
Return the number of processed bytes

This signal is emitted when data has been received on this connection. The buffer will contain the bytes read from the operating system. The slot must return the number of bytes that has been processed. The bytes not processed will be stored in the receive buffer for this class and presented again to the slot when more data arrives. The new data will be appended to the old data.

Examples
AsyncTcpServer_demo.cpp.

Definition at line 374 of file AsyncTcpConnection.h.

Referenced by onDataReceived().

◆ DEFAULT_RECV_BUF_LEN

const int Async::TcpConnection::DEFAULT_RECV_BUF_LEN = 1024
static

The default size of the reception buffer.

Definition at line 178 of file AsyncTcpConnection.h.

◆ disconnected

sigc::signal<void, TcpConnection *, DisconnectReason> Async::TcpConnection::disconnected

Get common name for the SSL connection.

Returns
Returns the common name for the associated X509 certificate

A signal that is emitted when a connection has been terminated

Parameters
conThe connection object
reasonThe reason for the disconnect

Definition at line 358 of file AsyncTcpConnection.h.

Referenced by emitDisconnected().

◆ sslConnectionReady

sigc::signal<void, TcpConnection*> Async::TcpConnection::sslConnectionReady

A signal that is emitted when the SSL connection is ready.

Parameters
conThe connection object

This signal is emitted when the SSL initialization and handshake has finished after the application has called the enableSsl() function.

Definition at line 399 of file AsyncTcpConnection.h.

◆ verifyPeer

sigc::signal<if_all_true_acc::result_type, TcpConnection*, int, X509_STORE_CTX*>::accumulated<if_all_true_acc> Async::TcpConnection::verifyPeer

A signal that is emitted on SSL/TLS certificate verification.

Parameters
conThe connection object
preverify_okIs true if the OpenSSL verification is ok
x509_store_ctxThe X509 store context

Connect to this signal to be able to tap in to the certificate verification process. All slots that connect to this signal must return true for the verification process to succeed.

For more information on the function arguments have a look at the manual page for the OpenSSL function SSL_set_verify().

Definition at line 390 of file AsyncTcpConnection.h.

Referenced by emitVerifyPeer().


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