2023-10-05 13:59:32 +02:00
|
|
|
#ifndef ASYNC_MQTT5_READ_MESSAGE_OP_HPP
|
|
|
|
#define ASYNC_MQTT5_READ_MESSAGE_OP_HPP
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
#include <boost/asio/prepend.hpp>
|
|
|
|
#include <boost/asio/recycling_allocator.hpp>
|
|
|
|
|
|
|
|
#include <async_mqtt5/types.hpp>
|
2023-10-06 11:51:04 +02:00
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
#include <async_mqtt5/detail/control_packet.hpp>
|
2023-10-06 11:51:04 +02:00
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
#include <async_mqtt5/impl/disconnect_op.hpp>
|
2023-10-06 11:51:04 +02:00
|
|
|
#include <async_mqtt5/impl/publish_rec_op.hpp>
|
2023-11-08 08:49:28 +01:00
|
|
|
#include <async_mqtt5/impl/re_auth_op.hpp>
|
2023-10-06 11:51:04 +02:00
|
|
|
|
2023-12-07 09:32:34 +01:00
|
|
|
#include <async_mqtt5/impl/codecs/message_decoders.hpp>
|
2023-10-05 13:59:32 +02:00
|
|
|
|
|
|
|
namespace async_mqtt5::detail {
|
|
|
|
|
|
|
|
namespace asio = boost::asio;
|
|
|
|
|
|
|
|
template <typename ClientService>
|
|
|
|
class read_message_op {
|
|
|
|
using client_service = ClientService;
|
|
|
|
struct on_message {};
|
|
|
|
struct on_disconnect {};
|
|
|
|
|
|
|
|
std::shared_ptr<client_service> _svc_ptr;
|
|
|
|
public:
|
|
|
|
read_message_op(
|
|
|
|
const std::shared_ptr<client_service>& svc_ptr
|
|
|
|
) :
|
|
|
|
_svc_ptr(svc_ptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
read_message_op(read_message_op&&) noexcept = default;
|
|
|
|
read_message_op(const read_message_op&) = delete;
|
|
|
|
|
|
|
|
using executor_type = typename client_service::executor_type;
|
|
|
|
executor_type get_executor() const noexcept {
|
|
|
|
return _svc_ptr->get_executor();
|
|
|
|
}
|
|
|
|
|
|
|
|
using allocator_type = asio::recycling_allocator<void>;
|
|
|
|
allocator_type get_allocator() const noexcept {
|
|
|
|
return allocator_type {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void perform() {
|
|
|
|
_svc_ptr->async_assemble(
|
|
|
|
std::chrono::seconds(20),
|
|
|
|
asio::prepend(std::move(*this), on_message {})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(
|
|
|
|
on_message, error_code ec,
|
2023-11-03 08:38:28 +01:00
|
|
|
uint8_t control_code,
|
2023-10-05 13:59:32 +02:00
|
|
|
byte_citer first, byte_citer last
|
|
|
|
) {
|
|
|
|
if (ec == client::error::malformed_packet)
|
|
|
|
return on_malformed_packet(
|
|
|
|
"Malformed Packet received from the Server"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
ec == asio::error::operation_aborted ||
|
|
|
|
ec == asio::error::no_recovery
|
|
|
|
)
|
|
|
|
return;
|
|
|
|
|
2023-11-03 08:38:28 +01:00
|
|
|
dispatch(control_code, first, last);
|
2023-10-05 13:59:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(on_disconnect, error_code ec) {
|
2023-11-03 08:38:28 +01:00
|
|
|
if (!ec)
|
2023-10-05 13:59:32 +02:00
|
|
|
perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void dispatch(
|
2023-11-03 08:38:28 +01:00
|
|
|
uint8_t control_byte,
|
2023-10-05 13:59:32 +02:00
|
|
|
byte_citer first, byte_citer last
|
|
|
|
) {
|
|
|
|
auto code = control_code_e(control_byte & 0b11110000);
|
2023-12-07 09:32:34 +01:00
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
switch (code) {
|
2023-12-06 08:25:12 +01:00
|
|
|
case control_code_e::publish: {
|
2023-10-05 13:59:32 +02:00
|
|
|
auto msg = decoders::decode_publish(
|
2024-01-02 11:40:53 +01:00
|
|
|
control_byte, static_cast<uint32_t>(std::distance(first, last)), first
|
2023-10-05 13:59:32 +02:00
|
|
|
);
|
|
|
|
if (!msg.has_value())
|
|
|
|
return on_malformed_packet(
|
|
|
|
"Malformed PUBLISH received: cannot decode"
|
|
|
|
);
|
|
|
|
|
|
|
|
publish_rec_op { _svc_ptr }.perform(std::move(*msg));
|
|
|
|
}
|
|
|
|
break;
|
2023-12-06 08:25:12 +01:00
|
|
|
case control_code_e::disconnect: {
|
2023-10-05 13:59:32 +02:00
|
|
|
_svc_ptr->close_stream();
|
|
|
|
_svc_ptr->open_stream();
|
|
|
|
}
|
|
|
|
break;
|
2023-12-06 08:25:12 +01:00
|
|
|
case control_code_e::auth: {
|
2023-11-08 08:49:28 +01:00
|
|
|
auto rv = decoders::decode_auth(
|
2024-01-02 11:40:53 +01:00
|
|
|
static_cast<uint32_t>(std::distance(first, last)), first
|
2023-11-08 08:49:28 +01:00
|
|
|
);
|
|
|
|
if (!rv.has_value())
|
|
|
|
return on_malformed_packet(
|
|
|
|
"Malformed AUTH received: cannot decode"
|
|
|
|
);
|
|
|
|
|
|
|
|
re_auth_op { _svc_ptr }.perform(std::move(*rv));
|
2023-10-05 13:59:32 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-10-06 11:51:04 +02:00
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_malformed_packet(const std::string& reason) {
|
|
|
|
auto props = disconnect_props {};
|
|
|
|
props[prop::reason_string] = reason;
|
|
|
|
auto svc_ptr = _svc_ptr; // copy before this is moved
|
2023-12-07 09:32:34 +01:00
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
async_disconnect(
|
|
|
|
disconnect_rc_e::malformed_packet, props, false, svc_ptr,
|
|
|
|
asio::prepend(std::move(*this), on_disconnect {})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace async_mqtt5::detail
|
|
|
|
|
|
|
|
#endif // !ASYNC_MQTT5_READ_MESSAGE_OP_HPP
|