Serial-TCP/IPbridge  1.0
connection.h
Go to the documentation of this file.
1 
12 #ifndef CONNECTION_H
13 #define CONNECTION_H
14 
15 #include <memory>
16 #include <string>
17 
18 #include "base_exception.h"
19 #include "config.h"
20 
28 class Connection {
29  public:
34  public:
35  explicit ConnectionError(const std::string &errorMessage):
36  BaseException(errorMessage) {}
37  };
38 
44  public:
45  explicit ConnectionClosed(const std::string &errorMessage):
46  ConnectionError(errorMessage) {}
47  };
48 
49  public:
57  Connection(const Config &config, time_t timeout);
58 
62  virtual ~Connection();
63 
69  virtual void close() = 0;
70 
80  std::string tryReceiveMessage();
81 
95  size_t trySendMessage(const std::string &message);
96 
97  private:
110  virtual std::string doReceiveMessage(Config::LengthFieldMessageLength messageLength) = 0;
111 
124  virtual size_t doSendMessage(const std::string &message) = 0;
125 
135  std::string tryReceiveMessageViaBuffer();
136 
146  std::string tryReceiveMessageViaLengthField();
147 
159 
176  size_t trySendMessageViaLengthField(const std::string &message);
177 
189  void sendMessageAtOnce(const std::string &message);
190 
191  // Disable copy ctor and assignment operator since this is
192  // a reference object
193  Connection(const Connection &);
194  Connection &operator=(const Connection &);
195 
196  private:
200  time_t _timeout;
201 };
202 
203 #endif // #ifndef CONNECTION_H
204 
205 // End of file connection.h
std::string tryReceiveMessage()
Tries to receive a message from a client.
virtual ~Connection()
Destructor.
time_t _timeout
Connection timeout.
Definition: connection.h:200
virtual void close()=0
Closes an established TCP connection.
Connection & operator=(const Connection &)
virtual std::string doReceiveMessage(Config::LengthFieldMessageLength messageLength)=0
Receives a message.
std::string receiveFixedLengthMessage(Config::LengthFieldMessageLength messageLength)
Receives a message of the selected length.
Config class - declarations.
size_t trySendMessageViaLengthField(const std::string &message)
Tries to send a message via length field.
BaseException class - declarations.
Base class for all configuration classes.
Definition: config.h:28
Base exception class for all project exceptions.
Definition: base_exception.h:23
Connection(const Config &config, time_t timeout)
Constructor.
This exception is thrown when trying to read or send a message while the connection is closed...
Definition: connection.h:43
std::string tryReceiveMessageViaBuffer()
Tries to receive a message via fixed-length buffer.
std::string tryReceiveMessageViaLengthField()
Tries to receive a message via length field.
ConnectionError(const std::string &errorMessage)
Definition: connection.h:35
size_t trySendMessage(const std::string &message)
Tries to send a message to the client.
This exception is thrown in case of a connection related problem.
Definition: connection.h:33
void sendMessageAtOnce(const std::string &message)
Sends the selected message at once.
const Config _config
Configuration.
Definition: connection.h:198
ConnectionClosed(const std::string &errorMessage)
Definition: connection.h:45
std::tr1::uint32_t LengthFieldMessageLength
Message length type when using length field.
Definition: config.h:49
virtual size_t doSendMessage(const std::string &message)=0
Sends the selected message.
Abstract class for all connection types.
Definition: connection.h:28