async_mqtt 5.0.0
Loading...
Searching...
No Matches
store_packet_variant.hpp
Go to the documentation of this file.
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_PACKET_STORE_PACKET_VARIANT_HPP)
8#define ASYNC_MQTT_PACKET_STORE_PACKET_VARIANT_HPP
9
10#include <async_mqtt/util/variant.hpp>
11#include <async_mqtt/packet/packet_id_type.hpp>
12#include <async_mqtt/packet/v3_1_1_publish.hpp>
13#include <async_mqtt/packet/v3_1_1_pubrel.hpp>
14#include <async_mqtt/packet/v5_publish.hpp>
15#include <async_mqtt/packet/v5_pubrel.hpp>
16#include <async_mqtt/exception.hpp>
17
19
20namespace async_mqtt {
21
33
38template <std::size_t PacketIdBytes>
40public:
41 using packet_id_t = typename packet_id_type<PacketIdBytes>::type;
42
48 :res_{
49 [&] {
50 switch (packet.opts().get_qos()) {
51 case qos::at_least_once:
52 return response_packet::v3_1_1_puback;
53 case qos::exactly_once:
54 return response_packet::v3_1_1_pubrec;
55 default:
56 throw make_error(
57 errc::bad_message,
58 "qos::at_most_once cannot store"
59 );
60 }
61 }()
62 },
63 var_{force_move(packet)}
64 {}
65
74
79 basic_store_packet_variant(v5::basic_publish_packet<PacketIdBytes> packet)
80 :res_{
81 [&] {
82 switch (packet.opts().get_qos()) {
83 case qos::at_least_once:
84 return response_packet::v5_puback;
85 case qos::exactly_once:
86 return response_packet::v5_pubrec;
87 default:
88 throw make_error(
89 errc::bad_message,
90 "qos::at_most_once cannot store"
91 );
92 }
93 }()
94 },
95 var_{force_move(packet)}
96 {}
97
104 var_{force_move(packet)}
105 {}
106
111 template <typename Func>
112 auto visit(Func&& func) const& {
113 return
114 async_mqtt::visit(
115 std::forward<Func>(func),
116 var_
117 );
118 }
119
124 template <typename Func>
125 auto visit(Func&& func) & {
126 return
127 async_mqtt::visit(
128 std::forward<Func>(func),
129 var_
130 );
131 }
132
137 template <typename Func>
138 auto visit(Func&& func) && {
139 return
140 async_mqtt::visit(
141 std::forward<Func>(func),
142 force_move(var_)
143 );
144 }
145
151 std::vector<as::const_buffer> const_buffer_sequence() const {
152 return visit(
153 overload {
154 [] (auto const& p) {
155 return p.const_buffer_sequence();
156 },
157 [] (system_error const&) {
158 BOOST_ASSERT(false);
159 return std::vector<as::const_buffer>{};
160 }
161 }
162 );
163 }
164
169 packet_id_t packet_id() const {
170 return visit(
171 overload {
172 [] (auto const& p) {
173 return p.packet_id();
174 }
175 }
176 );
177 }
178
183 std::size_t size() const {
184 return visit(
185 overload {
186 [] (auto const& p) -> std::size_t {
187 return p.size();
188 }
189 }
190 );
191 }
192
197 std::uint32_t get_message_expiry_interval() const {
198 return visit(
199 overload {
200 [] (v5::basic_publish_packet<PacketIdBytes> const& p) {
201 std::uint32_t ret = 0;
202 bool finish = false;
203 for (auto const& prop : p.props()) {
204 prop.visit(
205 overload {
207 ret = p.val();
208 finish = true;
209 },
210 [](auto const&) {
211 }
212 }
213 );
214 if (finish) break;
215 }
216 return ret;
217 },
218 [] (auto const&) {
219 return std::uint32_t(0);
220 }
221 }
222 );
223 }
224
229 void update_message_expiry_interval(std::uint32_t val) const {
230 visit(
231 overload {
232 [&] (v5::basic_publish_packet<PacketIdBytes>& p) {
233 bool finish = false;
234 for (auto& prop : p.props()) {
235 prop.visit(
236 overload {
239 finish = true;
240 },
241 [](auto&) {
242 }
243 }
244 );
245 if (finish) break;
246 }
247 },
248 [] (auto&) {
249 }
250 }
251 );
252 }
253
259 return res_;
260 }
261
262private:
263 using variant_t = variant<
266 v5::basic_publish_packet<PacketIdBytes>,
268 >;
269
270 response_packet res_;
271 variant_t var_;
272};
273
274template <std::size_t PacketIdBytes>
275inline std::ostream& operator<<(std::ostream& o, basic_store_packet_variant<PacketIdBytes> const& v) {
276 v.visit(
277 overload {
278 [&] (auto const& p) {
279 o << p;
280 }
281 }
282 );
283 return o;
284}
285
286using store_packet_variant = basic_store_packet_variant<2>;
287
288} // namespace async_mqtt
289
290#endif // ASYNC_MQTT_PACKET_STORE_PACKET_VARIANT_HPP
Definition packet_variant.hpp:49
auto visit(Func &&func) const &
visit to variant
Definition packet_variant.hpp:74
MQTT packet variant for store.
Definition store_packet_variant.hpp:39
response_packet response_packet_type() const
Get response packet type corresponding to this packet.
Definition store_packet_variant.hpp:258
auto visit(Func &&func) const &
visit to variant
Definition store_packet_variant.hpp:112
basic_store_packet_variant(v3_1_1::basic_pubrel_packet< PacketIdBytes > packet)
constructor
Definition store_packet_variant.hpp:70
basic_store_packet_variant(v5::basic_publish_packet< PacketIdBytes > packet)
constructor
Definition store_packet_variant.hpp:79
std::vector< as::const_buffer > const_buffer_sequence() const
Create const buffer sequence it is for boost asio APIs.
Definition store_packet_variant.hpp:151
std::size_t size() const
Get packet size.
Definition store_packet_variant.hpp:183
basic_store_packet_variant(v5::basic_pubrel_packet< PacketIdBytes > packet)
constructor
Definition store_packet_variant.hpp:102
packet_id_t packet_id() const
Get packet id.
Definition store_packet_variant.hpp:169
auto visit(Func &&func) &&
visit to variant
Definition store_packet_variant.hpp:138
void update_message_expiry_interval(std::uint32_t val) const
Definition store_packet_variant.hpp:229
auto visit(Func &&func) &
visit to variant
Definition store_packet_variant.hpp:125
basic_store_packet_variant(v3_1_1::basic_publish_packet< PacketIdBytes > packet)
constructor
Definition store_packet_variant.hpp:47
std::uint32_t get_message_expiry_interval() const
Definition store_packet_variant.hpp:197
MQTT PUBLISH packet (v3.1.1)
Definition v3_1_1_publish.hpp:50
constexpr pub::opts opts() const
Get publish_options.
Definition v3_1_1_publish.hpp:331
MQTT PUBREL packet (v3.1.1)
Definition v3_1_1_pubrel.hpp:41
MQTT PUBREL packet (v5)
Definition v5_pubrel.hpp:45
response_packet
corresponding response packet
Definition store_packet_variant.hpp:25
@ v5_pubrec
stored packet is v5_basic_publish_packet QoS2
@ v3_1_1_pubcomp
stored packet is v3_1_1_basic_pubrel_packet
@ v3_1_1_pubrec
stored packet is v3_1_1_basic_publish_packet QoS2
@ v5_pubcomp
stored packet is v5_basic_rel_packet
@ v3_1_1_puback
stored packet is v3_1_1_basic_publish_packet QoS1
@ v5_puback
stored packet is v5_basic_publish_packet QoS1
async_mqtt error class. It is used as CompletionToken parameter and exception.
Definition exception.hpp:29