1 | #ifndef FACT_Connection
|
---|
2 | #define FACT_Connection
|
---|
3 |
|
---|
4 | #include <deque>
|
---|
5 | #include <string>
|
---|
6 | #include <boost/asio.hpp>
|
---|
7 | #include <boost/function.hpp>
|
---|
8 | #include <boost/asio/deadline_timer.hpp>
|
---|
9 |
|
---|
10 | #include "MessageImp.h"
|
---|
11 |
|
---|
12 | class Connection : public MessageImp, public boost::asio::ip::tcp::socket
|
---|
13 | {
|
---|
14 | private:
|
---|
15 | MessageImp *fLog;
|
---|
16 |
|
---|
17 | std::string fAddress;
|
---|
18 | std::string fPort;
|
---|
19 |
|
---|
20 | enum ConnectionStatus_t
|
---|
21 | {
|
---|
22 | kDisconnected = 0,
|
---|
23 | kConnecting = 1,
|
---|
24 | kConnected = 2,
|
---|
25 | };
|
---|
26 |
|
---|
27 | protected:
|
---|
28 | boost::asio::deadline_timer fInTimeout;
|
---|
29 |
|
---|
30 | private:
|
---|
31 | boost::asio::deadline_timer fOutTimeout;
|
---|
32 | boost::asio::deadline_timer fConnectionTimer;
|
---|
33 | std::deque<std::vector<char>> fOutQueue;
|
---|
34 |
|
---|
35 | ConnectionStatus_t fConnectionStatus;
|
---|
36 |
|
---|
37 | std::string fErrConnect;
|
---|
38 | std::string fMsgConnect;
|
---|
39 |
|
---|
40 | public:
|
---|
41 | void SetLogStream(MessageImp *log) { fLog = log; }
|
---|
42 | std::ostream &Out() { return fLog ? fLog->Out() : Out(); }
|
---|
43 |
|
---|
44 | // -------- Abbreviations for starting async tasks ---------
|
---|
45 |
|
---|
46 | void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0);
|
---|
47 | void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
|
---|
48 | void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
|
---|
49 | void (Connection::*handler)(const boost::system::error_code&));
|
---|
50 |
|
---|
51 | private:
|
---|
52 | void AsyncConnect(boost::asio::ip::tcp::resolver::iterator iterator);
|
---|
53 |
|
---|
54 | void CloseImp(bool restart=true);
|
---|
55 |
|
---|
56 | void ConnectImp(const boost::system::error_code& error,
|
---|
57 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
---|
58 |
|
---|
59 | void HandleConnectionTimer(const boost::system::error_code &error);
|
---|
60 | void HandleWriteTimeout(const boost::system::error_code &error);
|
---|
61 | void HandleSentData(const boost::system::error_code& error, size_t);
|
---|
62 |
|
---|
63 | int Write(const Time &t, const char *txt, int qos=kInfo);
|
---|
64 |
|
---|
65 | virtual void ConnectionEstablished() { }
|
---|
66 |
|
---|
67 | public:
|
---|
68 | Connection(boost::asio::io_service& io_service, std::ostream &out);
|
---|
69 |
|
---|
70 | // ------------------------ connect --------------------------
|
---|
71 |
|
---|
72 | void SetEndpoint(const std::string &addr, int port);
|
---|
73 | void SetEndpoint(const std::string &addr, const std::string &port);
|
---|
74 | void SetEndpoint(const std::string &addr);
|
---|
75 |
|
---|
76 | void StartConnect();
|
---|
77 |
|
---|
78 | // ------------------------ close --------------------------
|
---|
79 | void PostClose(bool restart=true);
|
---|
80 |
|
---|
81 | // ------------------------ write --------------------------
|
---|
82 | void SendMessageImp(const std::vector<char> msg);
|
---|
83 | void PostMessage(const void *msg, size_t s=0);
|
---|
84 | void PostMessage(const std::string &cmd, size_t s=-1);
|
---|
85 |
|
---|
86 | template<typename T, size_t N>
|
---|
87 | void PostMessage(const boost::array<T, N> &msg)
|
---|
88 | {
|
---|
89 | PostMessage(msg.begin(), msg.size()*sizeof(T));
|
---|
90 | }
|
---|
91 |
|
---|
92 | template<typename T>
|
---|
93 | void PostMessage(const std::vector<T> &msg)
|
---|
94 | {
|
---|
95 | PostMessage(&msg[0], msg.size()*sizeof(T));
|
---|
96 | }
|
---|
97 |
|
---|
98 | // ------------------------ others --------------------------
|
---|
99 |
|
---|
100 | virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0) { }
|
---|
101 | virtual void HandleReadTimeout(const boost::system::error_code&) { }
|
---|
102 |
|
---|
103 | int IsClosed() const { return !is_open(); }
|
---|
104 |
|
---|
105 | bool IsConnected() const { return fConnectionStatus==kConnected; }
|
---|
106 | bool IsConnecting() const { return fConnectionStatus==kConnecting; }
|
---|
107 |
|
---|
108 | std::string URL() const { return fAddress + ":" + fPort; }
|
---|
109 | };
|
---|
110 |
|
---|
111 | #endif
|
---|