async_mqtt 4.1.0
Loading...
Searching...
No Matches
v3_1_1_pubrec.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_PUBREC_HPP)
8#define ASYNC_MQTT_PACKET_V3_1_1_PUBREC_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
38template <std::size_t PacketIdBytes>
40public:
41 using packet_id_t = typename packet_id_type<PacketIdBytes>::type;
42
48 packet_id_t packet_id
49 )
50 : all_(all_.capacity())
51 {
52 all_[0] = static_cast<char>(make_fixed_header(control_packet_type::pubrec, 0b0000));
53 all_[1] = boost::numeric_cast<char>(PacketIdBytes);
54 endian_store(packet_id, &all_[2]);
55 }
56
58 // fixed_header
59 if (buf.empty()) {
60 throw make_error(
61 errc::bad_message,
62 "v3_1_1::pubrec_packet fixed_header doesn't exist"
63 );
64 }
65 all_.push_back(buf.front());
66 buf.remove_prefix(1);
67 auto cpt_opt = get_control_packet_type_with_check(static_cast<std::uint8_t>(all_.back()));
68 if (!cpt_opt || *cpt_opt != control_packet_type::pubrec) {
69 throw make_error(
70 errc::bad_message,
71 "v3_1_1::pubrec_packet fixed_header is invalid"
72 );
73 }
74
75 // remaining_length
76 if (buf.empty()) {
77 throw make_error(
78 errc::bad_message,
79 "v3_1_1::pubrec_packet remaining_length doesn't exist"
80 );
81 }
82 all_.push_back(buf.front());
83 buf.remove_prefix(1);
84 if (static_cast<std::uint8_t>(all_.back()) != PacketIdBytes) {
85 throw make_error(
86 errc::bad_message,
87 "v3_1_1::pubrec_packet remaining_length is invalid"
88 );
89 }
90
91 // variable header
92 if (buf.size() != PacketIdBytes) {
93 throw make_error(
94 errc::bad_message,
95 "v3_1_1::pubrec_packet variable header doesn't match its length"
96 );
97 }
98 std::copy(buf.begin(), buf.end(), std::back_inserter(all_));
99 }
100
101 constexpr control_packet_type type() const {
102 return control_packet_type::pubrec;
103 }
104
110 std::vector<as::const_buffer> const_buffer_sequence() const {
111 std::vector<as::const_buffer> ret;
112
113 ret.emplace_back(as::buffer(all_.data(), all_.size()));
114 return ret;
115 }
116
121 std::size_t size() const {
122 return all_.size();
123 }
124
129 static constexpr std::size_t num_of_const_buffer_sequence() {
130 return 1; // all
131 }
132
137 packet_id_t packet_id() const {
138 return endian_load<packet_id_t>(&all_[2]);
139 }
140
141private:
143};
144
145template <std::size_t PacketIdBytes>
146inline std::ostream& operator<<(std::ostream& o, basic_pubrec_packet<PacketIdBytes> const& v) {
147 o <<
148 "v3_1_1::pubrec{" <<
149 "pid:" << v.packet_id() <<
150 "}";
151 return o;
152}
153
159
160} // namespace async_mqtt::v3_1_1
161
162#endif // ASYNC_MQTT_PACKET_V3_1_1_PUBREC_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 PUBREC packet (v3.1.1)
Definition v3_1_1_pubrec.hpp:39
packet_id_t packet_id() const
Get packet_id.
Definition v3_1_1_pubrec.hpp:137
std::size_t size() const
Get packet size.
Definition v3_1_1_pubrec.hpp:121
std::vector< as::const_buffer > const_buffer_sequence() const
Create const buffer sequence it is for boost asio APIs.
Definition v3_1_1_pubrec.hpp:110
basic_pubrec_packet(packet_id_t packet_id)
constructor
Definition v3_1_1_pubrec.hpp:47
static constexpr std::size_t num_of_const_buffer_sequence()
Get number of element of const_buffer_sequence.
Definition v3_1_1_pubrec.hpp:129