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 | protected:
|
---|
20 | boost::asio::deadline_timer fInTimeout;
|
---|
21 |
|
---|
22 | private:
|
---|
23 | boost::asio::deadline_timer fOutTimeout;
|
---|
24 | boost::asio::deadline_timer fConnectionTimer;
|
---|
25 | std::deque<std::vector<char>> fOutQueue; // Shell we directly put ba:buffers into the queue?
|
---|
26 |
|
---|
27 | int fConnectionStatus; // 0=offline, 1=connecting, 2=connected
|
---|
28 |
|
---|
29 | std::string fErrConnect;
|
---|
30 | std::string fMsgConnect;
|
---|
31 |
|
---|
32 | protected:
|
---|
33 | char fReadBuffer[1000];
|
---|
34 |
|
---|
35 |
|
---|
36 | public:
|
---|
37 | void SetLogStream(MessageImp *log) { fLog = log; }
|
---|
38 |
|
---|
39 | // -------- Abbreviations for starting async tasks ---------
|
---|
40 |
|
---|
41 | int Write(const Time &t, const char *txt, int qos=kInfo);
|
---|
42 |
|
---|
43 | void AsyncRead(boost::asio::mutable_buffers_1 buffers, int type=0);
|
---|
44 | void AsyncWrite(boost::asio::mutable_buffers_1 buffers);
|
---|
45 | void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
|
---|
46 | void (Connection::*handler)(const boost::system::error_code&));
|
---|
47 | void AsyncConnect(boost::asio::ip::tcp::resolver::iterator iterator);
|
---|
48 |
|
---|
49 | std::string URL() const { return fAddress + ":" + fPort; }
|
---|
50 |
|
---|
51 | void CloseImp(bool restart=true);
|
---|
52 |
|
---|
53 | void ConnectImp(const boost::system::error_code& error,
|
---|
54 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
---|
55 |
|
---|
56 | public:
|
---|
57 |
|
---|
58 | // ------------------------ close --------------------------
|
---|
59 | // close from another thread
|
---|
60 | void PostClose(bool restart=true);
|
---|
61 |
|
---|
62 | // ------------------------ write --------------------------
|
---|
63 | void HandleConnectionTimer(const boost::system::error_code &error);
|
---|
64 | void HandleWriteTimeout(const boost::system::error_code &error);
|
---|
65 | void HandleSentData(const boost::system::error_code& error, size_t);
|
---|
66 | void SendMessageImp(const std::vector<char> &msg);
|
---|
67 | void PostMessage(std::vector<uint16_t> msg);
|
---|
68 | void PostMessage(const std::vector<char> &msg);
|
---|
69 | void PostMessage(const void *msg, size_t s=0);
|
---|
70 | void PostMessage(const std::string &cmd, size_t s=-1);
|
---|
71 |
|
---|
72 | template<std::size_t N>
|
---|
73 | void PostMessage(boost::array<uint16_t, N> arr)
|
---|
74 | {
|
---|
75 | // Convert to network byte order
|
---|
76 | for_each(arr.begin(), arr.end(), htons);
|
---|
77 | PostMessage(std::vector<uint16_t>(arr.begin(), arr.end()));
|
---|
78 | }
|
---|
79 |
|
---|
80 | // ------------------------ connect --------------------------
|
---|
81 |
|
---|
82 | virtual void ConnectionEstablished() { }
|
---|
83 |
|
---|
84 | void StartConnect();
|
---|
85 |
|
---|
86 | Connection(boost::asio::io_service& io_service, std::ostream &out);
|
---|
87 | // Connection(boost::asio::io_service& io_service, const std::string &addr="localhost", int port=5000);
|
---|
88 | // Connection(boost::asio::io_service& io_service, const std::string &addr, const std::string &port);
|
---|
89 |
|
---|
90 | void SetEndpoint(const std::string &addr, int port);
|
---|
91 | void SetEndpoint(const std::string &addr, const std::string &port);
|
---|
92 | void SetEndpoint(const std::string &addr);
|
---|
93 |
|
---|
94 | // ------------------------ others --------------------------
|
---|
95 |
|
---|
96 | virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0) { }
|
---|
97 | virtual void HandleReadTimeout(const boost::system::error_code&) { }
|
---|
98 |
|
---|
99 | int IsClosed() const { return !is_open(); }
|
---|
100 |
|
---|
101 | bool IsConnected() const { return fConnectionStatus==2; }
|
---|
102 | bool IsConnecting() const { return fConnectionStatus==1; }
|
---|
103 | };
|
---|
104 |
|
---|
105 | #endif
|
---|