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

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