Serial-TCP/IPbridge  1.0
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Connection Class Referenceabstract

Abstract class for all connection types. More...

#include <connection.h>

Inheritance diagram for Connection:
SerialConnection TCPIPConnection

Classes

class  ConnectionClosed
 This exception is thrown when trying to read or send a message while the connection is closed. More...
 
class  ConnectionError
 This exception is thrown in case of a connection related problem. More...
 

Public Member Functions

 Connection (const Config &config, time_t timeout)
 Constructor. More...
 
virtual ~Connection ()
 Destructor. More...
 
virtual void close ()=0
 Closes an established TCP connection. More...
 
std::string tryReceiveMessage ()
 Tries to receive a message from a client. More...
 
size_t trySendMessage (const std::string &message)
 Tries to send a message to the client. More...
 

Private Member Functions

virtual std::string doReceiveMessage (Config::LengthFieldMessageLength messageLength)=0
 Receives a message. More...
 
virtual size_t doSendMessage (const std::string &message)=0
 Sends the selected message. More...
 
std::string tryReceiveMessageViaBuffer ()
 Tries to receive a message via fixed-length buffer. More...
 
std::string tryReceiveMessageViaLengthField ()
 Tries to receive a message via length field. More...
 
std::string receiveFixedLengthMessage (Config::LengthFieldMessageLength messageLength)
 Receives a message of the selected length. More...
 
size_t trySendMessageViaLengthField (const std::string &message)
 Tries to send a message via length field. More...
 
void sendMessageAtOnce (const std::string &message)
 Sends the selected message at once. More...
 
 Connection (const Connection &)
 
Connectionoperator= (const Connection &)
 

Private Attributes

const Config _config
 Configuration. More...
 
time_t _timeout
 Connection timeout. More...
 

Detailed Description

Abstract class for all connection types.

Each connection class should derive from this class.

It provides reference semantics (objects copying and comparison is NOT allowed).

Constructor & Destructor Documentation

Connection::Connection ( const Config config,
time_t  timeout 
)

Constructor.

Parameters
configConnection configuration.
timeoutConnection timeout (in seconds, if 0 then the timeout is disabled).
virtual Connection::~Connection ( )
virtual

Destructor.

Connection::Connection ( const Connection )
private

Member Function Documentation

virtual void Connection::close ( )
pure virtual

Closes an established TCP connection.

If the connection is not established, nothing will happen.

Implemented in TCPIPConnection, and SerialConnection.

virtual std::string Connection::doReceiveMessage ( Config::LengthFieldMessageLength  messageLength)
privatepure virtual

Receives a message.

Parameters
messageLengthLength of the message.
Returns
Received message.

This operation should be nonblocking.

If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.

Implemented in TCPIPConnection, and SerialConnection.

virtual size_t Connection::doSendMessage ( const std::string &  message)
privatepure virtual

Sends the selected message.

Parameters
messageMessage to be sent.
Returns
Number of sent bytes.

This operation should be nonblocking.

If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.

Implemented in TCPIPConnection, and SerialConnection.

Connection& Connection::operator= ( const Connection )
private
std::string Connection::receiveFixedLengthMessage ( Config::LengthFieldMessageLength  messageLength)
private

Receives a message of the selected length.

Parameters
messageLengthRequested message length.
Returns
Received message.

If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.

void Connection::sendMessageAtOnce ( const std::string &  message)
private

Sends the selected message at once.

Parameters
messageMessage to be sent.

This method checks timeout and if it expires, it throws ConnectionClosed.

If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.

std::string Connection::tryReceiveMessage ( )

Tries to receive a message from a client.

Returns
Received message or an empty string if there was no message to be received.

If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.

std::string Connection::tryReceiveMessageViaBuffer ( )
private

Tries to receive a message via fixed-length buffer.

Returns
Received message or an empty string if there was no message to be received.

If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.

std::string Connection::tryReceiveMessageViaLengthField ( )
private

Tries to receive a message via length field.

Returns
Received message or an empty string if there was no message to be received.

If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.

size_t Connection::trySendMessage ( const std::string &  message)

Tries to send a message to the client.

Parameters
messageMessage to be sent.
Returns
Number of bytes that were actually sent (if < message.size(), then the message did not fit into the sending buffer, so try again later - you have to send only the part that wan't sent yet!).

If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.

size_t Connection::trySendMessageViaLengthField ( const std::string &  message)
private

Tries to send a message via length field.

Parameters
messageMessage to be sent.
Returns
Number of sent bytes.

If the whole message length does not fit into the length field, only the part of the message that can be send will be sent.

This method checks timeout and if it expires, it throws ConnectionClosed.

If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.

Member Data Documentation

const Config Connection::_config
private

Configuration.

time_t Connection::_timeout
private

Connection timeout.


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