async_mqtt 9.0.1
Loading...
Searching...
No Matches
customized_ssl_stream.hpp
Go to the documentation of this file.
1// Copyright Takatoshi Kondo 2024
2//
3// Distributed under the Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#if !defined(ASYNC_MQTT_PREDEFINED_LAYER_CUSTOMIZED_SSL_STREAM_HPP)
8#define ASYNC_MQTT_PREDEFINED_LAYER_CUSTOMIZED_SSL_STREAM_HPP
9
10#include <boost/asio.hpp>
11#include <boost/asio/ssl.hpp>
12
13#include <async_mqtt/util/stream_traits.hpp>
14#include <async_mqtt/util/log.hpp>
15
17
18namespace async_mqtt {
19
20namespace as = boost::asio;
21namespace tls = as::ssl; // for backword compatilibity
22
23static constexpr auto shutdown_timeout = std::chrono::seconds(3);
24
33template <typename NextLayer>
34struct layer_customize<as::ssl::stream<NextLayer>> {
35 template <
36 typename CompletionToken
37 >
38 static auto
39 async_close(
40 as::ssl::stream<NextLayer>& stream,
41 CompletionToken&& token
42 ) {
43 return as::async_compose<
44 CompletionToken,
45 void(error_code const& ec)
46 > (
47 async_close_impl{
48 stream
49 },
50 token,
51 stream
52 );
53 }
54
55 struct async_close_impl {
56 as::ssl::stream<NextLayer>& stream;
57 enum {
58 shutdown,
59 complete
60 } state = shutdown;
61
62 template <typename Self>
63 void operator()(
64 Self& self
65 ) {
66 BOOST_ASSERT(state == shutdown);
67 auto tim = std::make_shared<as::steady_timer>(
68 stream.get_executor(),
69 shutdown_timeout
70 );
71 auto self_sp = std::make_shared<Self>(force_move(self));
72 tim->async_wait(
73 as::consign(
74 as::append(
75 std::ref(*self_sp),
76 std::weak_ptr<as::steady_timer>(tim)
77 ),
78 self_sp
79 )
80 );
81 stream.async_shutdown(
82 as::consign(
83 std::ref(*self_sp),
84 self_sp,
85 tim
86 )
87 );
88 }
89
90 template <typename Self>
91 void operator()(
92 Self& self,
93 error_code const& ec,
94 std::weak_ptr<as::steady_timer> wp
95 ) {
96 if (!ec) {
97 if (auto sp = wp.lock()) {
98 ASYNC_MQTT_LOG("mqtt_impl", info)
99 << "TLS async_shutdown timeout";
100 BOOST_ASSERT(state == shutdown);
101 state = complete;
102 self.complete(ec);
103 return;
104 }
105 }
106 ASYNC_MQTT_LOG("mqtt_impl", info)
107 << "TLS async_shutdown timeout doesn't processed. ec:" << ec.message();
108 }
109
110 template <typename Self>
111 void operator()(
112 Self& self,
113 error_code const& ec
114 ) {
115 if (state == complete) {
116 ASYNC_MQTT_LOG("mqtt_impl", info)
117 << "TLS async_shutdown already timeout";
118 }
119 else {
120 ASYNC_MQTT_LOG("mqtt_impl", info)
121 << "TLS async_shutdown ec:" << ec.message();
122 state = complete;
123 self.complete(ec);
124 }
125 }
126 };
127};
128
129} // namespace async_mqtt
130
131#endif // ASYNC_MQTT_PREDEFINED_LAYER_CUSTOMIZED_SSL_STREAM_HPP
sys::error_code error_code
sys is a namespace alias of boost::sytem.
Definition error.hpp:56
@ info
info level api call is output
customization class template for underlying layer In order to adapt your layer to async_mqtt,...
Definition stream_traits.hpp:101