41 using packet_id_t =
typename packet_id_type<PacketIdBytes>::type;
53 std::vector<topic_sharename>
params,
56 : fixed_header_{make_fixed_header(control_packet_type::unsubscribe, 0b0010)},
57 entries_{force_move(
params)},
59 property_length_(async_mqtt::
size(
props)),
60 props_(force_move(
props))
62 using namespace std::literals;
63 topic_length_buf_entries_.reserve(entries_.size());
64 for (
auto const&
e : entries_) {
65 topic_length_buf_entries_.push_back(
67 boost::numeric_cast<std::uint16_t>(
e.all_topic().size())
72 endian_store(
packet_id, packet_id_.data());
74 auto pb = val_to_variable_bytes(boost::numeric_cast<std::uint32_t>(property_length_));
76 property_length_buf_.push_back(
e);
79 for (
auto const& prop : props_) {
81 if (!validate_property(property_location::unsubscribe,
id)) {
84 "v5::unsubscribe_packet property "s + id_to_str(
id) +
" is not allowed"
89 remaining_length_ += property_length_buf_.size() + property_length_;
91 for (
auto const& e : entries_) {
92 auto size = e.all_topic().size();
96 "v5::unsubscribe_packet length of topic is invalid"
103 if (!utf8string_check(e.all_topic())) {
106 "v5::unsubscribe_packet topic filter invalid utf8"
111 remaining_length_buf_ = val_to_variable_bytes(boost::numeric_cast<std::uint32_t>(remaining_length_));
119 "v5::unsubscribe_packet fixed_header doesn't exist"
122 fixed_header_ =
static_cast<std::uint8_t
>(buf.front());
123 buf.remove_prefix(1);
124 auto cpt_opt = get_control_packet_type_with_check(
static_cast<std::uint8_t
>(fixed_header_));
125 if (!cpt_opt || *cpt_opt != control_packet_type::unsubscribe) {
128 "v5::unsubscribe_packet fixed_header is invalid"
133 if (
auto vl_opt = insert_advance_variable_length(buf, remaining_length_buf_)) {
134 remaining_length_ = *vl_opt;
137 throw make_error(errc::bad_message,
"v5::unsubscribe_packet remaining length is invalid");
139 if (remaining_length_ != buf.size()) {
140 throw make_error(errc::bad_message,
"v5::unsubscribe_packet remaining length doesn't match buf.size()");
144 if (!copy_advance(buf, packet_id_)) {
147 "v5::unsubscribe_packet packet_id doesn't exist"
152 auto it = buf.begin();
153 if (
auto pl_opt = variable_bytes_to_val(it, buf.end())) {
154 property_length_ = *pl_opt;
155 std::copy(buf.begin(), it, std::back_inserter(property_length_buf_));
156 buf.remove_prefix(std::size_t(std::distance(buf.begin(), it)));
157 if (buf.size() < property_length_) {
160 "v5::unsubscribe_packet properties_don't match its length"
163 auto prop_buf = buf.substr(0, property_length_);
164 props_ = make_properties(prop_buf, property_location::unsubscribe);
165 buf.remove_prefix(property_length_);
170 "v5::unsubscribe_packet property_length is invalid"
174 if (remaining_length_ == 0) {
175 throw make_error(errc::bad_message,
"v5::unsubscribe_packet doesn't have entries");
178 while (!buf.empty()) {
180 static_vector<char, 2> topic_length_buf;
181 if (!insert_advance(buf, topic_length_buf)) {
184 "v5::unsubscribe_packet length of topic is invalid"
187 auto topic_length = endian_load<std::uint16_t>(topic_length_buf.data());
188 topic_length_buf_entries_.push_back(topic_length_buf);
191 if (buf.size() < topic_length) {
194 "v5::unsubscribe_packet topic doesn't match its length"
197 auto topic = buf.substr(0, topic_length);
198 if (!utf8string_check(topic)) {
201 "v5::unsubscribe_packet topic filter invalid utf8"
204 buf.remove_prefix(topic_length);
205 entries_.emplace_back(force_move(topic));
209 constexpr control_packet_type type()
const {
210 return control_packet_type::unsubscribe;
219 std::vector<as::const_buffer>
ret;
222 ret.emplace_back(as::buffer(&fixed_header_, 1));
224 ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
226 ret.emplace_back(as::buffer(packet_id_.data(), packet_id_.size()));
228 ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
229 auto props_cbs = async_mqtt::const_buffer_sequence(props_);
232 BOOST_ASSERT(entries_.size() == topic_length_buf_entries_.size());
233 auto it = topic_length_buf_entries_.begin();
234 for (
auto const&
e : entries_) {
235 ret.emplace_back(as::buffer(
it->data(),
it->size()));
236 ret.emplace_back(as::buffer(
e.all_topic()));
250 remaining_length_buf_.size() +
263 [&] () -> std::size_t {
264 if (property_length_buf_.size() == 0)
return 0;
267 async_mqtt::num_of_const_buffer_sequence(props_);
284 std::vector<topic_sharename>
const&
entries()
const {
297 std::uint8_t fixed_header_;
298 std::vector<static_vector<char, 2>> topic_length_buf_entries_;
299 std::vector<topic_sharename> entries_;
301 std::size_t remaining_length_;
304 std::size_t property_length_ = 0;