omegalib  13.1
omicron::TcpConnection Class Reference

A TCP Connection. More...

#include <Tcp.h>

Public Types

enum  ConnectionState { ConnectionListening, ConnectionOpen, ConnectionClosed }
 

Public Member Functions

 TcpConnection (const ConnectionInfo &ci)
 
virtual ~TcpConnection ()
 
void handle_connect (const asio::error_code &error)
 
tcp::socket & getSocket ()
 Connection properties. More...
 
ConnectionState getState ()
 Gets the connection state. More...
 
const ConnectionInfogetConnectionInfo ()
 Gets the connection info object. More...
 
bool poll ()
 Connection management. More...
 
void close ()
 Forces a connection close. More...
 
void waitClose ()
 Waits for the other end to close the connection. More...
 
void open (const String &host, int port)
 Opens a connection to a server. More...
 
void write (const String &data)
 Data IO Note: all the write methods are blocking: they return only when the data has been fully written to the internal buffers. More...
 
void write (void *data, size_t size)
 Writes a buffer to the connection stream. More...
 
size_t readUntil (void *buffer, size_t size, char delimiter= '\0')
 Synchronously read byte data until the specified delimiter is found or the buffer fills up. More...
 
size_t read (void *buffer, size_t size)
 Synchronously read thre specified number of bytes from the stream. More...
 
size_t availableBytes ()
 Returns the number of bytes available to be read. More...
 
virtual void handleConnected ()
 Connection events. More...
 
virtual void handleClosed ()
 
virtual void handleError (const ConnectionError &err)
 
virtual void handleData ()
 
- Public Member Functions inherited from omicron::ReferenceType
 ReferenceType ()
 
virtual ~ReferenceType ()
 
void ref ()
 
void unref ()
 
long refCount ()
 

Protected Member Functions

void doHandleConnected ()
 

Protected Attributes

String myHost
 
int myPort
 
ConnectionInfo myConnectionInfo
 
ConnectionState myState
 
tcp::socket mySocket
 
asio::streambuf myInputBuffer
 

Friends

class TcpServer
 

Additional Inherited Members

- Static Public Member Functions inherited from omicron::ReferenceType
static void printObjCounts ()
 
- Static Protected Attributes inherited from omicron::ReferenceType
static List< ReferenceType * > mysObjList
 

Detailed Description

A TCP Connection.

Can be used to establish a connection to a TCP server (using the open method). It is also used by the TcpSerer class to represent each client connection. User code can derive this class and reimplement the handleConnected, handleData, handleClose and handleError methods.

+ Inheritance diagram for omicron::TcpConnection:

Member Enumeration Documentation

Enumerator
ConnectionListening 

The connection is waiting for the other side to respond.

ConnectionOpen 

The connection is open. Read / write methods can be used.

ConnectionClosed 

The connection is closed.

Constructor & Destructor Documentation

omicron::TcpConnection::TcpConnection ( const ConnectionInfo ci)
virtual omicron::TcpConnection::~TcpConnection ( )
inlinevirtual

Member Function Documentation

size_t omicron::TcpConnection::availableBytes ( )
inline

Returns the number of bytes available to be read.

void omicron::TcpConnection::close ( )

Forces a connection close.

To gracefully close connections, one side should call waitClose (usually the client), while the other calls close. waitClose will return once the connection has been closed correctly. It is the user's responsibility to signal each end when a connection should be closed, and call close and waitClose appropriately.

void omicron::TcpConnection::doHandleConnected ( )
protected
const ConnectionInfo& omicron::TcpConnection::getConnectionInfo ( )
inline

Gets the connection info object.

tcp::socket& omicron::TcpConnection::getSocket ( )
inline

Connection properties.

Gets the internal ASIO socket object.

ConnectionState omicron::TcpConnection::getState ( )
inline

Gets the connection state.

void omicron::TcpConnection::handle_connect ( const asio::error_code &  error)
virtual void omicron::TcpConnection::handleClosed ( )
virtual
virtual void omicron::TcpConnection::handleConnected ( )
virtual

Connection events.

Reimplemented in omega::MissionControlConnection, and omicron::AssetCacheConnection.

virtual void omicron::TcpConnection::handleData ( )
virtual
virtual void omicron::TcpConnection::handleError ( const ConnectionError err)
virtual
void omicron::TcpConnection::open ( const String host,
int  port 
)

Opens a connection to a server.

bool omicron::TcpConnection::poll ( )

Connection management.

Polls the connection. Calls handleData when new data is available. Calls handleClosed when the connection has been closed from the other end.

size_t omicron::TcpConnection::read ( void *  buffer,
size_t  size 
)

Synchronously read thre specified number of bytes from the stream.

size_t omicron::TcpConnection::readUntil ( void *  buffer,
size_t  size,
char  delimiter = '\0' 
)

Synchronously read byte data until the specified delimiter is found or the buffer fills up.

void omicron::TcpConnection::waitClose ( )

Waits for the other end to close the connection.

void omicron::TcpConnection::write ( const String data)

Data IO Note: all the write methods are blocking: they return only when the data has been fully written to the internal buffers.

Therefore, buffers passed to write methods can be safely modified to after a write call. Writes a string to the connection stream. The string will NOT be NULL terminated.

void omicron::TcpConnection::write ( void *  data,
size_t  size 
)

Writes a buffer to the connection stream.

Friends And Related Function Documentation

friend class TcpServer
friend

Member Data Documentation

ConnectionInfo omicron::TcpConnection::myConnectionInfo
protected
String omicron::TcpConnection::myHost
protected
asio::streambuf omicron::TcpConnection::myInputBuffer
protected
int omicron::TcpConnection::myPort
protected
tcp::socket omicron::TcpConnection::mySocket
protected
ConnectionState omicron::TcpConnection::myState
protected

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