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 #include <restinio/asio_include.hpp>
10 
11 #if !defined(RESTINIO_USE_BOOST_ASIO)
12  #include <asio/ssl.hpp>
13 #else
14  #include <boost/asio/ssl.hpp>
15 #endif
16 
17 namespace restinio
18 {
19 
20 namespace impl
21 {
22 
23 //
24 // tls_socket_t
25 //
26 
27 //! Socket adapter for asio::ssl::stream< asio::ip::tcp::socket >.
28 /*!
29  As asio::ssl::stream< asio::ip::tcp::socket > class is not movable
30  and lack some some functionality compared to asio::ip::tcp::socket
31  it is necesasary to have an adapter for it to use it the same way as
32  asio::ip::tcp::socket in template classes and functions.
33 */
35 {
36  public:
39  tls_socket_t( const tls_socket_t & ) = delete;
40  tls_socket_t & operator = ( const tls_socket_t & ) = delete;
41 
43  asio_ns::io_context & io_context,
44  context_handle_t tls_context )
45  : m_context{ std::move( tls_context ) }
47  {}
48 
49  tls_socket_t( tls_socket_t && ) = default;
50  tls_socket_t & operator = ( tls_socket_t && ) = default;
51 
52  void
53  swap( tls_socket_t & sock )
54  {
55  std::swap( m_context, sock.m_context );
56  std::swap( m_socket, sock.m_socket );
57  }
58 
59  auto &
61  {
62  return m_socket->lowest_layer();
63  }
64 
65  const auto &
66  lowest_layer() const
67  {
68  return m_socket->lowest_layer();
69  }
70 
71  auto
73  {
74  return this->lowest_layer().get_executor();
75  }
76 
77  auto
79  {
80  return this->lowest_layer().remote_endpoint();
81  }
82 
83  auto
84  is_open() const
85  {
86  return this->lowest_layer().is_open();
87  }
88 
89  template< typename... Args >
90  void
91  cancel( Args &&... args )
92  {
93  this->lowest_layer().cancel( std::forward< Args >( args )... );
94  }
95 
96  template< typename... Args >
97  auto
98  async_read_some( Args &&... args )
99  {
100  return m_socket->async_read_some( std::forward< Args >( args )... );
101  }
102 
103  template< typename... Args >
104  auto
105  async_write_some( Args &&... args )
106  {
107  return m_socket->async_write_some( std::forward< Args >( args )... );
108  }
109 
110  template< typename... Args >
111  void
112  shutdown( Args &&... args )
113  {
114  this->lowest_layer().shutdown( std::forward< Args >( args )... );
115  }
116 
117  template< typename... Args >
118  void
119  close( Args &&... args )
120  {
121  this->lowest_layer().close( std::forward< Args >( args )... );
122  }
123 
124  template< typename... Args >
125  auto
126  async_handshake( Args &&... args )
127  {
128  return m_socket->async_handshake( std::forward< Args >( args )... );
129  }
130 
131  auto &
133  {
134  return m_socket->get_io_context();
135  }
136 
137  private:
140 };
141 
142 } /* namespace impl */
143 
144 } /* namespace restinio */
void cancel(Args &&... args)
Definition: tls_socket.hpp:91
auto async_write_some(Args &&... args)
Definition: tls_socket.hpp:105
void swap(tls_socket_t &sock)
Definition: tls_socket.hpp:53
tls_socket_t(asio_ns::io_context &io_context, context_handle_t tls_context)
Definition: tls_socket.hpp:42
std::unique_ptr< socket_t > m_socket
Definition: tls_socket.hpp:139
tls_socket_t & operator=(const tls_socket_t &)=delete
tls_socket_t(const tls_socket_t &)=delete
const auto & lowest_layer() const
Definition: tls_socket.hpp:66
auto async_handshake(Args &&... args)
Definition: tls_socket.hpp:126
context_handle_t m_context
Definition: tls_socket.hpp:138
void shutdown(Args &&... args)
Definition: tls_socket.hpp:112
tls_socket_t & operator=(tls_socket_t &&)=default
Socket adapter for asio::ssl::stream< asio::ip::tcp::socket >.
Definition: tls_socket.hpp:34
auto async_read_some(Args &&... args)
Definition: tls_socket.hpp:98
tls_socket_t(tls_socket_t &&)=default
void close(Args &&... args)
Definition: tls_socket.hpp:119
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64