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

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