| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- //
- // Copyright (c) 2023-2025 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
- //
- // Distributed under the Boost Software License, Version 1.0.
- // (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef BOOST_MQTT5_PUBLISH_SEND_OP_HPP
- #define BOOST_MQTT5_PUBLISH_SEND_OP_HPP
- #include <boost/mqtt5/error.hpp>
- #include <boost/mqtt5/reason_codes.hpp>
- #include <boost/mqtt5/types.hpp>
- #include <boost/mqtt5/detail/cancellable_handler.hpp>
- #include <boost/mqtt5/detail/control_packet.hpp>
- #include <boost/mqtt5/detail/internal_types.hpp>
- #include <boost/mqtt5/detail/topic_validation.hpp>
- #include <boost/mqtt5/detail/utf8_mqtt.hpp>
- #include <boost/mqtt5/impl/codecs/message_decoders.hpp>
- #include <boost/mqtt5/impl/codecs/message_encoders.hpp>
- #include <boost/mqtt5/impl/disconnect_op.hpp>
- #include <boost/asio/associated_allocator.hpp>
- #include <boost/asio/associated_executor.hpp>
- #include <boost/asio/cancellation_type.hpp>
- #include <boost/asio/detached.hpp>
- #include <boost/asio/error.hpp>
- #include <boost/asio/prepend.hpp>
- #include <cstdint>
- #include <memory>
- #include <string>
- #include <type_traits>
- namespace boost::mqtt5::detail {
- namespace asio = boost::asio;
- template <qos_e qos_type>
- using on_publish_signature = std::conditional_t<
- qos_type == qos_e::at_most_once,
- void (error_code),
- std::conditional_t<
- qos_type == qos_e::at_least_once,
- void (error_code, reason_code, puback_props),
- void (error_code, reason_code, pubcomp_props)
- >
- >;
- template <qos_e qos_type>
- using on_publish_props_type = std::conditional_t<
- qos_type == qos_e::at_most_once,
- void,
- std::conditional_t<
- qos_type == qos_e::at_least_once,
- puback_props,
- pubcomp_props
- >
- >;
- template <typename ClientService, typename Handler, qos_e qos_type>
- class publish_send_op {
- using client_service = ClientService;
- struct on_publish {};
- struct on_puback {};
- struct on_pubrec {};
- struct on_pubrel {};
- struct on_pubcomp {};
- std::shared_ptr<client_service> _svc_ptr;
- using handler_type = cancellable_handler<
- Handler,
- typename client_service::executor_type
- >;
- handler_type _handler;
- serial_num_t _serial_num;
- public:
- publish_send_op(
- std::shared_ptr<client_service> svc_ptr,
- Handler&& handler
- ) :
- _svc_ptr(std::move(svc_ptr)),
- _handler(std::move(handler), _svc_ptr->get_executor())
- {
- auto slot = asio::get_associated_cancellation_slot(_handler);
- if (slot.is_connected())
- slot.assign([&svc = *_svc_ptr](asio::cancellation_type_t) {
- svc.cancel();
- });
- }
- publish_send_op(publish_send_op&&) = default;
- publish_send_op(const publish_send_op&) = delete;
- publish_send_op& operator=(publish_send_op&&) = default;
- publish_send_op& operator=(const publish_send_op&) = delete;
- using allocator_type = asio::associated_allocator_t<handler_type>;
- allocator_type get_allocator() const noexcept {
- return asio::get_associated_allocator(_handler);
- }
- using executor_type = typename client_service::executor_type;
- executor_type get_executor() const noexcept {
- return _svc_ptr->get_executor();
- }
- void perform(
- std::string topic, std::string payload,
- retain_e retain, const publish_props& props
- ) {
- uint16_t packet_id = 0;
- if constexpr (qos_type != qos_e::at_most_once) {
- packet_id = _svc_ptr->allocate_pid();
- if (packet_id == 0)
- return complete_immediate(client::error::pid_overrun, packet_id);
- }
- auto ec = validate_publish(topic, payload, retain, props);
- if (ec)
- return complete_immediate(ec, packet_id);
- _serial_num = _svc_ptr->next_serial_num();
- auto publish = control_packet<allocator_type>::of(
- with_pid, get_allocator(),
- encoders::encode_publish, packet_id,
- std::move(topic), std::move(payload),
- qos_type, retain, dup_e::no, props
- );
- auto max_packet_size = _svc_ptr->connack_property(prop::maximum_packet_size)
- .value_or(default_max_send_size);
- if (publish.size() > max_packet_size)
- return complete_immediate(client::error::packet_too_large, packet_id);
- send_publish(std::move(publish));
- }
- void send_publish(control_packet<allocator_type> publish) {
- auto wire_data = publish.wire_data();
- _svc_ptr->async_send(
- wire_data,
- _serial_num,
- send_flag::throttled * (qos_type != qos_e::at_most_once),
- asio::prepend(std::move(*this), on_publish {}, std::move(publish))
- );
- }
- void resend_publish(control_packet<allocator_type> publish) {
- if (_handler.cancelled() != asio::cancellation_type_t::none)
- return complete(
- asio::error::operation_aborted, publish.packet_id()
- );
- send_publish(std::move(publish));
- }
- void operator()(
- on_publish, control_packet<allocator_type> publish,
- error_code ec
- ) {
- if (ec == asio::error::try_again)
- return resend_publish(std::move(publish));
- if constexpr (qos_type == qos_e::at_most_once)
- return complete(ec);
- else {
- auto packet_id = publish.packet_id();
- if (ec)
- return complete(ec, packet_id);
- if constexpr (qos_type == qos_e::at_least_once)
- _svc_ptr->async_wait_reply(
- control_code_e::puback, packet_id,
- asio::prepend(
- std::move(*this), on_puback {}, std::move(publish)
- )
- );
- else if constexpr (qos_type == qos_e::exactly_once)
- _svc_ptr->async_wait_reply(
- control_code_e::pubrec, packet_id,
- asio::prepend(
- std::move(*this), on_pubrec {}, std::move(publish)
- )
- );
- }
- }
- template <
- qos_e q = qos_type,
- std::enable_if_t<q == qos_e::at_least_once, bool> = true
- >
- void operator()(
- on_puback, control_packet<allocator_type> publish,
- error_code ec, byte_citer first, byte_citer last
- ) {
- if (ec == asio::error::try_again) // "resend unanswered"
- return resend_publish(std::move(publish.set_dup()));
- uint16_t packet_id = publish.packet_id();
- if (ec)
- return complete(ec, packet_id);
- auto puback = decoders::decode_puback(
- static_cast<uint32_t>(std::distance(first, last)), first
- );
- if (!puback.has_value()) {
- on_malformed_packet("Malformed PUBACK: cannot decode");
- return resend_publish(std::move(publish.set_dup()));
- }
- auto& [reason_code, props] = *puback;
- auto rc = to_reason_code<reason_codes::category::puback>(reason_code);
- if (!rc) {
- on_malformed_packet("Malformed PUBACK: invalid Reason Code");
- return resend_publish(std::move(publish.set_dup()));
- }
- complete(ec, packet_id, *rc, std::move(props));
- }
- template <
- qos_e q = qos_type,
- std::enable_if_t<q == qos_e::exactly_once, bool> = true
- >
- void operator()(
- on_pubrec, control_packet<allocator_type> publish,
- error_code ec, byte_citer first, byte_citer last
- ) {
- if (ec == asio::error::try_again) // "resend unanswered"
- return resend_publish(std::move(publish.set_dup()));
- uint16_t packet_id = publish.packet_id();
- if (ec)
- return complete(ec, packet_id);
- auto pubrec = decoders::decode_pubrec(
- static_cast<uint32_t>(std::distance(first, last)), first
- );
- if (!pubrec.has_value()) {
- on_malformed_packet("Malformed PUBREC: cannot decode");
- return resend_publish(std::move(publish.set_dup()));
- }
- auto& [reason_code, props] = *pubrec;
- auto rc = to_reason_code<reason_codes::category::pubrec>(reason_code);
- if (!rc) {
- on_malformed_packet("Malformed PUBREC: invalid Reason Code");
- return resend_publish(std::move(publish.set_dup()));
- }
- if (*rc)
- return complete(ec, packet_id, *rc);
- auto pubrel = control_packet<allocator_type>::of(
- with_pid, get_allocator(),
- encoders::encode_pubrel, packet_id,
- 0, pubrel_props {}
- );
- send_pubrel(std::move(pubrel), false);
- }
- void send_pubrel(control_packet<allocator_type> pubrel, bool throttled) {
- auto wire_data = pubrel.wire_data();
- _svc_ptr->async_send(
- wire_data,
- _serial_num,
- (send_flag::throttled * throttled) | send_flag::prioritized,
- asio::prepend(std::move(*this), on_pubrel {}, std::move(pubrel))
- );
- }
- template <
- qos_e q = qos_type,
- std::enable_if_t<q == qos_e::exactly_once, bool> = true
- >
- void operator()(
- on_pubrel, control_packet<allocator_type> pubrel, error_code ec
- ) {
- if (ec == asio::error::try_again)
- return send_pubrel(std::move(pubrel), true);
- uint16_t packet_id = pubrel.packet_id();
- if (ec)
- return complete(ec, packet_id);
- _svc_ptr->async_wait_reply(
- control_code_e::pubcomp, packet_id,
- asio::prepend(std::move(*this), on_pubcomp {}, std::move(pubrel))
- );
- }
- template <
- qos_e q = qos_type,
- std::enable_if_t<q == qos_e::exactly_once, bool> = true
- >
- void operator()(
- on_pubcomp, control_packet<allocator_type> pubrel,
- error_code ec,
- byte_citer first, byte_citer last
- ) {
- if (ec == asio::error::try_again) // "resend unanswered"
- return send_pubrel(std::move(pubrel), true);
- uint16_t packet_id = pubrel.packet_id();
- if (ec)
- return complete(ec, packet_id);
- auto pubcomp = decoders::decode_pubcomp(
- static_cast<uint32_t>(std::distance(first, last)), first
- );
- if (!pubcomp.has_value()) {
- on_malformed_packet("Malformed PUBCOMP: cannot decode");
- return send_pubrel(std::move(pubrel), true);
- }
- auto& [reason_code, props] = *pubcomp;
- auto rc = to_reason_code<reason_codes::category::pubcomp>(reason_code);
- if (!rc) {
- on_malformed_packet("Malformed PUBCOMP: invalid Reason Code");
- return send_pubrel(std::move(pubrel), true);
- }
- return complete(ec, pubrel.packet_id(), *rc);
- }
- private:
- error_code validate_publish(
- const std::string& topic, const std::string& payload,
- retain_e retain, const publish_props& props
- ) const {
- constexpr uint8_t default_retain_available = 1;
- constexpr uint8_t default_maximum_qos = 2;
- constexpr uint8_t default_payload_format_ind = 0;
- auto topic_name_valid = props[prop::topic_alias].has_value() ?
- validate_topic_alias_name(topic) == validation_result::valid :
- validate_topic_name(topic) == validation_result::valid
- ;
- if (!topic_name_valid)
- return client::error::invalid_topic;
- auto max_qos = _svc_ptr->connack_property(prop::maximum_qos)
- .value_or(default_maximum_qos);
- auto retain_available = _svc_ptr->connack_property(prop::retain_available)
- .value_or(default_retain_available);
- if (uint8_t(qos_type) > max_qos)
- return client::error::qos_not_supported;
- if (retain_available == 0 && retain == retain_e::yes)
- return client::error::retain_not_available;
- auto payload_format_ind = props[prop::payload_format_indicator]
- .value_or(default_payload_format_ind);
- if (
- payload_format_ind == 1 &&
- validate_mqtt_utf8(payload) != validation_result::valid
- )
- return client::error::malformed_packet;
- return validate_props(props);
- }
- error_code validate_props(const publish_props& props) const {
- constexpr uint16_t default_topic_alias_max = 0;
- const auto& topic_alias = props[prop::topic_alias];
- if (topic_alias) {
- auto topic_alias_max = _svc_ptr->connack_property(prop::topic_alias_maximum)
- .value_or(default_topic_alias_max);
- if (topic_alias_max == 0 || *topic_alias > topic_alias_max)
- return client::error::topic_alias_maximum_reached;
- if (*topic_alias == 0 )
- return client::error::malformed_packet;
- }
- const auto& response_topic = props[prop::response_topic];
- if (
- response_topic &&
- validate_topic_name(*response_topic) != validation_result::valid
- )
- return client::error::malformed_packet;
- const auto& user_properties = props[prop::user_property];
- for (const auto& user_property: user_properties)
- if (!is_valid_string_pair(user_property))
- return client::error::malformed_packet;
- if (!props[prop::subscription_identifier].empty())
- return client::error::malformed_packet;
- const auto& content_type = props[prop::content_type];
- if (
- content_type &&
- validate_mqtt_utf8(*content_type) != validation_result::valid
- )
- return client::error::malformed_packet;
- return error_code {};
- }
- void on_malformed_packet(const std::string& reason) {
- auto props = disconnect_props {};
- props[prop::reason_string] = reason;
- async_disconnect(
- disconnect_rc_e::malformed_packet, props, _svc_ptr,
- asio::detached
- );
- }
- template <
- qos_e q = qos_type,
- std::enable_if_t<q == qos_e::at_most_once, bool> = true
- >
- void complete(error_code ec, uint16_t = 0) {
- _handler.complete(ec);
- }
- template <
- qos_e q = qos_type,
- std::enable_if_t<q == qos_e::at_most_once, bool> = true
- >
- void complete_immediate(error_code ec, uint16_t) {
- _handler.complete_immediate(ec);
- }
- template <
- typename Props = on_publish_props_type<qos_type>,
- std::enable_if_t<
- std::is_same_v<Props, puback_props> ||
- std::is_same_v<Props, pubcomp_props>,
- bool
- > = true
- >
- void complete(
- error_code ec, uint16_t packet_id,
- reason_code rc = reason_codes::empty, Props&& props = Props {}
- ) {
- _svc_ptr->free_pid(packet_id, true);
- _handler.complete(ec, rc, std::forward<Props>(props));
- }
- template <
- typename Props = on_publish_props_type<qos_type>,
- std::enable_if_t<
- std::is_same_v<Props, puback_props> ||
- std::is_same_v<Props, pubcomp_props>,
- bool
- > = true
- >
- void complete_immediate(error_code ec, uint16_t packet_id) {
- if (packet_id != 0)
- _svc_ptr->free_pid(packet_id, false);
- _handler.complete_immediate(ec, reason_codes::empty, Props {});
- }
- };
- template <typename ClientService, qos_e qos_type>
- class initiate_async_publish {
- std::shared_ptr<ClientService> _svc_ptr;
- public:
- explicit initiate_async_publish(std::shared_ptr<ClientService> svc_ptr) :
- _svc_ptr(std::move(svc_ptr))
- {}
- using executor_type = typename ClientService::executor_type;
- executor_type get_executor() const noexcept {
- return _svc_ptr->get_executor();
- }
- template <typename Handler>
- void operator()(
- Handler&& handler,
- std::string topic, std::string payload,
- retain_e retain, const publish_props& props
- ) {
- detail::publish_send_op<ClientService, Handler, qos_type> {
- _svc_ptr, std::move(handler)
- }.perform(
- std::move(topic), std::move(payload), retain, props
- );
- }
- };
- } // end namespace boost::mqtt5::detail
- #endif // !BOOST_MQTT5_PUBLISH_SEND_OP_HPP
|