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