Serial-TCP/IPbridge
1.0
|
Abstract class for all connection types. More...
#include <connection.h>
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 &) | |
Connection & | operator= (const Connection &) |
Private Attributes | |
const Config | _config |
Configuration. More... | |
time_t | _timeout |
Connection timeout. More... | |
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).
Connection::Connection | ( | const Config & | config, |
time_t | timeout | ||
) |
Constructor.
config | Connection configuration. |
timeout | Connection timeout (in seconds, if 0 then the timeout is disabled). |
|
virtual |
Destructor.
|
private |
|
pure virtual |
Closes an established TCP connection.
If the connection is not established, nothing will happen.
Implemented in TCPIPConnection, and SerialConnection.
|
privatepure virtual |
Receives a message.
messageLength | Length of the 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.
|
privatepure virtual |
Sends the selected message.
message | Message to be sent. |
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.
|
private |
|
private |
Receives a message of the selected length.
messageLength | Requested message length. |
If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.
|
private |
Sends the selected message at once.
message | Message 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.
If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.
|
private |
Tries to receive a message via fixed-length buffer.
If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.
|
private |
Tries to receive a message via length field.
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.
message | Message to be sent. |
If there is some error, ConnectionError will be thrown. If the connection is closed, ConnectionClosed will be thrown.
|
private |
Tries to send a message via length field.
message | Message to be sent. |
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.
|
private |
Configuration.
|
private |
Connection timeout.