Fesapi  2.0.0.0
This project provides C++ classes which allow an easy access in import and export to the Energistics standards.
ssl_stream.h
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 namespace boost {
24 namespace beast {
25 
37 template<class NextLayer>
39  : public boost::asio::ssl::stream_base
40 {
41  using stream_type = boost::asio::ssl::stream<NextLayer>;
42 
43  std::unique_ptr<stream_type> p_;
44  boost::asio::ssl::context* ctx_;
45 
46 public:
48  using native_handle_type = typename stream_type::native_handle_type;
49 
51  using impl_struct = typename stream_type::impl_struct;
52 
54  using next_layer_type = typename stream_type::next_layer_type;
55 
57  using lowest_layer_type = typename stream_type::lowest_layer_type;
58 
60  using executor_type = typename stream_type::executor_type;
61 
62  template<class Arg>
63  ssl_stream(
64  Arg&& arg,
65  boost::asio::ssl::context& ctx)
66  : p_(new stream_type{
67  std::forward<Arg>(arg), ctx})
68  , ctx_(&ctx)
69  {
70  }
71 
72  ssl_stream(ssl_stream&& other)
73  : p_(std::move(other.p_))
74  , ctx_(other.ctx_)
75  {
76  }
77 
78  ssl_stream& operator=(ssl_stream&& other)
79  {
80  p_ = std::move(other.p_);
81  ctx_ = other.ctx_;
82  return *this;
83  }
84 
86  get_executor() noexcept
87  {
88  return p_->get_executor();
89  }
90 
92  native_handle()
93  {
94  return p_->native_handle();
95  }
96 
97  next_layer_type const&
98  next_layer() const
99  {
100  return p_->next_layer();
101  }
102 
104  next_layer()
105  {
106  return p_->next_layer();
107  }
108 
110  lowest_layer()
111  {
112  return p_->lowest_layer();
113  }
114 
115  lowest_layer_type const&
116  lowest_layer() const
117  {
118  return p_->lowest_layer();
119  }
120 
121  void
122  set_verify_mode(boost::asio::ssl::verify_mode v)
123  {
124  p_->set_verify_mode(v);
125  }
126 
127  boost::system::error_code
128  set_verify_mode(boost::asio::ssl::verify_mode v,
129  boost::system::error_code& ec)
130  {
131  return p_->set_verify_mode(v, ec);
132  }
133 
134  void
135  set_verify_depth(int depth)
136  {
137  p_->set_verify_depth(depth);
138  }
139 
140  boost::system::error_code
141  set_verify_depth(
142  int depth, boost::system::error_code& ec)
143  {
144  return p_->set_verify_depth(depth, ec);
145  }
146 
147  template<class VerifyCallback>
148  void
149  set_verify_callback(VerifyCallback callback)
150  {
151  p_->set_verify_callback(callback);
152  }
153 
154  template<class VerifyCallback>
155  boost::system::error_code
156  set_verify_callback(VerifyCallback callback,
157  boost::system::error_code& ec)
158  {
159  return p_->set_verify_callback(callback, ec);
160  }
161 
162  void
163  handshake(handshake_type type)
164  {
165  p_->handshake(type);
166  }
167 
168  boost::system::error_code
169  handshake(handshake_type type,
170  boost::system::error_code& ec)
171  {
172  return p_->handshake(type, ec);
173  }
174 
175  template<class ConstBufferSequence>
176  void
177  handshake(
178  handshake_type type, ConstBufferSequence const& buffers)
179  {
180  p_->handshake(type, buffers);
181  }
182 
183  template<class ConstBufferSequence>
184  boost::system::error_code
185  handshake(handshake_type type,
186  ConstBufferSequence const& buffers,
187  boost::system::error_code& ec)
188  {
189  return p_->handshake(type, buffers, ec);
190  }
191 
192  template<class HandshakeHandler>
193  BOOST_ASIO_INITFN_RESULT_TYPE(HandshakeHandler,
194  void(boost::system::error_code))
195  async_handshake(handshake_type type,
196  BOOST_ASIO_MOVE_ARG(HandshakeHandler) handler)
197  {
198  return p_->async_handshake(type,
199  BOOST_ASIO_MOVE_CAST(HandshakeHandler)(handler));
200  }
201 
202  template<class ConstBufferSequence, class BufferedHandshakeHandler>
203  BOOST_ASIO_INITFN_RESULT_TYPE(BufferedHandshakeHandler,
204  void (boost::system::error_code, std::size_t))
205  async_handshake(handshake_type type, ConstBufferSequence const& buffers,
206  BOOST_ASIO_MOVE_ARG(BufferedHandshakeHandler) handler)
207  {
208  return p_->async_handshake(type, buffers,
209  BOOST_ASIO_MOVE_CAST(BufferedHandshakeHandler)(handler));
210  }
211 
212  void
213  shutdown()
214  {
215  p_->shutdown();
216  }
217 
218  boost::system::error_code
219  shutdown(boost::system::error_code& ec)
220  {
221  return p_->shutdown(ec);
222  }
223 
224  template<class ShutdownHandler>
225  BOOST_ASIO_INITFN_RESULT_TYPE(ShutdownHandler,
226  void (boost::system::error_code))
227  async_shutdown(BOOST_ASIO_MOVE_ARG(ShutdownHandler) handler)
228  {
229  return p_->async_shutdown(
230  BOOST_ASIO_MOVE_CAST(ShutdownHandler)(handler));
231  }
232 
233  template<class ConstBufferSequence>
234  std::size_t
235  write_some(ConstBufferSequence const& buffers)
236  {
237  return p_->write_some(buffers);
238  }
239 
240  template<class ConstBufferSequence>
241  std::size_t
242  write_some(ConstBufferSequence const& buffers,
243  boost::system::error_code& ec)
244  {
245  return p_->write_some(buffers, ec);
246  }
247 
248  template<class ConstBufferSequence, class WriteHandler>
249  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
250  void (boost::system::error_code, std::size_t))
251  async_write_some(ConstBufferSequence const& buffers,
252  BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
253  {
254  return p_->async_write_some(buffers,
255  BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
256  }
257 
258  template<class MutableBufferSequence>
259  std::size_t
260  read_some(MutableBufferSequence const& buffers)
261  {
262  return p_->read_some(buffers);
263  }
264 
265  template<class MutableBufferSequence>
266  std::size_t
267  read_some(MutableBufferSequence const& buffers,
268  boost::system::error_code& ec)
269  {
270  return p_->read_some(buffers, ec);
271  }
272 
273  template<class MutableBufferSequence, class ReadHandler>
274  BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
275  void(boost::system::error_code, std::size_t))
276  async_read_some(MutableBufferSequence const& buffers,
277  BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
278  {
279  return p_->async_read_some(buffers,
280  BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
281  }
282 
283  template<class SyncStream>
284  friend
285  void
286  teardown(boost::beast::websocket::role_type,
287  ssl_stream<SyncStream>& stream,
288  boost::system::error_code& ec);
289 
290  template<class AsyncStream, class TeardownHandler>
291  friend
292  void
293  async_teardown(boost::beast::websocket::role_type,
294  ssl_stream<AsyncStream>& stream, TeardownHandler&& handler);
295 };
296 
297 // These hooks are used to inform boost::beast::websocket::stream on
298 // how to tear down the connection as part of the WebSocket
299 // protocol specifications
300 
301 template<class SyncStream>
302 inline
303 void
304 teardown(
305  boost::beast::websocket::role_type role,
306  ssl_stream<SyncStream>& stream,
307  boost::system::error_code& ec)
308 {
309  // Just forward it to the wrapped ssl::stream
310  using boost::beast::websocket::teardown;
311  teardown(role, *stream.p_, ec);
312 }
313 
314 template<class AsyncStream, class TeardownHandler>
315 inline
316 void
317 async_teardown(
318  boost::beast::websocket::role_type role,
319  ssl_stream<AsyncStream>& stream,
320  TeardownHandler&& handler)
321 {
322  // Just forward it to the wrapped ssl::stream
323  using boost::beast::websocket::async_teardown;
324  async_teardown(role,
325  *stream.p_, std::forward<TeardownHandler>(handler));
326 }
327 
328 }
329 }
330 
331 #endif
Definition: ssl_stream.h:40
typename stream_type::impl_struct impl_struct
Structure for use with deprecated impl_type.
Definition: ssl_stream.h:51
typename stream_type::lowest_layer_type lowest_layer_type
The type of the lowest layer.
Definition: ssl_stream.h:57
typename stream_type::native_handle_type native_handle_type
The native handle type of the SSL stream.
Definition: ssl_stream.h:48
typename stream_type::next_layer_type next_layer_type
The type of the next layer.
Definition: ssl_stream.h:54
typename stream_type::executor_type executor_type
The type of the executor associated with the object.
Definition: ssl_stream.h:60