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

Last change on this file since 10350 was 10284, checked in by tbretz, 14 years ago
- Added enum ConnectionStatus_t - change fConnectionStatus type to ConnectionStatus_t - removed obsolete fReadBuffer - added const qualifiers to const_buffer_1 in AsyncWrite - added Out() member function - (temporarily?) removed some PostMessage member functions - removed host and port from constructors for simplification - improved how connections are established - improved output of host name during connection attempts
File size: 3.3 KB
Line 
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
12class Connection : public MessageImp, public boost::asio::ip::tcp::socket
13{
14private:
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
27protected:
28 boost::asio::deadline_timer fInTimeout;
29
30private:
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
40public:
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
51private:
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
67public:
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
Note: See TracBrowser for help on using the repository browser.