Async  1.5.0
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Friends | List of all members
Async::TcpConnection Class Reference

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

#include <AsyncTcpConnection.h>

Inheritance diagram for Async::TcpConnection:
Async::FramedTcpConnection

Public Types

enum  DisconnectReason {
  DR_HOST_NOT_FOUND, DR_REMOTE_DISCONNECTED, DR_SYSTEM_ERROR, DR_RECV_BUFFER_OVERFLOW,
  DR_ORDERED_DISCONNECT, DR_PROTOCOL_ERROR
}
 Reason code for disconnects. More...
 

Public Member Functions

 TcpConnection (size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
 Constructor. More...
 
 TcpConnection (int sock, const IpAddress &remote_addr, uint16_t remote_port, size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
 Constructor. More...
 
virtual ~TcpConnection (void)
 Destructor. More...
 
void setRecvBufLen (size_t recv_buf_len)
 Set a new receive buffer size. More...
 
virtual void disconnect (void)
 Disconnect from the remote host. More...
 
virtual int write (const void *buf, int count)
 Write data to the TCP connection. More...
 
const IpAddressremoteHost (void) const
 Return the IP-address of the remote host. More...
 
uint16_t remotePort (void) const
 Return the remote port used. More...
 
bool isConnected (void) const
 Check if the connection is established or not. More...
 
bool isIdle (void) const
 Check if the connection is idle. More...
 

Static Public Member Functions

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

Public Attributes

sigc::signal< void, TcpConnection *, DisconnectReasondisconnected
 A signal that is emitted when a connection has been terminated. More...
 
sigc::signal< int, TcpConnection *, void *, int > dataReceived
 A signal that is emitted when data has been received on the connection. More...
 
sigc::signal< void, bool > sendBufferFull
 A signal that is emitted when the send buffer status changes. More...
 

Static Public Attributes

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

Protected Member Functions

void setSocket (int sock)
 Setup information about the connection. More...
 
void setRemoteAddr (const IpAddress &remote_addr)
 Setup information about the connection. More...
 
void setRemotePort (uint16_t remote_port)
 Setup information about the connection. More...
 
int socket (void) const
 Return the socket file descriptor. More...
 
virtual void onDisconnected (DisconnectReason reason)
 Called when a connection has been terminated. More...
 
virtual int onDataReceived (void *buf, int count)
 Called when data has been received on the connection. More...
 

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.

Examples:
AsyncTcpClient_demo.cpp, and AsyncTcpServer_demo.cpp.

Definition at line 123 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_RECV_BUFFER_OVERFLOW 

Receiver buffer overflow.

DR_ORDERED_DISCONNECT 

Disconnect ordered locally.

DR_PROTOCOL_ERROR 

Protocol error.

Definition at line 129 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 length of the receiver buffer to use

◆ 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 length of the receiver buffer to use

◆ ~TcpConnection()

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

Destructor.

Member Function Documentation

◆ disconnect()

virtual void Async::TcpConnection::disconnect ( void  )
virtual

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

◆ disconnectReasonStr()

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

Translate disconnect reason to a string.

◆ 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 218 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 NOTE: This function is overridden in Async::TcpClient.

Definition at line 227 of file AsyncTcpConnection.h.

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

Definition at line 320 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.

Definition at line 301 of file AsyncTcpConnection.h.

References disconnected.

◆ 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, AsyncTcpClient_demo.cpp, and AsyncTcpServer_demo.cpp.

Definition at line 205 of file AsyncTcpConnection.h.

◆ remotePort()

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

Return the remote port used.

Returns
Returns the remote port
Examples:
AsyncFramedTcpServer_demo.cpp, and AsyncTcpServer_demo.cpp.

Definition at line 211 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, an overflow disconnection will be issued on the next reception.

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

◆ 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 292 of file AsyncTcpConnection.h.

◆ 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:
AsyncTcpServer_demo.cpp.

Friends And Related Function Documentation

◆ TcpClientBase

friend class TcpClientBase
friend

Definition at line 326 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 250 of file AsyncTcpConnection.h.

Referenced by onDataReceived().

◆ DEFAULT_RECV_BUF_LEN

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

The default length of the reception buffer.

Definition at line 142 of file AsyncTcpConnection.h.

◆ disconnected

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

A signal that is emitted when a connection has been terminated.

Parameters
conThe connection object
reasonThe reason for the disconnect

Definition at line 234 of file AsyncTcpConnection.h.

Referenced by onDisconnected().

◆ sendBufferFull

sigc::signal<void, bool> Async::TcpConnection::sendBufferFull

A signal that is emitted when the send buffer status changes.

Parameters
is_fullSet to true if the buffer is full or false if a buffer full condition has been cleared

Definition at line 257 of file AsyncTcpConnection.h.


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