RESTinio
tls_socket.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
12
13#if !defined(RESTINIO_USE_BOOST_ASIO)
14 #include <asio/ssl.hpp>
15#else
16 #include <boost/asio/ssl.hpp>
17#endif
18
19namespace restinio
20{
21
22namespace impl
23{
24
25//
26// tls_socket_t
27//
28
30
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
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;
57
58 void
60 {
63 }
64
65 auto &
67 {
68 return m_socket->lowest_layer();
69 }
70
71 const auto &
73 {
74 return m_socket->lowest_layer();
75 }
76
86 socket_t &
88 {
89 return *m_socket;
90 }
91
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:
169 std::unique_ptr< socket_t > m_socket;
170};
171
172} /* namespace impl */
173
174} /* namespace restinio */
Socket adapter for asio::ssl::stream< asio::ip::tcp::socket >.
Definition: tls_socket.hpp:37
std::unique_ptr< socket_t > m_socket
Definition: tls_socket.hpp:169
tls_socket_t(tls_socket_t &&)=default
auto async_write_some(Args &&... args)
Definition: tls_socket.hpp:141
tls_socket_t(const tls_socket_t &)=delete
void shutdown(Args &&... args)
Definition: tls_socket.hpp:148
const socket_t & asio_ssl_stream() const
Get an access to underlying Asio's socket.
Definition: tls_socket.hpp:102
asio_ns::ssl::stream< asio_ns::ip::tcp::socket > socket_t
Definition: tls_socket.hpp:39
tls_socket_t(asio_ns::io_context &io_context, context_handle_t tls_context)
Definition: tls_socket.hpp:48
tls_socket_t & operator=(const tls_socket_t &)=delete
auto async_read_some(Args &&... args)
Definition: tls_socket.hpp:134
context_handle_t m_context
Definition: tls_socket.hpp:168
auto async_handshake(Args &&... args)
Definition: tls_socket.hpp:162
std::shared_ptr< asio_ns::ssl::context > context_handle_t
Definition: tls_socket.hpp:40
void cancel(Args &&... args)
Definition: tls_socket.hpp:127
socket_t & asio_ssl_stream()
Get an access to underlying Asio's socket.
Definition: tls_socket.hpp:87
void close(Args &&... args)
Definition: tls_socket.hpp:155
const auto & lowest_layer() const
Definition: tls_socket.hpp:72
void swap(tls_socket_t &sock)
Definition: tls_socket.hpp:59
void swap(optional< T > &x, optional< T > &y)
Definition: optional.hpp:1705
asio_ns::executor default_asio_executor
STL namespace.