async_mqtt 9.0.1
Loading...
Searching...
No Matches
stream.hpp
1// Copyright Takatoshi Kondo 2022
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_UTIL_STREAM_HPP)
8#define ASYNC_MQTT_UTIL_STREAM_HPP
9
10#include <utility>
11#include <type_traits>
12#include <deque>
13
14#include <boost/asio/async_result.hpp>
15
16#include <async_mqtt/util/stream_traits.hpp>
17#include <async_mqtt/util/static_vector.hpp>
18#include <async_mqtt/util/ioc_queue.hpp>
19#include <async_mqtt/util/buffer.hpp>
20#include <async_mqtt/error.hpp>
21#include <async_mqtt/util/log.hpp>
22
23#include <async_mqtt/util/detail/stream_impl.hpp>
24
25namespace async_mqtt {
26namespace as = boost::asio;
27namespace sys = boost::system;
28
29template <typename NextLayer>
30class stream {
31public:
32 using this_type = stream<NextLayer>;
33 using impl_type = detail::stream_impl<NextLayer>;
34 using next_layer_type = typename std::remove_reference<NextLayer>::type;
35 using lowest_layer_type =
36 typename std::remove_reference<
37 decltype(get_lowest_layer(std::declval<next_layer_type&>()))
38 >::type;
39 using executor_type = async_mqtt::executor_type<next_layer_type>;
40
41 template <
42 typename T,
43 typename... Args,
44 std::enable_if_t<!std::is_same_v<std::decay_t<T>, this_type>>* = nullptr
45 >
46 explicit
47 stream(T&& t, Args&&... args)
48 :impl_{
49 std::make_shared<impl_type>(
50 std::forward<T>(t),
51 std::forward<Args>(args)...
52 )
53 }
54 {
55 }
56
57 ~stream() {
58 ASYNC_MQTT_LOG("mqtt_impl", trace)
59 << ASYNC_MQTT_ADD_VALUE(address, this)
60 << "destroy";
61 }
62
63 stream(this_type&&) = default;
64 stream(this_type const&) = delete;
65 this_type& operator=(this_type&&) = default;
66 this_type& operator=(this_type const&) = delete;
67
68 next_layer_type const& next_layer() const {
69 return impl_->next_layer();
70 }
71 next_layer_type& next_layer() {
72 return impl_->next_layer();
73 }
74
75 lowest_layer_type const& lowest_layer() const {
76 return impl_->lowest_layer();
77 }
78 lowest_layer_type& lowest_layer() {
79 return impl_->lowest_layer();
80 }
81
82 template <
83 typename CompletionToken = as::default_completion_token_t<executor_type>
84 >
85 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
86 CompletionToken,
87 void(error_code, buffer)
88 )
89 async_read_packet(
90 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
91 );
92
93 template <
94 typename Packet,
95 typename CompletionToken = as::default_completion_token_t<executor_type>
96 >
97 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
98 CompletionToken,
99 void(error_code)
100 )
101 async_write_packet(
102 Packet packet,
103 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
104 );
105
106 as::any_io_executor get_executor() {
107 return impl_->get_executor();
108 };
109
110 template <
111 typename CompletionToken = as::default_completion_token_t<executor_type>
112 >
113 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
114 CompletionToken,
115 void()
116 )
117 async_close(
118 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
119 );
120
121 void set_bulk_write(bool val) {
122 impl_->set_bulk_write(val);
123 }
124
125 template <typename Executor1>
126 struct rebind_executor {
127 using other = stream<
128 typename NextLayer::template rebind_executor<Executor1>::other
129 >;
130 };
131
132 void set_bulk_read_buffer_size(std::size_t size) {
133 impl_->set_bulk_read_buffer_size(size);
134 }
135
136private:
137 // constructor
138 template <typename Other>
139 explicit
140 stream(
141 stream<Other>&& other
142 )
143 :impl_{force_move(other.impl_)}
144 {
145 }
146
147private:
148 std::shared_ptr<impl_type> impl_;
149};
150
151} // namespace async_mqtt
152
153#include <async_mqtt/util/impl/stream_read_packet.hpp>
154#include <async_mqtt/util/impl/stream_write_packet.hpp>
155#include <async_mqtt/util/impl/stream_close.hpp>
156
157#endif // ASYNC_MQTT_UTIL_STREAM_HPP
sys::error_code error_code
sys is a namespace alias of boost::sytem.
Definition error.hpp:56
@ trace
trace level for detaied behavior and reporting issue