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 |
|
---|
13 | class Connection : public MessageImp, public boost::asio::ip::tcp::socket
|
---|
14 | {
|
---|
15 | private:
|
---|
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 |
|
---|
32 | protected:
|
---|
33 | boost::asio::deadline_timer fInTimeout;
|
---|
34 |
|
---|
35 | private:
|
---|
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 |
|
---|
45 | public:
|
---|
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 |
|
---|
78 | private:
|
---|
79 | void AsyncConnect(boost::asio::ip::tcp::resolver::iterator iterator);
|
---|
80 | void AsyncConnect();
|
---|
81 |
|
---|
82 | void CloseImp(bool restart=true);
|
---|
83 |
|
---|
84 | bool ConnectImp(const boost::asio::ip::tcp::endpoint &endpoint,
|
---|
85 | const boost::system::error_code& error);
|
---|
86 | void ConnectIter(boost::asio::ip::tcp::resolver::iterator endpoint_iterator,
|
---|
87 | const boost::system::error_code& error);
|
---|
88 | void ConnectAddr(const boost::asio::ip::tcp::endpoint &endpoint,
|
---|
89 | const boost::system::error_code& error);
|
---|
90 |
|
---|
91 | void HandleConnectionTimer(const boost::system::error_code &error);
|
---|
92 | void HandleWriteTimeout(const boost::system::error_code &error);
|
---|
93 | void HandleSentData(const boost::system::error_code& error, size_t);
|
---|
94 |
|
---|
95 | int Write(const Time &t, const std::string &txt, int qos=kInfo);
|
---|
96 |
|
---|
97 | virtual void ConnectionEstablished() { }
|
---|
98 |
|
---|
99 | public:
|
---|
100 | Connection(boost::asio::io_service& io_service, std::ostream &out);
|
---|
101 |
|
---|
102 | // ------------------------ connect --------------------------
|
---|
103 |
|
---|
104 | void SetEndpoint(const std::string &addr, int port);
|
---|
105 | void SetEndpoint(const std::string &addr, const std::string &port);
|
---|
106 | void SetEndpoint(const std::string &addr);
|
---|
107 | void SetEndpoint(const boost::asio::ip::tcp::endpoint &ep) { fEndpoint = ep; }
|
---|
108 |
|
---|
109 | void StartConnect();
|
---|
110 |
|
---|
111 | // ------------------------ close --------------------------
|
---|
112 | void PostClose(bool restart=true);
|
---|
113 |
|
---|
114 | // ------------------------ write --------------------------
|
---|
115 | void SendMessageImp(const std::vector<char> msg);
|
---|
116 | void PostMessage(const void *msg, size_t s=0);
|
---|
117 | void PostMessage(const std::string &cmd, size_t s=-1);
|
---|
118 |
|
---|
119 | template<typename T, size_t N>
|
---|
120 | void PostMessage(const boost::array<T, N> &msg)
|
---|
121 | {
|
---|
122 | PostMessage(msg.begin(), msg.size()*sizeof(T));
|
---|
123 | }
|
---|
124 |
|
---|
125 | template<typename T>
|
---|
126 | void PostMessage(const std::vector<T> &msg)
|
---|
127 | {
|
---|
128 | PostMessage(&msg[0], msg.size()*sizeof(T));
|
---|
129 | }
|
---|
130 |
|
---|
131 | // ------------------------ others --------------------------
|
---|
132 |
|
---|
133 | virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0) { }
|
---|
134 | virtual void HandleReadTimeout(const boost::system::error_code&) { }
|
---|
135 |
|
---|
136 | int IsClosed() const { return !is_open(); }
|
---|
137 |
|
---|
138 | bool IsDisconnected() const { return fConnectionStatus==kDisconnected; }
|
---|
139 | bool IsConnected() const { return fConnectionStatus==kConnected; }
|
---|
140 | bool IsConnecting() const { return fConnectionStatus==kConnecting; }
|
---|
141 |
|
---|
142 | void SetDebugTx(bool b=true) { fDebugTx=b; }
|
---|
143 |
|
---|
144 | std::string URL() const { return fAddress + ":" + fPort; }
|
---|
145 |
|
---|
146 | const boost::asio::ip::tcp::endpoint &GetEndpoint() const { return fEndpoint; }
|
---|
147 | };
|
---|
148 |
|
---|
149 | #endif
|
---|