| 1 | #ifndef FACT_Connection
|
|---|
| 2 | #define FACT_Connection
|
|---|
| 3 |
|
|---|
| 4 | #include <deque>
|
|---|
| 5 | #include <string>
|
|---|
| 6 | #include <boost/asio.hpp>
|
|---|
| 7 | #include <boost/asio/deadline_timer.hpp>
|
|---|
| 8 |
|
|---|
| 9 | #include "MessageImp.h"
|
|---|
| 10 |
|
|---|
| 11 | class Connection : public MessageImp, public boost::asio::ip::tcp::socket
|
|---|
| 12 | {
|
|---|
| 13 | private:
|
|---|
| 14 | MessageImp *fLog;
|
|---|
| 15 |
|
|---|
| 16 | std::string fAddress;
|
|---|
| 17 | std::string fPort;
|
|---|
| 18 | protected:
|
|---|
| 19 | boost::asio::deadline_timer fInTimeout;
|
|---|
| 20 |
|
|---|
| 21 | private:
|
|---|
| 22 | boost::asio::deadline_timer fOutTimeout;
|
|---|
| 23 | boost::asio::deadline_timer fConnectionTimer;
|
|---|
| 24 | std::deque<std::vector<char>> fOutQueue; // Shell we directly put ba:buffers into the queue?
|
|---|
| 25 |
|
|---|
| 26 | int fConnectionStatus; // 0=offline, 1=connecting, 2=connected
|
|---|
| 27 |
|
|---|
| 28 | std::string fErrConnect;
|
|---|
| 29 | std::string fMsgConnect;
|
|---|
| 30 |
|
|---|
| 31 | protected:
|
|---|
| 32 | char fReadBuffer[1000];
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | public:
|
|---|
| 36 | void SetLogStream(MessageImp *log) { fLog = log; }
|
|---|
| 37 |
|
|---|
| 38 | // -------- Abbreviations for starting async tasks ---------
|
|---|
| 39 |
|
|---|
| 40 | int Write(const Time &t, const char *txt, int qos=kInfo);
|
|---|
| 41 |
|
|---|
| 42 | void AsyncRead(boost::asio::mutable_buffers_1 buffers/*,
|
|---|
| 43 | void (Connection::*handler)(const boost::system::error_code&, size_t)*/);
|
|---|
| 44 | void AsyncWrite(boost::asio::mutable_buffers_1 buffers/*,
|
|---|
| 45 | void (Connection::*handler)(const boost::system::error_code&)*/);
|
|---|
| 46 | void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
|
|---|
| 47 | void (Connection::*handler)(const boost::system::error_code&));
|
|---|
| 48 | void AsyncConnect(boost::asio::ip::tcp::resolver::iterator iterator);
|
|---|
| 49 |
|
|---|
| 50 | std::string URL() const { return fAddress + ":" + fPort; }
|
|---|
| 51 |
|
|---|
| 52 | public:
|
|---|
| 53 |
|
|---|
| 54 | // ------------------------ close --------------------------
|
|---|
| 55 | // close from another thread
|
|---|
| 56 | void CloseImp(bool restart=true);
|
|---|
| 57 | void PostClose(bool restart=true);
|
|---|
| 58 |
|
|---|
| 59 | // ------------------------ write --------------------------
|
|---|
| 60 | void HandleConnectionTimer(const boost::system::error_code &error);
|
|---|
| 61 | void HandleWriteTimeout(const boost::system::error_code &error);
|
|---|
| 62 | void HandleSentData(const boost::system::error_code& error, size_t);
|
|---|
| 63 | void SendMessageImp(const std::vector<char> &msg);
|
|---|
| 64 | void PostMessage(const std::vector<char> &msg);
|
|---|
| 65 | void PostMessage(const std::string &cmd, size_t s=-1);
|
|---|
| 66 |
|
|---|
| 67 | // ------------------------ connect --------------------------
|
|---|
| 68 |
|
|---|
| 69 | virtual void ConnectImp(const boost::system::error_code& error,
|
|---|
| 70 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
|
|---|
| 71 |
|
|---|
| 72 | void AsyncConnect();
|
|---|
| 73 |
|
|---|
| 74 | Connection(boost::asio::io_service& io_service, std::ostream &out);
|
|---|
| 75 | Connection(boost::asio::io_service& io_service, const std::string &addr="localhost", int port=5000);
|
|---|
| 76 | Connection(boost::asio::io_service& io_service, const std::string &addr, const std::string &port);
|
|---|
| 77 |
|
|---|
| 78 | void SetEndpoint(const char *addr, int port);
|
|---|
| 79 |
|
|---|
| 80 | // ------------------------ others --------------------------
|
|---|
| 81 |
|
|---|
| 82 | virtual void HandleReceivedData(const boost::system::error_code&, size_t) { }
|
|---|
| 83 | virtual void HandleReadTimeout(const boost::system::error_code&) { }
|
|---|
| 84 |
|
|---|
| 85 | int IsClosed() const { return !is_open(); }
|
|---|
| 86 |
|
|---|
| 87 | bool IsConnected() const { return fConnectionStatus==2; }
|
|---|
| 88 | bool IsConnecting() const { return fConnectionStatus==1; }
|
|---|
| 89 | };
|
|---|
| 90 |
|
|---|
| 91 | #endif
|
|---|