source: trunk/FACT++/src/Connection.h@ 11218

Last change on this file since 11218 was 11190, checked in by tbretz, 13 years ago
Added the possbility to give an IP Address with a port rather than a name which must be resolved; send keep alive packages in 10s intervals
File size: 4.8 KB
Line 
1#ifndef FACT_Connection
2#define FACT_Connection
3
4#include <deque>
5#include <string>
6#include <boost/bind.hpp>
7#include <boost/asio.hpp>
8#include <boost/function.hpp>
9#include <boost/asio/deadline_timer.hpp>
10
11#include "MessageImp.h"
12
13class Connection : public MessageImp, public boost::asio::ip::tcp::socket
14{
15private:
16 MessageImp *fLog;
17
18 std::string fAddress;
19 std::string fPort;
20
21 boost::asio::ip::tcp::endpoint fEndpoint;
22
23 bool fDebugTx;
24
25 enum ConnectionStatus_t
26 {
27 kDisconnected = 0,
28 kConnecting = 1,
29 kConnected = 2,
30 };
31
32protected:
33 boost::asio::deadline_timer fInTimeout;
34
35private:
36 boost::asio::deadline_timer fOutTimeout;
37 boost::asio::deadline_timer fConnectionTimer;
38 std::deque<std::vector<char>> fOutQueue;
39
40 ConnectionStatus_t fConnectionStatus;
41
42 std::string fErrConnect;
43 std::string fMsgConnect;
44
45public:
46 void SetLogStream(MessageImp *log) { fLog = log; }
47 std::ostream &Out() { return fLog ? fLog->Out() : Out(); }
48
49 // -------- Abbreviations for starting async tasks ---------
50
51 void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0);
52 void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
53
54 template<class T>
55 void AsyncWaitImp(boost::asio::deadline_timer &timer, int millisec,
56 void (T::*handler)(const boost::system::error_code&))
57 {
58 // - The boost::asio::basic_deadline_timer::expires_from_now()
59 // function cancels any pending asynchronous waits, and returns
60 // the number of asynchronous waits that were cancelled. If it
61 // returns 0 then you were too late and the wait handler has
62 // already been executed, or will soon be executed. If it
63 // returns 1 then the wait handler was successfully cancelled.
64 // - If a wait handler is cancelled, the bs::error_code passed to
65 // it contains the value bs::error::operation_aborted.
66 timer.expires_from_now(boost::posix_time::milliseconds(millisec));
67
68 timer.async_wait(boost::bind(handler, this, boost::asio::placeholders::error));
69 }
70
71 void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
72 void (Connection::*handler)(const boost::system::error_code&))
73 {
74 AsyncWaitImp(timer, millisec, handler);
75 }
76
77
78private:
79 void AsyncConnect(boost::asio::ip::tcp::resolver::iterator iterator);
80 void AsyncConnect();
81
82 void CloseImp(bool restart=true);
83
84 void ConnectAddrImp(const boost::asio::ip::tcp::endpoint &endpoint,
85 const boost::system::error_code& error);
86 void ConnectIterImp(boost::asio::ip::tcp::resolver::iterator endpoint_iterator,
87 const boost::system::error_code& error);
88
89 void HandleConnectionTimer(const boost::system::error_code &error);
90 void HandleWriteTimeout(const boost::system::error_code &error);
91 void HandleSentData(const boost::system::error_code& error, size_t);
92
93 int Write(const Time &t, const std::string &txt, int qos=kInfo);
94
95 virtual void ConnectionEstablished() { }
96
97public:
98 Connection(boost::asio::io_service& io_service, std::ostream &out);
99
100 // ------------------------ connect --------------------------
101
102 void SetEndpoint(const std::string &addr, int port);
103 void SetEndpoint(const std::string &addr, const std::string &port);
104 void SetEndpoint(const std::string &addr);
105 void SetEndpoint(const boost::asio::ip::tcp::endpoint &ep) { fEndpoint = ep; }
106
107 void StartConnect();
108
109 // ------------------------ close --------------------------
110 void PostClose(bool restart=true);
111
112 // ------------------------ write --------------------------
113 void SendMessageImp(const std::vector<char> msg);
114 void PostMessage(const void *msg, size_t s=0);
115 void PostMessage(const std::string &cmd, size_t s=-1);
116
117 template<typename T, size_t N>
118 void PostMessage(const boost::array<T, N> &msg)
119 {
120 PostMessage(msg.begin(), msg.size()*sizeof(T));
121 }
122
123 template<typename T>
124 void PostMessage(const std::vector<T> &msg)
125 {
126 PostMessage(&msg[0], msg.size()*sizeof(T));
127 }
128
129 // ------------------------ others --------------------------
130
131 virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0) { }
132 virtual void HandleReadTimeout(const boost::system::error_code&) { }
133
134 int IsClosed() const { return !is_open(); }
135
136 bool IsDisconnected() const { return fConnectionStatus==kDisconnected; }
137 bool IsConnected() const { return fConnectionStatus==kConnected; }
138 bool IsConnecting() const { return fConnectionStatus==kConnecting; }
139
140 void SetDebugTx(bool b=true) { fDebugTx=b; }
141
142 std::string URL() const { return fAddress + ":" + fPort; }
143
144 const boost::asio::ip::tcp::endpoint &GetEndpoint() const { return fEndpoint; }
145};
146
147#endif
Note: See TracBrowser for help on using the repository browser.