packio
ssl.h
Go to the documentation of this file.
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
4 
5 #ifndef PACKIO_EXTRA_SSL_H
6 #define PACKIO_EXTRA_SSL_H
7 
14 
15 #include "../internal/config.h"
16 #include "../internal/utils.h"
17 
18 #if PACKIO_STANDALONE_ASIO
19 #include <asio/ssl.hpp>
20 #else // PACKIO_STANDALONE_ASIO
21 #include <boost/asio/ssl.hpp>
22 #endif // PACKIO_STANDALONE_ASIO
23 
24 namespace packio {
25 namespace extra {
26 
30 template <typename SslStream>
31 class ssl_stream_adapter : public SslStream {
32 public:
33  using protocol_type = typename SslStream::lowest_layer_type::protocol_type;
34 
35  using SslStream::SslStream;
36 
38  bool is_open() const { return this->lowest_layer().is_open(); }
39 
41  template <typename... Args>
42  auto close(Args&&... args)
43  {
44  return this->lowest_layer().close(std::forward<Args>(args)...);
45  }
46 
49  template <typename... Args>
50  auto cancel(Args&&...)
51  {
52  static_assert(
53  internal::always_false_v<Args...>,
54  "websockets do not support cancel operations");
55  }
56 };
57 
63 template <typename Acceptor, typename SslStreamAdapter>
64 class ssl_acceptor_adapter : public Acceptor {
65 public:
66  template <typename Arg>
67  explicit ssl_acceptor_adapter(Arg&& arg, packio::net::ssl::context& context)
68  : Acceptor(std::forward<Arg>(arg)), context_(context)
69  {
70  }
71 
73  SslStreamAdapter accept();
74 
76  template <typename Handler>
77  void async_accept(Handler&& handler)
78  {
79  Acceptor::async_accept([this, handler = std::forward<Handler>(handler)](
80  auto ec, auto sock) mutable {
81  if (ec) {
82  handler(ec, SslStreamAdapter(std::move(sock), context_));
83  return;
84  }
85  auto stream = std::make_unique<SslStreamAdapter>(
86  std::move(sock), context_);
87  stream->async_handshake(
89  [handler = std::forward<Handler>(handler),
90  stream = std::move(stream)](auto ec) mutable {
91  handler(ec, std::move(*stream));
92  });
93  });
94  }
95 
96 private:
97  packio::net::ssl::context& context_;
98 };
99 
100 } // extra
101 } // packio
102 
103 #endif // PACKIO_EXTRA_SSL_H
A named argument.
Definition: arg.h:17
Adapter class to support websockets servers.
Definition: ssl.h:64
SslStreamAdapter accept()
Don't need definition, but declaration is used to determine the socket type.
void async_accept(Handler &&handler)
Accept the low level connection and perform a SSL handshake.
Definition: ssl.h:77
Adapter class to support SSL streams.
Definition: ssl.h:31
bool is_open() const
Reflect the state of the lower layer.
Definition: ssl.h:38
auto cancel(Args &&...)
Cancel is not possible on websockets, raise a compile-time error is the user tries to use it.
Definition: ssl.h:50
auto close(Args &&... args)
Close the lowest layer.
Definition: ssl.h:42
::packio::server< rpc, Acceptor, Dispatcher > server
The server for JSON-RPC.
Definition: json_rpc.h:42
The packio namespace.
Definition: arg.h:14