source: trunk/FACT++/src/ConnectionSSL.h@ 20115

Last change on this file since 20115 was 20017, checked in by tbretz, 4 years ago
Made ready for boost 1.70 by replacing get_io_context; the SSL connection has to be properly shutdown, but this required a new socket.. therefore switched to boost::optional to keep the socket.
File size: 5.5 KB
Line 
1#ifndef FACT_ConnectionSSL
2#define FACT_ConnectionSSL
3
4#include <list>
5#include <array>
6#include <string>
7
8#include <boost/bind.hpp>
9#include <boost/asio.hpp>
10#include <boost/optional.hpp>
11#include <boost/asio/ssl.hpp>
12#include <boost/function.hpp>
13#include <boost/asio/deadline_timer.hpp>
14
15#include "MessageImp.h"
16
17typedef boost::optional<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>> Stream;
18
19class ConnectionSSL : public MessageImp, public boost::asio::ssl::context/*,
20 public Stream*/
21{
22public:
23 Stream stream;
24
25private:
26 MessageImp *fLog;
27
28 std::string fAddress;
29 std::string fPort;
30
31 boost::asio::ip::tcp::endpoint fEndpoint;
32
33 bool fVerbose;
34 bool fDebugTx;
35
36 enum ConnectionStatus_t
37 {
38 kDisconnected = 0,
39 kConnecting = 1,
40 kConnected = 2,
41 };
42
43protected:
44 boost::asio::deadline_timer fInTimeout;
45
46private:
47 boost::asio::deadline_timer fOutTimeout;
48 boost::asio::deadline_timer fConnectionTimer;
49
50 size_t fQueueSize;
51
52 ConnectionStatus_t fConnectionStatus;
53
54 std::string fErrConnect;
55 std::string fMsgConnect;
56
57public:
58 void SetLogStream(MessageImp *log) { fLog = log; }
59 std::ostream &Out() { return fLog ? fLog->Out() : Out(); }
60
61 // -------- Abbreviations for starting async tasks ---------
62
63 void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0);
64 void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
65
66 template<class T>
67 void AsyncWaitImp(boost::asio::deadline_timer &timer, int millisec,
68 void (T::*handler)(const boost::system::error_code&))
69 {
70 // - The boost::asio::basic_deadline_timer::expires_from_now()
71 // function cancels any pending asynchronous waits, and returns
72 // the number of asynchronous waits that were cancelled. If it
73 // returns 0 then you were too late and the wait handler has
74 // already been executed, or will soon be executed. If it
75 // returns 1 then the wait handler was successfully cancelled.
76 // - If a wait handler is cancelled, the bs::error_code passed to
77 // it contains the value bs::error::operation_aborted.
78 timer.expires_from_now(boost::posix_time::milliseconds(millisec));
79
80 timer.async_wait(boost::bind(handler, this, boost::asio::placeholders::error));
81 }
82
83 void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
84 void (ConnectionSSL::*handler)(const boost::system::error_code&))
85 {
86 AsyncWaitImp(timer, millisec, handler);
87 }
88
89
90private:
91 void AsyncConnect(boost::asio::ip::tcp::resolver::iterator iterator);
92 void AsyncConnect();
93
94 void CloseImp(bool restart=true);
95
96 bool ConnectImp(const boost::asio::ip::tcp::endpoint &endpoint,
97 const boost::system::error_code& error);
98 void ConnectIter(boost::asio::ip::tcp::resolver::iterator endpoint_iterator,
99 const boost::system::error_code& error);
100 void ConnectAddr(const boost::asio::ip::tcp::endpoint &endpoint,
101 const boost::system::error_code& error);
102
103 void HandleConnectionTimer(const boost::system::error_code &error);
104 void HandleWriteTimeout(const boost::system::error_code &error);
105 void HandleSentData(const boost::system::error_code& error, size_t);
106 void HandleHandshake(const boost::asio::ip::tcp::endpoint &endpoint, const boost::system::error_code& error);
107 void HandleAsyncShutdown(const boost::system::error_code &error);
108
109 int Write(const Time &t, const std::string &txt, int qos=kInfo);
110
111 virtual void ConnectionEstablished() { }
112 virtual void ConnectionFailed() { }
113
114public:
115 ConnectionSSL(boost::asio::io_service& io_service, std::ostream &out);
116
117 // ------------------------ connect --------------------------
118
119 void SetEndpoint(const std::string &addr, int port);
120 void SetEndpoint(const std::string &addr, const std::string &port);
121 void SetEndpoint(const std::string &addr);
122 void SetEndpoint(const boost::asio::ip::tcp::endpoint &ep);
123
124 virtual void StartConnect();
125
126 // ------------------------ close --------------------------
127 void PostClose(bool restart=true);
128
129 // ------------------------ write --------------------------
130 void PostMessage(const void *msg, size_t s=0);
131 void PostMessage(const std::string &cmd, size_t s=-1);
132
133 template<typename T, size_t N>
134 void PostMessage(const std::array<T, N> &msg)
135 {
136 PostMessage(msg.begin(), msg.size()*sizeof(T));
137 }
138
139 template<typename T>
140 void PostMessage(const std::vector<T> &msg)
141 {
142 PostMessage(&msg[0], msg.size()*sizeof(T));
143 }
144
145 // ------------------------ others --------------------------
146
147 virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0) { }
148 virtual void HandleReadTimeout(const boost::system::error_code&) { }
149
150 bool IsTxQueueEmpty() const { return fQueueSize==0; /*fOutQueue.empty();*/ }
151
152 int IsClosed() const { return !stream->lowest_layer().is_open(); }
153
154 bool IsDisconnected() const { return fConnectionStatus==kDisconnected; }
155 bool IsConnected() const { return fConnectionStatus==kConnected; }
156 bool IsConnecting() const { return fConnectionStatus==kConnecting; }
157
158 void SetVerbose(bool b=true) { fVerbose=b; }
159 void SetDebugTx(bool b=true) { fDebugTx=b; }
160
161 bool GetVerbose() const { return fVerbose; }
162 bool GetDebugTx() const { return fDebugTx; }
163
164 std::string URL() const { return fAddress + ":" + fPort; }
165
166 const boost::asio::ip::tcp::endpoint &GetEndpoint() const { return fEndpoint; }
167};
168
169#endif
Note: See TracBrowser for help on using the repository browser.