RESTinio

tls_socket.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
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
30
36 class tls_socket_t
37{
38 public:
39 using socket_t = asio_ns::ssl::stream< asio_ns::ip::tcp::socket >;
40 using context_handle_t = std::shared_ptr< asio_ns::ssl::context >;
41 // Needed for asio >= 1.16.0 (starting with boost-1.72.0)
42#if RESTINIO_ASIO_VERSION >= 101600
43 using executor_type = default_asio_executor;
44#endif
45 tls_socket_t( const tls_socket_t & ) = delete;
46 tls_socket_t & operator = ( const tls_socket_t & ) = delete;
47
48 tls_socket_t(
49 asio_ns::io_context & io_context,
50 context_handle_t tls_context )
51 : m_context{ std::move( tls_context ) }
52 , m_socket{ std::make_unique< socket_t >( io_context, *m_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 &
66 lowest_layer()
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
86 socket_t &
87 asio_ssl_stream()
88 {
89 return *m_socket;
90 }
91
101 const socket_t &
102 asio_ssl_stream() const
103 {
104 return *m_socket;
105 }
106
107 auto
108 get_executor()
109 {
110 return this->lowest_layer().get_executor();
111 }
112
113 auto
114 remote_endpoint() const
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:
168 context_handle_t m_context;
169 std::unique_ptr< socket_t > m_socket;
170};
171
172} /* namespace impl */
173
174} /* namespace restinio */
asio_include.hpp
restinio::impl::tls_socket_t
Socket adapter for asio::ssl::stream< asio::ip::tcp::socket >.
Definition: tls_socket.hpp:37
restinio::impl::tls_socket_t::m_socket
std::unique_ptr< socket_t > m_socket
Definition: tls_socket.hpp:169
restinio::impl::tls_socket_t::tls_socket_t
tls_socket_t(tls_socket_t &&)=default
restinio::impl::tls_socket_t::async_write_some
auto async_write_some(Args &&... args)
Definition: tls_socket.hpp:141
restinio::impl::tls_socket_t::tls_socket_t
tls_socket_t(const tls_socket_t &)=delete
restinio::impl::tls_socket_t::shutdown
void shutdown(Args &&... args)
Definition: tls_socket.hpp:148
restinio::impl::tls_socket_t::get_executor
auto get_executor()
Definition: tls_socket.hpp:108
restinio::impl::tls_socket_t::asio_ssl_stream
const socket_t & asio_ssl_stream() const
Get an access to underlying Asio's socket.
Definition: tls_socket.hpp:102
restinio::impl::tls_socket_t::socket_t
asio_ns::ssl::stream< asio_ns::ip::tcp::socket > socket_t
Definition: tls_socket.hpp:39
restinio::impl::tls_socket_t::tls_socket_t
tls_socket_t(asio_ns::io_context &io_context, context_handle_t tls_context)
Definition: tls_socket.hpp:48
restinio::impl::tls_socket_t::is_open
auto is_open() const
Definition: tls_socket.hpp:120
restinio::impl::tls_socket_t::operator=
tls_socket_t & operator=(const tls_socket_t &)=delete
restinio::impl::tls_socket_t::async_read_some
auto async_read_some(Args &&... args)
Definition: tls_socket.hpp:134
restinio::impl::tls_socket_t::remote_endpoint
auto remote_endpoint() const
Definition: tls_socket.hpp:114
restinio::impl::tls_socket_t::m_context
context_handle_t m_context
Definition: tls_socket.hpp:168
restinio::impl::tls_socket_t::async_handshake
auto async_handshake(Args &&... args)
Definition: tls_socket.hpp:162
restinio::impl::tls_socket_t::context_handle_t
std::shared_ptr< asio_ns::ssl::context > context_handle_t
Definition: tls_socket.hpp:40
restinio::impl::tls_socket_t::cancel
void cancel(Args &&... args)
Definition: tls_socket.hpp:127
restinio::impl::tls_socket_t::asio_ssl_stream
socket_t & asio_ssl_stream()
Get an access to underlying Asio's socket.
Definition: tls_socket.hpp:87
restinio::impl::tls_socket_t::lowest_layer
auto & lowest_layer()
Definition: tls_socket.hpp:66
restinio::impl::tls_socket_t::close
void close(Args &&... args)
Definition: tls_socket.hpp:155
restinio::impl::tls_socket_t::lowest_layer
const auto & lowest_layer() const
Definition: tls_socket.hpp:72
restinio::impl::tls_socket_t::swap
void swap(tls_socket_t &sock)
Definition: tls_socket.hpp:59
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
nonstd::optional_lite::swap
void swap(optional< T > &x, optional< T > &y)
Definition: optional.hpp:1705
restinio
Definition: asio_include.hpp:21
restinio::default_asio_executor
asio_ns::executor default_asio_executor
Definition: asio_include.hpp:224
std
STL namespace.