async_mqtt 5.0.0
Loading...
Searching...
No Matches
v3_1_1_connack.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_CONNACK_HPP)
8#define ASYNC_MQTT_PACKET_V3_1_1_CONNACK_HPP
9
10#include <utility>
11#include <numeric>
12
13#include <async_mqtt/exception.hpp>
14#include <async_mqtt/buffer.hpp>
15
16#include <async_mqtt/util/move.hpp>
17#include <async_mqtt/util/static_vector.hpp>
18
19#include <async_mqtt/packet/fixed_header.hpp>
20#include <async_mqtt/packet/session_present.hpp>
21#include <async_mqtt/packet/connect_return_code.hpp>
22
23namespace async_mqtt::v3_1_1 {
24
25namespace as = boost::asio;
26
34public:
44 bool session_present,
45 connect_return_code return_code
46 )
47 : all_{
48 static_cast<char>(make_fixed_header(control_packet_type::connack, 0b0000)),
49 0b0010,
52 }
53 {
54 }
55
57 // fixed_header
58 if (buf.empty()) {
59 throw make_error(
60 errc::bad_message,
61 "v3_1_1::connack_packet fixed_header doesn't exist"
62 );
63 }
64 all_.push_back(buf.front());
65 buf.remove_prefix(1);
66 auto cpt_opt = get_control_packet_type_with_check(static_cast<std::uint8_t>(all_.back()));
67 if (!cpt_opt || *cpt_opt != control_packet_type::connack) {
68 throw make_error(
69 errc::bad_message,
70 "v3_1_1::connack_packet fixed_header is invalid"
71 );
72 }
73
74 // remaining_length
75 if (buf.empty()) {
76 throw make_error(
77 errc::bad_message,
78 "v3_1_1::connack_packet remaining_length doesn't exist"
79 );
80 }
81 all_.push_back(buf.front());
82 buf.remove_prefix(1);
83 if (static_cast<std::uint8_t>(all_.back()) != 0b00000010) {
84 throw make_error(
85 errc::bad_message,
86 "v3_1_1::connack_packet remaining_length is invalid"
87 );
88 }
89
90 // variable header
91 if (buf.size() != 2) {
92 throw make_error(
93 errc::bad_message,
94 "v3_1_1::connack_packet variable header doesn't match its length"
95 );
96 }
97 all_.push_back(buf.front());
98 buf.remove_prefix(1);
99 if ((static_cast<std::uint8_t>(all_.back()) & 0b11111110)!= 0) {
100 throw make_error(
101 errc::bad_message,
102 "v3_1_1::connack_packet connect acknowledge flags is invalid"
103 );
104 }
105 all_.push_back(buf.front());
106 if (static_cast<std::uint8_t>(all_.back()) > 5) {
107 throw make_error(
108 errc::bad_message,
109 "v3_1_1::connack_packet connect_return_code is invalid"
110 );
111 }
112 }
113
114 constexpr control_packet_type type() const {
115 return control_packet_type::connack;
116 }
117
123 std::vector<as::const_buffer> const_buffer_sequence() const {
124 std::vector<as::const_buffer> ret;
125
126 ret.emplace_back(as::buffer(all_.data(), all_.size()));
127 return ret;
128 }
129
134 std::size_t size() const {
135 return all_.size();
136 }
137
142 static constexpr std::size_t num_of_const_buffer_sequence() {
143 return 1; // all
144 }
145
150 bool session_present() const {
151 return is_session_present(all_[2]);
152 }
153
158 connect_return_code code() const {
159 return static_cast<connect_return_code>(all_[3]);
160 }
161
162private:
164};
165
166inline std::ostream& operator<<(std::ostream& o, connack_packet const& v) {
167 o <<
168 "v3_1_1::connack{" <<
169 "rc:" << v.code() << "," <<
170 "sp:" << v.session_present() <<
171 "}";
172 return o;
173}
174
175} // namespace async_mqtt::v3_1_1
176
177#endif // ASYNC_MQTT_PACKET_V3_1_1_CONNACK_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
Definition v3_1_1_connack.hpp:33
static constexpr std::size_t num_of_const_buffer_sequence()
Get number of element of const_buffer_sequence.
Definition v3_1_1_connack.hpp:142
bool session_present() const
Get session_present.
Definition v3_1_1_connack.hpp:150
std::size_t size() const
Get packet size.
Definition v3_1_1_connack.hpp:134
connect_return_code code() const
Get connect_return_code.
Definition v3_1_1_connack.hpp:158
connack_packet(bool session_present, connect_return_code return_code)
Definition v3_1_1_connack.hpp:43
std::vector< as::const_buffer > const_buffer_sequence() const
Create const buffer sequence. it is for boost asio APIs.
Definition v3_1_1_connack.hpp:123