7#if !defined(ASYNC_MQTT_BUFFER_TO_PACKET_VARIANT_HPP)
8#define ASYNC_MQTT_BUFFER_TO_PACKET_VARIANT_HPP
10#include <async_mqtt/util/optional.hpp>
12#include <async_mqtt/packet/packet_variant.hpp>
13#include <async_mqtt/packet/control_packet_type.hpp>
14#include <async_mqtt/packet/get_protocol_version.hpp>
18template <std::
size_t PacketIdBytes>
19basic_packet_variant<PacketIdBytes> buffer_to_basic_packet_variant(buffer buf, protocol_version ver) {
21 return make_error(errc::bad_message,
"packet size is too short");
24 switch (get_control_packet_type(std::uint8_t(buf[0]))) {
25 case control_packet_type::connect:
27 case protocol_version::v3_1_1:
28 return v3_1_1::connect_packet(force_move(buf));
29 case protocol_version::v5:
30 return v5::connect_packet(force_move(buf));
32 case protocol_version::undetermined:
33 switch (get_protocol_version(buf)) {
34 case protocol_version::v3_1_1:
35 return v3_1_1::connect_packet(force_move(buf));
36 case protocol_version::v5:
37 return v5::connect_packet(force_move(buf));
39 return make_error(errc::bad_message,
"connect_packet protocol_version is invalid");
42 case control_packet_type::connack:
44 case protocol_version::v3_1_1:
45 return v3_1_1::connack_packet(force_move(buf));
46 case protocol_version::v5:
47 return v5::connack_packet(force_move(buf));
49 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
52 case control_packet_type::publish:
54 case protocol_version::v3_1_1:
55 return v3_1_1::basic_publish_packet<PacketIdBytes>(force_move(buf));
56 case protocol_version::v5:
57 return v5::basic_publish_packet<PacketIdBytes>(force_move(buf));
59 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
62 case control_packet_type::puback:
64 case protocol_version::v3_1_1:
65 return v3_1_1::basic_puback_packet<PacketIdBytes>(force_move(buf));
66 case protocol_version::v5:
67 return v5::basic_puback_packet<PacketIdBytes>(force_move(buf));
69 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
72 case control_packet_type::pubrec:
74 case protocol_version::v3_1_1:
75 return v3_1_1::basic_pubrec_packet<PacketIdBytes>(force_move(buf));
76 case protocol_version::v5:
77 return v5::basic_pubrec_packet<PacketIdBytes>(force_move(buf));
79 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
82 case control_packet_type::pubrel:
84 case protocol_version::v3_1_1:
85 return v3_1_1::basic_pubrel_packet<PacketIdBytes>(force_move(buf));
86 case protocol_version::v5:
87 return v5::basic_pubrel_packet<PacketIdBytes>(force_move(buf));
89 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
92 case control_packet_type::pubcomp:
94 case protocol_version::v3_1_1:
95 return v3_1_1::basic_pubcomp_packet<PacketIdBytes>(force_move(buf));
96 case protocol_version::v5:
97 return v5::basic_pubcomp_packet<PacketIdBytes>(force_move(buf));
99 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
102 case control_packet_type::subscribe:
104 case protocol_version::v3_1_1:
105 return v3_1_1::basic_subscribe_packet<PacketIdBytes>(force_move(buf));
106 case protocol_version::v5:
107 return v5::basic_subscribe_packet<PacketIdBytes>(force_move(buf));
109 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
112 case control_packet_type::suback:
114 case protocol_version::v3_1_1:
115 return v3_1_1::basic_suback_packet<PacketIdBytes>(force_move(buf));
116 case protocol_version::v5:
117 return v5::basic_suback_packet<PacketIdBytes>(force_move(buf));
119 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
122 case control_packet_type::unsubscribe:
124 case protocol_version::v3_1_1:
125 return v3_1_1::basic_unsubscribe_packet<PacketIdBytes>(force_move(buf));
126 case protocol_version::v5:
127 return v5::basic_unsubscribe_packet<PacketIdBytes>(force_move(buf));
129 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
132 case control_packet_type::unsuback:
134 case protocol_version::v3_1_1:
135 return v3_1_1::basic_unsuback_packet<PacketIdBytes>(force_move(buf));
136 case protocol_version::v5:
137 return v5::basic_unsuback_packet<PacketIdBytes>(force_move(buf));
139 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
142 case control_packet_type::pingreq:
144 case protocol_version::v3_1_1:
145 return v3_1_1::pingreq_packet(force_move(buf));
146 case protocol_version::v5:
147 return v5::pingreq_packet(force_move(buf));
149 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
152 case control_packet_type::pingresp:
154 case protocol_version::v3_1_1:
155 return v3_1_1::pingresp_packet(force_move(buf));
156 case protocol_version::v5:
157 return v5::pingresp_packet(force_move(buf));
159 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
162 case control_packet_type::disconnect:
164 case protocol_version::v3_1_1:
165 return v3_1_1::disconnect_packet(force_move(buf));
166 case protocol_version::v5:
167 return v5::disconnect_packet(force_move(buf));
169 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
172 case control_packet_type::auth:
174 case protocol_version::v5:
175 return v5::auth_packet(force_move(buf));
177 return make_error(errc::bad_message,
"packet mismatched to the protocol_version");
183 return make_error(errc::bad_message,
"control_packet_type is invalid");
185 catch (system_error
const& se) {
192packet_variant buffer_to_packet_variant(buffer buf, protocol_version ver) {
193 return buffer_to_basic_packet_variant<2>(force_move(buf), ver);