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