async_mqtt 5.0.0
Loading...
Searching...
No Matches
v3_1_1_pubcomp.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_V3_1_1_PUBCOMP_HPP)
8#define ASYNC_MQTT_PACKET_V3_1_1_PUBCOMP_HPP
9
10#include <utility>
11#include <numeric>
12
13#include <boost/numeric/conversion/cast.hpp>
14
15#include <async_mqtt/exception.hpp>
16#include <async_mqtt/buffer.hpp>
17
18#include <async_mqtt/util/move.hpp>
19#include <async_mqtt/util/static_vector.hpp>
20#include <async_mqtt/util/endian_convert.hpp>
21
22#include <async_mqtt/packet/packet_id_type.hpp>
23#include <async_mqtt/packet/fixed_header.hpp>
24
25namespace async_mqtt::v3_1_1 {
26
27namespace as = boost::asio;
28
39template <std::size_t PacketIdBytes>
41public:
42 using packet_id_t = typename packet_id_type<PacketIdBytes>::type;
43
49 packet_id_t packet_id
50 )
51 : all_(all_.capacity())
52 {
53 all_[0] = static_cast<char>(make_fixed_header(control_packet_type::pubcomp, 0b0000));
54 all_[1] = boost::numeric_cast<char>(PacketIdBytes);
55 endian_store(packet_id, &all_[2]);
56 }
57
59 // fixed_header
60 if (buf.empty()) {
61 throw make_error(
62 errc::bad_message,
63 "v3_1_1::pubcomp_packet fixed_header doesn't exist"
64 );
65 }
66 all_.push_back(buf.front());
67 buf.remove_prefix(1);
68 auto cpt_opt = get_control_packet_type_with_check(static_cast<std::uint8_t>(all_.back()));
69 if (!cpt_opt || *cpt_opt != control_packet_type::pubcomp) {
70 throw make_error(
71 errc::bad_message,
72 "v3_1_1::pubcomp_packet fixed_header is invalid"
73 );
74 }
75
76 // remaining_length
77 if (buf.empty()) {
78 throw make_error(
79 errc::bad_message,
80 "v3_1_1::pubcomp_packet remaining_length doesn't exist"
81 );
82 }
83 all_.push_back(buf.front());
84 buf.remove_prefix(1);
85 if (static_cast<std::uint8_t>(all_.back()) != PacketIdBytes) {
86 throw make_error(
87 errc::bad_message,
88 "v3_1_1::pubcomp_packet remaining_length is invalid"
89 );
90 }
91
92 // variable header
93 if (buf.size() != PacketIdBytes) {
94 throw make_error(
95 errc::bad_message,
96 "v3_1_1::pubcomp_packet variable header doesn't match its length"
97 );
98 }
99 std::copy(buf.begin(), buf.end(), std::back_inserter(all_));
100 }
101
102 constexpr control_packet_type type() const {
103 return control_packet_type::pubcomp;
104 }
105
111 std::vector<as::const_buffer> const_buffer_sequence() const {
112 std::vector<as::const_buffer> ret;
113
114 ret.emplace_back(as::buffer(all_.data(), all_.size()));
115 return ret;
116 }
117
122 std::size_t size() const {
123 return all_.size();
124 }
125
130 static constexpr std::size_t num_of_const_buffer_sequence() {
131 return 1; // all
132 }
133
138 packet_id_t packet_id() const {
139 return endian_load<packet_id_t>(&all_[2]);
140 }
141
142private:
143 boost::container::static_vector<char, 2 + PacketIdBytes> all_;
144};
145
146template <std::size_t PacketIdBytes>
147inline std::ostream& operator<<(std::ostream& o, basic_pubcomp_packet<PacketIdBytes> const& v) {
148 o <<
149 "v3_1_1::pubcomp{" <<
150 "pid:" << v.packet_id() <<
151 "}";
152 return o;
153}
154
160
161} // namespace async_mqtt::v3_1_1
162
163#endif // ASYNC_MQTT_PACKET_V3_1_1_PUBCOMP_HPP
Definition packet_variant.hpp:49
buffer that has string_view interface This class provides string_view interface. This class hold stri...
Definition buffer.hpp:30
MQTT PUBCOMP packet (v3.1.1)
Definition v3_1_1_pubcomp.hpp:40
static constexpr std::size_t num_of_const_buffer_sequence()
Get number of element of const_buffer_sequence.
Definition v3_1_1_pubcomp.hpp:130
basic_pubcomp_packet(packet_id_t packet_id)
constructor
Definition v3_1_1_pubcomp.hpp:48
std::size_t size() const
Get packet size.
Definition v3_1_1_pubcomp.hpp:122
packet_id_t packet_id() const
Get packet_id.
Definition v3_1_1_pubcomp.hpp:138
std::vector< as::const_buffer > const_buffer_sequence() const
Create const buffer sequence it is for boost asio APIs.
Definition v3_1_1_pubcomp.hpp:111