]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | // Official repository: https://github.com/boostorg/beast | |
8 | // | |
9 | ||
10 | #ifndef BOOST_BEAST_EXAMPLE_COMMON_SSL_STREAM_HPP | |
11 | #define BOOST_BEAST_EXAMPLE_COMMON_SSL_STREAM_HPP | |
12 | ||
13 | // This include is necessary to work with `ssl::stream` and `boost::beast::websocket::stream` | |
14 | #include <boost/beast/websocket/ssl.hpp> | |
15 | ||
16 | #include <boost/asio/ip/tcp.hpp> | |
17 | #include <boost/asio/ssl/stream.hpp> | |
18 | #include <cstddef> | |
19 | #include <memory> | |
20 | #include <type_traits> | |
21 | #include <utility> | |
22 | ||
23 | /** C++11 enabled SSL socket wrapper | |
24 | ||
25 | This wrapper provides an interface identical to `boost::asio::ssl::stream`, | |
26 | with the following additional properties: | |
27 | ||
28 | @li Satisfies @b MoveConstructible | |
29 | ||
30 | @li Satisfies @b MoveAssignable | |
31 | ||
32 | @li Constructible from a moved socket. | |
33 | */ | |
34 | template<class NextLayer> | |
35 | class ssl_stream | |
36 | : public boost::asio::ssl::stream_base | |
37 | { | |
b32b8144 FG |
38 | using stream_type = boost::asio::ssl::stream<NextLayer>; |
39 | ||
40 | std::unique_ptr<stream_type> p_; | |
41 | boost::asio::ssl::context* ctx_; | |
42 | ||
43 | public: | |
44 | /// The native handle type of the SSL stream. | |
45 | using native_handle_type = typename stream_type::native_handle_type; | |
46 | ||
47 | /// Structure for use with deprecated impl_type. | |
48 | using impl_struct = typename stream_type::impl_struct; | |
49 | ||
50 | /// The type of the next layer. | |
51 | using next_layer_type = typename stream_type::next_layer_type; | |
52 | ||
53 | /// The type of the lowest layer. | |
54 | using lowest_layer_type = typename stream_type::lowest_layer_type; | |
55 | ||
56 | /// The type of the executor associated with the object. | |
57 | using executor_type = typename stream_type::executor_type; | |
58 | ||
11fdf7f2 | 59 | template<class Arg> |
b32b8144 | 60 | ssl_stream( |
11fdf7f2 | 61 | Arg&& arg, |
b32b8144 FG |
62 | boost::asio::ssl::context& ctx) |
63 | : p_(new stream_type{ | |
11fdf7f2 | 64 | std::forward<Arg>(arg), ctx}) |
b32b8144 FG |
65 | , ctx_(&ctx) |
66 | { | |
b32b8144 FG |
67 | } |
68 | ||
69 | ssl_stream(ssl_stream&& other) | |
11fdf7f2 | 70 | : p_(std::move(other.p_)) |
b32b8144 FG |
71 | , ctx_(other.ctx_) |
72 | { | |
b32b8144 FG |
73 | } |
74 | ||
75 | ssl_stream& operator=(ssl_stream&& other) | |
76 | { | |
11fdf7f2 | 77 | p_ = std::move(other.p_); |
b32b8144 FG |
78 | ctx_ = other.ctx_; |
79 | return *this; | |
80 | } | |
81 | ||
82 | executor_type | |
83 | get_executor() noexcept | |
84 | { | |
85 | return p_->get_executor(); | |
86 | } | |
87 | ||
88 | native_handle_type | |
89 | native_handle() | |
90 | { | |
91 | return p_->native_handle(); | |
92 | } | |
93 | ||
94 | next_layer_type const& | |
95 | next_layer() const | |
96 | { | |
97 | return p_->next_layer(); | |
98 | } | |
99 | ||
100 | next_layer_type& | |
101 | next_layer() | |
102 | { | |
103 | return p_->next_layer(); | |
104 | } | |
105 | ||
106 | lowest_layer_type& | |
107 | lowest_layer() | |
108 | { | |
109 | return p_->lowest_layer(); | |
110 | } | |
111 | ||
112 | lowest_layer_type const& | |
113 | lowest_layer() const | |
114 | { | |
115 | return p_->lowest_layer(); | |
116 | } | |
117 | ||
118 | void | |
119 | set_verify_mode(boost::asio::ssl::verify_mode v) | |
120 | { | |
121 | p_->set_verify_mode(v); | |
122 | } | |
123 | ||
124 | boost::system::error_code | |
125 | set_verify_mode(boost::asio::ssl::verify_mode v, | |
126 | boost::system::error_code& ec) | |
127 | { | |
128 | return p_->set_verify_mode(v, ec); | |
129 | } | |
130 | ||
131 | void | |
132 | set_verify_depth(int depth) | |
133 | { | |
134 | p_->set_verify_depth(depth); | |
135 | } | |
136 | ||
137 | boost::system::error_code | |
138 | set_verify_depth( | |
139 | int depth, boost::system::error_code& ec) | |
140 | { | |
141 | return p_->set_verify_depth(depth, ec); | |
142 | } | |
143 | ||
144 | template<class VerifyCallback> | |
145 | void | |
146 | set_verify_callback(VerifyCallback callback) | |
147 | { | |
148 | p_->set_verify_callback(callback); | |
149 | } | |
150 | ||
151 | template<class VerifyCallback> | |
152 | boost::system::error_code | |
153 | set_verify_callback(VerifyCallback callback, | |
154 | boost::system::error_code& ec) | |
155 | { | |
156 | return p_->set_verify_callback(callback, ec); | |
157 | } | |
158 | ||
159 | void | |
160 | handshake(handshake_type type) | |
161 | { | |
162 | p_->handshake(type); | |
163 | } | |
164 | ||
165 | boost::system::error_code | |
166 | handshake(handshake_type type, | |
167 | boost::system::error_code& ec) | |
168 | { | |
169 | return p_->handshake(type, ec); | |
170 | } | |
171 | ||
172 | template<class ConstBufferSequence> | |
173 | void | |
174 | handshake( | |
175 | handshake_type type, ConstBufferSequence const& buffers) | |
176 | { | |
177 | p_->handshake(type, buffers); | |
178 | } | |
179 | ||
180 | template<class ConstBufferSequence> | |
181 | boost::system::error_code | |
182 | handshake(handshake_type type, | |
183 | ConstBufferSequence const& buffers, | |
184 | boost::system::error_code& ec) | |
185 | { | |
186 | return p_->handshake(type, buffers, ec); | |
187 | } | |
188 | ||
189 | template<class HandshakeHandler> | |
190 | BOOST_ASIO_INITFN_RESULT_TYPE(HandshakeHandler, | |
191 | void(boost::system::error_code)) | |
192 | async_handshake(handshake_type type, | |
193 | BOOST_ASIO_MOVE_ARG(HandshakeHandler) handler) | |
194 | { | |
195 | return p_->async_handshake(type, | |
196 | BOOST_ASIO_MOVE_CAST(HandshakeHandler)(handler)); | |
197 | } | |
198 | ||
199 | template<class ConstBufferSequence, class BufferedHandshakeHandler> | |
200 | BOOST_ASIO_INITFN_RESULT_TYPE(BufferedHandshakeHandler, | |
201 | void (boost::system::error_code, std::size_t)) | |
202 | async_handshake(handshake_type type, ConstBufferSequence const& buffers, | |
203 | BOOST_ASIO_MOVE_ARG(BufferedHandshakeHandler) handler) | |
204 | { | |
205 | return p_->async_handshake(type, buffers, | |
206 | BOOST_ASIO_MOVE_CAST(BufferedHandshakeHandler)(handler)); | |
207 | } | |
208 | ||
209 | void | |
210 | shutdown() | |
211 | { | |
212 | p_->shutdown(); | |
213 | } | |
214 | ||
215 | boost::system::error_code | |
216 | shutdown(boost::system::error_code& ec) | |
217 | { | |
218 | return p_->shutdown(ec); | |
219 | } | |
220 | ||
221 | template<class ShutdownHandler> | |
222 | BOOST_ASIO_INITFN_RESULT_TYPE(ShutdownHandler, | |
223 | void (boost::system::error_code)) | |
224 | async_shutdown(BOOST_ASIO_MOVE_ARG(ShutdownHandler) handler) | |
225 | { | |
226 | return p_->async_shutdown( | |
227 | BOOST_ASIO_MOVE_CAST(ShutdownHandler)(handler)); | |
228 | } | |
229 | ||
230 | template<class ConstBufferSequence> | |
231 | std::size_t | |
232 | write_some(ConstBufferSequence const& buffers) | |
233 | { | |
234 | return p_->write_some(buffers); | |
235 | } | |
236 | ||
237 | template<class ConstBufferSequence> | |
238 | std::size_t | |
239 | write_some(ConstBufferSequence const& buffers, | |
240 | boost::system::error_code& ec) | |
241 | { | |
242 | return p_->write_some(buffers, ec); | |
243 | } | |
244 | ||
245 | template<class ConstBufferSequence, class WriteHandler> | |
246 | BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, | |
247 | void (boost::system::error_code, std::size_t)) | |
248 | async_write_some(ConstBufferSequence const& buffers, | |
249 | BOOST_ASIO_MOVE_ARG(WriteHandler) handler) | |
250 | { | |
251 | return p_->async_write_some(buffers, | |
252 | BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); | |
253 | } | |
254 | ||
255 | template<class MutableBufferSequence> | |
256 | std::size_t | |
257 | read_some(MutableBufferSequence const& buffers) | |
258 | { | |
259 | return p_->read_some(buffers); | |
260 | } | |
261 | ||
262 | template<class MutableBufferSequence> | |
263 | std::size_t | |
264 | read_some(MutableBufferSequence const& buffers, | |
265 | boost::system::error_code& ec) | |
266 | { | |
267 | return p_->read_some(buffers, ec); | |
268 | } | |
269 | ||
270 | template<class MutableBufferSequence, class ReadHandler> | |
271 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
272 | void(boost::system::error_code, std::size_t)) | |
273 | async_read_some(MutableBufferSequence const& buffers, | |
274 | BOOST_ASIO_MOVE_ARG(ReadHandler) handler) | |
275 | { | |
276 | return p_->async_read_some(buffers, | |
277 | BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); | |
278 | } | |
279 | ||
280 | template<class SyncStream> | |
281 | friend | |
282 | void | |
283 | teardown(boost::beast::websocket::role_type, | |
284 | ssl_stream<SyncStream>& stream, | |
285 | boost::system::error_code& ec); | |
286 | ||
287 | template<class AsyncStream, class TeardownHandler> | |
288 | friend | |
289 | void | |
290 | async_teardown(boost::beast::websocket::role_type, | |
291 | ssl_stream<AsyncStream>& stream, TeardownHandler&& handler); | |
292 | }; | |
293 | ||
294 | // These hooks are used to inform boost::beast::websocket::stream on | |
295 | // how to tear down the connection as part of the WebSocket | |
296 | // protocol specifications | |
297 | ||
298 | template<class SyncStream> | |
299 | inline | |
300 | void | |
301 | teardown( | |
302 | boost::beast::websocket::role_type role, | |
303 | ssl_stream<SyncStream>& stream, | |
304 | boost::system::error_code& ec) | |
305 | { | |
306 | // Just forward it to the wrapped ssl::stream | |
307 | using boost::beast::websocket::teardown; | |
308 | teardown(role, *stream.p_, ec); | |
309 | } | |
310 | ||
311 | template<class AsyncStream, class TeardownHandler> | |
312 | inline | |
313 | void | |
314 | async_teardown( | |
315 | boost::beast::websocket::role_type role, | |
316 | ssl_stream<AsyncStream>& stream, | |
317 | TeardownHandler&& handler) | |
318 | { | |
319 | // Just forward it to the wrapped ssl::stream | |
320 | using boost::beast::websocket::async_teardown; | |
321 | async_teardown(role, | |
322 | *stream.p_, std::forward<TeardownHandler>(handler)); | |
323 | } | |
324 | ||
325 | #endif |