RESTinio
Loading...
Searching...
No Matches
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
19namespace restinio
20{
21
22namespace 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:
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 )
53 {}
54
55 tls_socket_t( tls_socket_t && ) = default;
56 tls_socket_t & operator = ( tls_socket_t && ) = default;
57
58 void
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 &
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 */
#define RESTINIO_ASIO_VERSION
Socket adapter for asio::ssl::stream< asio::ip::tcp::socket >.
std::unique_ptr< socket_t > m_socket
tls_socket_t(tls_socket_t &&)=default
auto async_write_some(Args &&... args)
tls_socket_t(const tls_socket_t &)=delete
void shutdown(Args &&... args)
const socket_t & asio_ssl_stream() const
Get an access to underlying Asio's socket.
asio_ns::ssl::stream< asio_ns::ip::tcp::socket > socket_t
tls_socket_t(asio_ns::io_context &io_context, context_handle_t tls_context)
tls_socket_t & operator=(const tls_socket_t &)=delete
tls_socket_t & operator=(tls_socket_t &&)=default
auto async_read_some(Args &&... args)
auto async_handshake(Args &&... args)
std::shared_ptr< asio_ns::ssl::context > context_handle_t
void cancel(Args &&... args)
socket_t & asio_ssl_stream()
Get an access to underlying Asio's socket.
void close(Args &&... args)
const auto & lowest_layer() const
void swap(tls_socket_t &sock)