2023-10-05 13:59:32 +02:00
|
|
|
#ifndef ASYNC_MQTT5_SUBSCRIBE_OP_HPP
|
|
|
|
#define ASYNC_MQTT5_SUBSCRIBE_OP_HPP
|
|
|
|
|
2023-12-01 15:46:53 +01:00
|
|
|
#include <algorithm>
|
2023-12-13 15:13:07 +01:00
|
|
|
#include <cstdint>
|
2023-12-01 15:46:53 +01:00
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
#include <boost/asio/detached.hpp>
|
|
|
|
|
|
|
|
#include <async_mqtt5/error.hpp>
|
2023-11-29 11:50:07 +01:00
|
|
|
#include <async_mqtt5/types.hpp>
|
2023-10-05 13:59:32 +02:00
|
|
|
|
|
|
|
#include <async_mqtt5/detail/cancellable_handler.hpp>
|
2023-10-06 11:51:04 +02:00
|
|
|
#include <async_mqtt5/detail/control_packet.hpp>
|
|
|
|
#include <async_mqtt5/detail/internal_types.hpp>
|
2023-12-13 15:13:07 +01:00
|
|
|
#include <async_mqtt5/detail/topic_validation.hpp>
|
2023-10-05 13:59:32 +02:00
|
|
|
|
2023-12-07 09:32:34 +01:00
|
|
|
#include <async_mqtt5/impl/codecs/message_decoders.hpp>
|
|
|
|
#include <async_mqtt5/impl/codecs/message_encoders.hpp>
|
2023-10-05 13:59:32 +02:00
|
|
|
|
|
|
|
#include <async_mqtt5/impl/disconnect_op.hpp>
|
|
|
|
|
|
|
|
namespace async_mqtt5::detail {
|
|
|
|
|
|
|
|
namespace asio = boost::asio;
|
|
|
|
|
|
|
|
template <typename ClientService, typename Handler>
|
|
|
|
class subscribe_op {
|
|
|
|
using client_service = ClientService;
|
2023-12-22 09:17:45 +01:00
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
struct on_subscribe {};
|
|
|
|
struct on_suback {};
|
|
|
|
|
|
|
|
std::shared_ptr<client_service> _svc_ptr;
|
|
|
|
|
|
|
|
cancellable_handler<
|
|
|
|
Handler,
|
|
|
|
typename client_service::executor_type,
|
|
|
|
std::tuple<std::vector<reason_code>, suback_props>
|
|
|
|
> _handler;
|
|
|
|
|
|
|
|
public:
|
|
|
|
subscribe_op(
|
2023-12-07 09:32:34 +01:00
|
|
|
const std::shared_ptr<client_service>& svc_ptr,
|
|
|
|
Handler&& handler
|
2023-10-05 13:59:32 +02:00
|
|
|
) :
|
|
|
|
_svc_ptr(svc_ptr),
|
|
|
|
_handler(std::move(handler), get_executor())
|
|
|
|
{}
|
|
|
|
|
|
|
|
subscribe_op(subscribe_op&&) noexcept = default;
|
|
|
|
subscribe_op(const subscribe_op&) noexcept = delete;
|
|
|
|
|
|
|
|
using executor_type = typename client_service::executor_type;
|
|
|
|
executor_type get_executor() const noexcept {
|
|
|
|
return _svc_ptr->get_executor();
|
|
|
|
}
|
|
|
|
|
|
|
|
using allocator_type = asio::associated_allocator_t<Handler>;
|
|
|
|
allocator_type get_allocator() const noexcept {
|
|
|
|
return asio::get_associated_allocator(_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void perform(
|
|
|
|
const std::vector<subscribe_topic>& topics,
|
|
|
|
const subscribe_props& props
|
2023-12-07 12:18:28 +01:00
|
|
|
) {
|
2023-10-05 13:59:32 +02:00
|
|
|
uint16_t packet_id = _svc_ptr->allocate_pid();
|
|
|
|
if (packet_id == 0)
|
2023-12-22 09:17:45 +01:00
|
|
|
return complete_post(
|
|
|
|
client::error::pid_overrun, packet_id, topics.size()
|
|
|
|
);
|
|
|
|
|
|
|
|
auto ec = validate_subscribe(topics, props);
|
|
|
|
if (ec)
|
|
|
|
return complete_post(ec, packet_id, topics.size());
|
2023-10-05 13:59:32 +02:00
|
|
|
|
|
|
|
auto subscribe = control_packet<allocator_type>::of(
|
|
|
|
with_pid, get_allocator(),
|
|
|
|
encoders::encode_subscribe, packet_id,
|
|
|
|
topics, props
|
|
|
|
);
|
|
|
|
|
2024-01-02 16:01:56 +01:00
|
|
|
auto max_packet_size = static_cast<size_t>(
|
2024-01-04 15:31:06 +01:00
|
|
|
_svc_ptr->connack_property(prop::maximum_packet_size)
|
2024-01-02 16:01:56 +01:00
|
|
|
.value_or(default_max_send_size)
|
|
|
|
);
|
2023-12-22 09:17:45 +01:00
|
|
|
if (subscribe.size() > max_packet_size)
|
|
|
|
return complete_post(
|
|
|
|
client::error::packet_too_large, packet_id, topics.size()
|
|
|
|
);
|
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
send_subscribe(std::move(subscribe));
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_subscribe(control_packet<allocator_type> subscribe) {
|
2023-11-28 11:04:00 +01:00
|
|
|
if (_handler.empty()) // already cancelled
|
|
|
|
return _svc_ptr->free_pid(subscribe.packet_id());
|
|
|
|
|
2023-12-22 10:07:20 +01:00
|
|
|
auto wire_data = subscribe.wire_data();
|
2023-10-05 13:59:32 +02:00
|
|
|
_svc_ptr->async_send(
|
|
|
|
wire_data,
|
|
|
|
no_serial, send_flag::none,
|
|
|
|
asio::prepend(
|
|
|
|
std::move(*this), on_subscribe {}, std::move(subscribe)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(
|
|
|
|
on_subscribe, control_packet<allocator_type> packet,
|
|
|
|
error_code ec
|
|
|
|
) {
|
|
|
|
if (ec == asio::error::try_again)
|
|
|
|
return send_subscribe(std::move(packet));
|
|
|
|
|
|
|
|
auto packet_id = packet.packet_id();
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
return complete(ec, packet_id, {}, {});
|
|
|
|
|
|
|
|
_svc_ptr->async_wait_reply(
|
|
|
|
control_code_e::suback, packet_id,
|
2023-11-06 12:13:44 +01:00
|
|
|
asio::prepend(std::move(*this), on_suback {}, std::move(packet))
|
2023-10-05 13:59:32 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(
|
|
|
|
on_suback, control_packet<allocator_type> packet,
|
|
|
|
error_code ec, byte_citer first, byte_citer last
|
|
|
|
) {
|
|
|
|
if (ec == asio::error::try_again) // "resend unanswered"
|
|
|
|
return send_subscribe(std::move(packet));
|
|
|
|
|
|
|
|
uint16_t packet_id = packet.packet_id();
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
return complete(ec, packet_id, {}, {});
|
|
|
|
|
2024-01-02 11:40:53 +01:00
|
|
|
auto suback = decoders::decode_suback(
|
|
|
|
static_cast<uint32_t>(std::distance(first, last)), first
|
|
|
|
);
|
2023-10-05 13:59:32 +02:00
|
|
|
if (!suback.has_value()) {
|
|
|
|
on_malformed_packet("Malformed SUBACK: cannot decode");
|
|
|
|
return send_subscribe(std::move(packet));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& [props, reason_codes] = *suback;
|
|
|
|
|
|
|
|
complete(
|
|
|
|
ec, packet_id,
|
|
|
|
to_reason_codes(std::move(reason_codes)), std::move(props)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2023-12-22 09:17:45 +01:00
|
|
|
error_code validate_subscribe(
|
|
|
|
const std::vector<subscribe_topic>& topics, const subscribe_props& props
|
|
|
|
) const {
|
|
|
|
error_code ec;
|
|
|
|
for (const auto& topic: topics) {
|
|
|
|
ec = validate_topic(topic);
|
|
|
|
if (ec)
|
|
|
|
return ec;
|
|
|
|
}
|
2023-12-13 15:13:07 +01:00
|
|
|
|
2023-12-22 09:17:45 +01:00
|
|
|
ec = validate_props(props);
|
|
|
|
return ec;
|
2023-12-13 15:13:07 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 09:17:45 +01:00
|
|
|
error_code validate_topic(const subscribe_topic& topic) const {
|
2024-01-04 15:31:06 +01:00
|
|
|
auto wildcard_available = _svc_ptr->connack_property(
|
2023-12-22 09:17:45 +01:00
|
|
|
prop::wildcard_subscription_available
|
|
|
|
).value_or(1);
|
2024-01-04 15:31:06 +01:00
|
|
|
auto shared_available = _svc_ptr->connack_property(
|
2023-12-22 09:17:45 +01:00
|
|
|
prop::shared_subscription_available
|
|
|
|
).value_or(1);
|
|
|
|
|
2023-12-13 15:13:07 +01:00
|
|
|
std::string_view topic_filter = topic.topic_filter;
|
|
|
|
|
|
|
|
validation_result result = validation_result::valid;
|
|
|
|
if (
|
|
|
|
topic_filter.compare(0, shared_sub_id.size(), shared_sub_id) == 0
|
|
|
|
) {
|
|
|
|
if (!shared_available)
|
|
|
|
return client::error::shared_subscription_not_available;
|
|
|
|
|
|
|
|
result = validate_shared_topic_filter(topic_filter, wildcard_available);
|
|
|
|
} else
|
|
|
|
result = wildcard_available ?
|
|
|
|
validate_topic_filter(topic_filter) :
|
|
|
|
validate_topic_name(topic_filter);
|
|
|
|
|
|
|
|
if (result == validation_result::invalid)
|
|
|
|
return client::error::invalid_topic;
|
|
|
|
if (!wildcard_available && result != validation_result::valid)
|
|
|
|
return client::error::wildcard_subscription_not_available;
|
2023-11-29 11:50:07 +01:00
|
|
|
return error_code {};
|
|
|
|
}
|
|
|
|
|
2023-12-22 09:17:45 +01:00
|
|
|
error_code validate_props(const subscribe_props& props) const {
|
|
|
|
auto user_properties = props[prop::user_property];
|
|
|
|
for (const auto& user_prop: user_properties)
|
|
|
|
if (validate_mqtt_utf8(user_prop) != validation_result::valid)
|
|
|
|
return client::error::malformed_packet;
|
2023-12-13 15:13:07 +01:00
|
|
|
|
2023-12-22 09:17:45 +01:00
|
|
|
auto sub_id = props[prop::subscription_identifier];
|
|
|
|
if (!sub_id.has_value())
|
|
|
|
return error_code {};
|
2023-12-13 15:13:07 +01:00
|
|
|
|
2024-01-04 15:31:06 +01:00
|
|
|
auto sub_id_available = _svc_ptr->connack_property(
|
2023-12-22 09:17:45 +01:00
|
|
|
prop::subscription_identifier_available
|
|
|
|
).value_or(1);
|
|
|
|
|
|
|
|
if (!sub_id_available)
|
|
|
|
return client::error::subscription_identifier_not_available;
|
|
|
|
|
|
|
|
return (min_subscription_identifier <= *sub_id &&
|
|
|
|
*sub_id <= max_subscription_identifier) ?
|
|
|
|
error_code {} :
|
|
|
|
client::error::subscription_identifier_not_available;
|
2023-12-13 15:13:07 +01:00
|
|
|
}
|
|
|
|
|
2023-12-07 09:32:34 +01:00
|
|
|
static std::vector<reason_code> to_reason_codes(
|
|
|
|
std::vector<uint8_t> codes
|
|
|
|
) {
|
2023-10-05 13:59:32 +02:00
|
|
|
std::vector<reason_code> ret;
|
|
|
|
for (uint8_t code : codes) {
|
|
|
|
auto rc = to_reason_code<reason_codes::category::suback>(code);
|
|
|
|
if (rc)
|
|
|
|
ret.push_back(*rc);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_malformed_packet(const std::string& reason) {
|
2023-11-29 11:50:07 +01:00
|
|
|
auto props = disconnect_props {};
|
2023-10-05 13:59:32 +02:00
|
|
|
props[prop::reason_string] = reason;
|
|
|
|
async_disconnect(
|
|
|
|
disconnect_rc_e::malformed_packet, props, false, _svc_ptr,
|
|
|
|
asio::detached
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-22 09:17:45 +01:00
|
|
|
void complete_post(error_code ec, uint16_t packet_id, size_t num_topics) {
|
|
|
|
if (packet_id != 0)
|
|
|
|
_svc_ptr->free_pid(packet_id);
|
2023-10-05 13:59:32 +02:00
|
|
|
_handler.complete_post(
|
2023-12-07 09:32:34 +01:00
|
|
|
ec, std::vector<reason_code> { num_topics, reason_codes::empty },
|
|
|
|
suback_props {}
|
2023-10-05 13:59:32 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void complete(
|
|
|
|
error_code ec, uint16_t packet_id,
|
|
|
|
std::vector<reason_code> reason_codes, suback_props props
|
|
|
|
) {
|
2023-12-01 15:46:53 +01:00
|
|
|
if (!_svc_ptr->subscriptions_present()) {
|
|
|
|
bool has_success_rc = std::any_of(
|
|
|
|
reason_codes.cbegin(), reason_codes.cend(),
|
|
|
|
[](const reason_code& rc) { return !rc; }
|
|
|
|
);
|
|
|
|
if (has_success_rc)
|
|
|
|
_svc_ptr->subscriptions_present(true);
|
|
|
|
}
|
|
|
|
|
2023-10-05 13:59:32 +02:00
|
|
|
_svc_ptr->free_pid(packet_id);
|
|
|
|
_handler.complete(ec, std::move(reason_codes), std::move(props));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace async_mqtt5::detail
|
|
|
|
|
|
|
|
#endif // !ASYNC_MQTT5_SUBSCRIBE_OP_HPP
|