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

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