async_mqtt 5.0.0
Loading...
Searching...
No Matches
buffer_to_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_BUFFER_TO_PACKET_VARIANT_HPP)
8#define ASYNC_MQTT_BUFFER_TO_PACKET_VARIANT_HPP
9
10#include <async_mqtt/util/optional.hpp>
11
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>
15
16namespace async_mqtt {
17
18template <std::size_t PacketIdBytes>
19basic_packet_variant<PacketIdBytes> buffer_to_basic_packet_variant(buffer buf, protocol_version ver) {
20 if (buf.size() < 2) {
21 return make_error(errc::bad_message, "packet size is too short");
22 }
23 try {
24 switch (get_control_packet_type(std::uint8_t(buf[0]))) {
25 case control_packet_type::connect:
26 switch (ver) {
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));
31 break;
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));
38 default:
39 return make_error(errc::bad_message, "connect_packet protocol_version is invalid");
40 }
41 } break;
42 case control_packet_type::connack:
43 switch (ver) {
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));
48 default:
49 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
50 }
51 break;
52 case control_packet_type::publish:
53 switch (ver) {
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));
58 default:
59 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
60 }
61 break;
62 case control_packet_type::puback:
63 switch (ver) {
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));
68 default:
69 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
70 }
71 break;
72 case control_packet_type::pubrec:
73 switch (ver) {
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));
78 default:
79 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
80 }
81 break;
82 case control_packet_type::pubrel:
83 switch (ver) {
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));
88 default:
89 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
90 }
91 break;
92 case control_packet_type::pubcomp:
93 switch (ver) {
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));
98 default:
99 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
100 }
101 break;
102 case control_packet_type::subscribe:
103 switch (ver) {
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));
108 default:
109 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
110 }
111 break;
112 case control_packet_type::suback:
113 switch (ver) {
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));
118 default:
119 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
120 }
121 break;
122 case control_packet_type::unsubscribe:
123 switch (ver) {
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));
128 default:
129 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
130 }
131 break;
132 case control_packet_type::unsuback:
133 switch (ver) {
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));
138 default:
139 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
140 }
141 break;
142 case control_packet_type::pingreq:
143 switch (ver) {
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));
148 default:
149 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
150 }
151 break;
152 case control_packet_type::pingresp:
153 switch (ver) {
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));
158 default:
159 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
160 }
161 break;
162 case control_packet_type::disconnect:
163 switch (ver) {
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));
168 default:
169 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
170 }
171 break;
172 case control_packet_type::auth:
173 switch (ver) {
174 case protocol_version::v5:
175 return v5::auth_packet(force_move(buf));
176 default:
177 return make_error(errc::bad_message, "packet mismatched to the protocol_version");
178 }
179 break;
180 default:
181 break;
182 }
183 return make_error(errc::bad_message, "control_packet_type is invalid");
184 }
185 catch (system_error const& se) {
186 return se;
187 }
188}
189
190
191inline
192packet_variant buffer_to_packet_variant(buffer buf, protocol_version ver) {
193 return buffer_to_basic_packet_variant<2>(force_move(buf), ver);
194}
195
196} // namespace async_mqtt
197
198#endif // ASYNC_MQTT_BUFFER_TO_PACKET_VARIANT_HPP