async_mqtt 9.0.1
Loading...
Searching...
No Matches
store_packet_variant.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_PACKET_STORE_PACKET_VARIANT_HPP)
8#define ASYNC_MQTT_PACKET_STORE_PACKET_VARIANT_HPP
9
10#include <variant>
11
12#include <async_mqtt/util/overload.hpp>
13#include <async_mqtt/packet/store_packet_variant_fwd.hpp>
14#include <async_mqtt/packet/packet_id_type.hpp>
15#include <async_mqtt/packet/v3_1_1_publish.hpp>
16#include <async_mqtt/packet/v3_1_1_pubrel.hpp>
17#include <async_mqtt/packet/v5_publish.hpp>
18#include <async_mqtt/packet/v5_pubrel.hpp>
19
20namespace async_mqtt {
21
39
40template <std::size_t PacketIdBytes>
41class basic_store_packet_variant {
42public:
43
49 :res_{
50 [&] {
51 switch (packet.opts().get_qos()) {
56 default:
57 throw system_error{
60 )
61 };
62 }
63 }()
64 },
65 var_{force_move(packet)}
66 {}
67
76
82 :res_{
83 [&] {
84 switch (packet.opts().get_qos()) {
89 default:
90 throw system_error{
93 )
94 };
95 }
96 }()
97 },
98 var_{force_move(packet)}
99 {}
100
107 var_{force_move(packet)}
108 {}
109
114 template <typename Func>
115 auto visit(Func&& func) const& {
116 return
117 std::visit(
118 std::forward<Func>(func),
119 var_
120 );
121 }
122
127 template <typename Func>
128 auto visit(Func&& func) & {
129 return
130 std::visit(
131 std::forward<Func>(func),
132 var_
133 );
134 }
135
140 template <typename Func>
141 auto visit(Func&& func) && {
142 return
143 std::visit(
144 std::forward<Func>(func),
145 force_move(var_)
146 );
147 }
148
154 std::vector<as::const_buffer> const_buffer_sequence() const {
155 return visit(
156 overload {
157 [] (auto const& p) {
158 return p.const_buffer_sequence();
159 },
160 [] (error_code const&) {
161 BOOST_ASSERT(false);
162 return std::vector<as::const_buffer>{};
163 }
164 }
165 );
166 }
167
173 return visit(
174 overload {
175 [] (auto const& p) {
176 return p.packet_id();
177 }
178 }
179 );
180 }
181
186 std::size_t size() const {
187 return visit(
188 overload {
189 [] (auto const& p) -> std::size_t {
190 return p.size();
191 }
192 }
193 );
194 }
195
200 std::uint32_t get_message_expiry_interval() const {
201 return visit(
202 overload {
204 std::uint32_t ret = 0;
205 bool finish = false;
206 for (auto const& prop : p.props()) {
207 prop.visit(
208 overload {
210 ret = p.val();
211 finish = true;
212 },
213 [](auto const&) {
214 }
215 }
216 );
217 if (finish) break;
218 }
219 return ret;
220 },
221 [] (auto const&) {
222 return std::uint32_t(0);
223 }
224 }
225 );
226 }
227
232 void update_message_expiry_interval(std::uint32_t val) const {
233 visit(
234 overload {
236 bool finish = false;
237 for (auto& prop : p.props()) {
238 prop.visit(
239 overload {
242 finish = true;
243 },
244 [](auto&) {
245 }
246 }
247 );
248 if (finish) break;
249 }
250 },
251 [] (auto&) {
252 }
253 }
254 );
255 }
256
262 return res_;
263 }
264
265private:
266 using variant_t = std::variant<
271 >;
272
273 response_packet res_;
274 variant_t var_;
275};
276
277template <std::size_t PacketIdBytes>
278inline std::ostream& operator<<(std::ostream& o, basic_store_packet_variant<PacketIdBytes> const& v) {
279 v.visit(
280 overload {
281 [&] (auto const& p) {
282 o << p;
283 }
284 }
285 );
286 return o;
287}
288
289} // namespace async_mqtt
290
291#endif // ASYNC_MQTT_PACKET_STORE_PACKET_VARIANT_HPP
response_packet response_packet_type() const
Get response packet type corresponding to this packet.
Definition store_packet_variant.hpp:261
auto visit(Func &&func) const &
visit to variant
Definition store_packet_variant.hpp:115
basic_store_packet_variant(v3_1_1::basic_pubrel_packet< PacketIdBytes > packet)
constructor
Definition store_packet_variant.hpp:72
basic_store_packet_variant(v5::basic_publish_packet< PacketIdBytes > packet)
constructor
Definition store_packet_variant.hpp:81
std::vector< as::const_buffer > const_buffer_sequence() const
Create const buffer sequence it is for boost asio APIs.
Definition store_packet_variant.hpp:154
std::size_t size() const
Get packet size.
Definition store_packet_variant.hpp:186
basic_store_packet_variant(v5::basic_pubrel_packet< PacketIdBytes > packet)
constructor
Definition store_packet_variant.hpp:105
auto visit(Func &&func) &&
visit to variant
Definition store_packet_variant.hpp:141
basic_packet_id_type< PacketIdBytes >::type packet_id() const
Get packet id.
Definition store_packet_variant.hpp:172
void update_message_expiry_interval(std::uint32_t val) const
Update MessageExpiryInterval property.
Definition store_packet_variant.hpp:232
auto visit(Func &&func) &
visit to variant
Definition store_packet_variant.hpp:128
basic_store_packet_variant(v3_1_1::basic_publish_packet< PacketIdBytes > packet)
constructor
Definition store_packet_variant.hpp:48
std::uint32_t get_message_expiry_interval() const
Get MessageExpiryInterval property value.
Definition store_packet_variant.hpp:200
message_expiry_interval property
Definition property.hpp:193
MQTT PUBLISH packet (v3.1.1)
Definition v3_1_1_publish.hpp:61
constexpr pub::opts opts() const
Get publish_options.
Definition v3_1_1_publish.hpp:165
MQTT PUBREL packet (v3.1.1)
Definition v3_1_1_pubrel.hpp:55
MQTT PUBLISH packet (v5)
Definition v5_publish.hpp:63
constexpr pub::opts opts() const
Get publish_options.
Definition v5_publish.hpp:172
MQTT PUBREL packet (v5)
Definition v5_pubrel.hpp:56
sys::error_code error_code
sys is a namespace alias of boost::sytem.
Definition error.hpp:56
sys::system_error system_error
system_error is a type alias of boost::sytem::system_error.
Definition error.hpp:67
error_code make_error_code(mqtt_error v)
make error code
@ packet_not_allowed_to_store
Packet is not allowd to be stored.
@ exactly_once
Exactly once delivery.
@ at_least_once
At least once delivery.
response_packet
corresponding response packet
Definition store_packet_variant.hpp:31
@ 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
packet idenfitifer type class template
Definition packet_id_type.hpp:25
constexpr qos get_qos() const
Get qos.
Definition pubopts.hpp:248