async_mqtt 5.0.0
Loading...
Searching...
No Matches
v3_1_1_pingresp.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_PINGRESP_HPP)
8#define ASYNC_MQTT_PACKET_V3_1_1_PINGRESP_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
41public:
42
47 : all_(all_.capacity())
48 {
49 all_[0] = static_cast<char>(make_fixed_header(control_packet_type::pingresp, 0b0000));
50 all_[1] = char(0);
51 }
52
54 // fixed_header
55 if (buf.empty()) {
56 throw make_error(
57 errc::bad_message,
58 "v3_1_1::pingresp_packet fixed_header doesn't exist"
59 );
60 }
61 all_.push_back(buf.front());
62 buf.remove_prefix(1);
63 auto cpt_opt = get_control_packet_type_with_check(static_cast<std::uint8_t>(all_.back()));
64 if (!cpt_opt || *cpt_opt != control_packet_type::pingresp) {
65 throw make_error(
66 errc::bad_message,
67 "v3_1_1::pingresp_packet fixed_header is invalid"
68 );
69 }
70
71 // remaining_length
72 if (buf.empty()) {
73 throw make_error(
74 errc::bad_message,
75 "v3_1_1::pingresp_packet remaining_length doesn't exist"
76 );
77 }
78 all_.push_back(buf.front());
79
80 if (static_cast<std::uint8_t>(all_.back()) != 0) {
81 throw make_error(
82 errc::bad_message,
83 "v3_1_1::pingresp_packet remaining_length is invalid"
84 );
85 }
86 }
87
88 constexpr control_packet_type type() const {
89 return control_packet_type::pingresp;
90 }
91
97 std::vector<as::const_buffer> const_buffer_sequence() const {
98 std::vector<as::const_buffer> ret;
99
100 ret.emplace_back(as::buffer(all_.data(), all_.size()));
101 return ret;
102 }
103
108 std::size_t size() const {
109 return all_.size();
110 }
111
116 static constexpr std::size_t num_of_const_buffer_sequence() {
117 return 1; // all
118 }
119
120private:
122};
123
124inline std::ostream& operator<<(std::ostream& o, pingresp_packet const& /*v*/) {
125 o <<
126 "v3_1_1::pingresp{}";
127 return o;
128}
129
130} // namespace async_mqtt::v3_1_1
131
132#endif // ASYNC_MQTT_PACKET_V3_1_1_PINGRESP_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 PINGRESP packet (v3.1.1)
Definition v3_1_1_pingresp.hpp:40
pingresp_packet()
constructor
Definition v3_1_1_pingresp.hpp:46
static constexpr std::size_t num_of_const_buffer_sequence()
Get number of element of const_buffer_sequence.
Definition v3_1_1_pingresp.hpp:116
std::size_t size() const
Get packet size.
Definition v3_1_1_pingresp.hpp:108
std::vector< as::const_buffer > const_buffer_sequence() const
Create const buffer sequence it is for boost asio APIs.
Definition v3_1_1_pingresp.hpp:97