RESTinio
tls_socket.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  Socket adapter for asio::ssl::stream< asio::ip::tcp::socket >.
7 */
8 
9 #pragma once
10 
11 #include <restinio/asio_include.hpp>
12 
13 #if !defined(RESTINIO_USE_BOOST_ASIO)
14  #include <asio/ssl.hpp>
15 #else
16  #include <boost/asio/ssl.hpp>
17 #endif
18 
19 namespace restinio
20 {
21 
22 namespace impl
23 {
24 
25 //
26 // tls_socket_t
27 //
28 
29 //! Socket adapter for asio::ssl::stream< asio::ip::tcp::socket >.
30 /*!
31  As asio::ssl::stream< asio::ip::tcp::socket > class is not movable
32  and lack some some functionality compared to asio::ip::tcp::socket
33  it is necesasary to have an adapter for it to use it the same way as
34  asio::ip::tcp::socket in template classes and functions.
35 */
37 {
38  public:
41  // Needed for asio >= 1.16.0 (starting with boost-1.72.0)
42 #if RESTINIO_ASIO_VERSION >= 101600
44 #endif
45  tls_socket_t( const tls_socket_t & ) = delete;
46  tls_socket_t & operator = ( const tls_socket_t & ) = delete;
47 
49  asio_ns::io_context & io_context,
50  context_handle_t tls_context )
51  : m_context{ std::move( tls_context ) }
53  {}
54 
55  tls_socket_t( tls_socket_t && ) = default;
56  tls_socket_t & operator = ( tls_socket_t && ) = default;
57 
58  void
59  swap( tls_socket_t & sock )
60  {
61  std::swap( m_context, sock.m_context );
62  std::swap( m_socket, sock.m_socket );
63  }
64 
65  auto &
67  {
68  return m_socket->lowest_layer();
69  }
70 
71  const auto &
72  lowest_layer() const
73  {
74  return m_socket->lowest_layer();
75  }
76 
77  /*!
78  * \brief Get an access to underlying Asio's socket.
79  *
80  * This feature can be useful if there is a need to call some
81  * Asio's socket specific methods like `native_handle`.
82  *
83  * \since
84  * v.0.5.2
85  */
86  socket_t &
88  {
89  return *m_socket;
90  }
91 
92  /*!
93  * \brief Get an access to underlying Asio's socket.
94  *
95  * This feature can be useful if there is a need to call some
96  * Asio's socket specific methods like `native_handle`.
97  *
98  * \since
99  * v.0.5.2
100  */
101  const socket_t &
103  {
104  return *m_socket;
105  }
106 
107  auto
109  {
110  return this->lowest_layer().get_executor();
111  }
112 
113  auto
115  {
116  return this->lowest_layer().remote_endpoint();
117  }
118 
119  auto
120  is_open() const
121  {
122  return this->lowest_layer().is_open();
123  }
124 
125  template< typename... Args >
126  void
127  cancel( Args &&... args )
128  {
129  this->lowest_layer().cancel( std::forward< Args >( args )... );
130  }
131 
132  template< typename... Args >
133  auto
134  async_read_some( Args &&... args )
135  {
136  return m_socket->async_read_some( std::forward< Args >( args )... );
137  }
138 
139  template< typename... Args >
140  auto
141  async_write_some( Args &&... args )
142  {
143  return m_socket->async_write_some( std::forward< Args >( args )... );
144  }
145 
146  template< typename... Args >
147  void
148  shutdown( Args &&... args )
149  {
150  this->lowest_layer().shutdown( std::forward< Args >( args )... );
151  }
152 
153  template< typename... Args >
154  void
155  close( Args &&... args )
156  {
157  this->lowest_layer().close( std::forward< Args >( args )... );
158  }
159 
160  template< typename... Args >
161  auto
162  async_handshake( Args &&... args )
163  {
164  return m_socket->async_handshake( std::forward< Args >( args )... );
165  }
166 
167  private:
170 };
171 
172 } /* namespace impl */
173 
174 } /* namespace restinio */
void cancel(Args &&... args)
Definition: tls_socket.hpp:127
const socket_t & asio_ssl_stream() const
Get an access to underlying Asio&#39;s socket.
Definition: tls_socket.hpp:102
auto async_write_some(Args &&... args)
Definition: tls_socket.hpp:141
void swap(tls_socket_t &sock)
Definition: tls_socket.hpp:59
tls_socket_t(asio_ns::io_context &io_context, context_handle_t tls_context)
Definition: tls_socket.hpp:48
std::unique_ptr< socket_t > m_socket
Definition: tls_socket.hpp:169
tls_socket_t & operator=(const tls_socket_t &)=delete
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, std::optional< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in std::optional<Value_Type> if parameter exis...
Definition: value_or.hpp:64
tls_socket_t(const tls_socket_t &)=delete
socket_t & asio_ssl_stream()
Get an access to underlying Asio&#39;s socket.
Definition: tls_socket.hpp:87
const auto & lowest_layer() const
Definition: tls_socket.hpp:72
auto async_handshake(Args &&... args)
Definition: tls_socket.hpp:162
context_handle_t m_context
Definition: tls_socket.hpp:168
void shutdown(Args &&... args)
Definition: tls_socket.hpp:148
tls_socket_t & operator=(tls_socket_t &&)=default
Socket adapter for asio::ssl::stream< asio::ip::tcp::socket >.
Definition: tls_socket.hpp:36
auto async_read_some(Args &&... args)
Definition: tls_socket.hpp:134
tls_socket_t(tls_socket_t &&)=default
void close(Args &&... args)
Definition: tls_socket.hpp:155