Files
TerracottaWarriors/Plugins/MqttClient/Source/DTMqtt/MqttCPP/response_options.cpp

111 lines
2.7 KiB
C++
Raw Normal View History

2025-07-14 22:24:27 +08:00
// Copyright 2023 Dexter.Wan. All Rights Reserved.
// EMail: 45141961@qq.com
#include "response_options.h"
namespace mqtt {
/////////////////////////////////////////////////////////////////////////////
response_options::response_options(int mqttVersion /*=MQTTVERSION_DEFAULT*/)
: opts_(MQTTAsync_responseOptions_initializer)
{
set_mqtt_version(mqttVersion);
}
response_options::response_options(const token_ptr& tok,
int mqttVersion /*=MQTTVERSION_DEFAULT*/)
: response_options(mqttVersion)
{
set_token(tok);
}
response_options::response_options(const response_options& other)
: opts_(other.opts_), tok_(other.tok_), props_(other.props_)
{
update_c_struct();
}
response_options& response_options::operator=(const response_options& rhs)
{
opts_ = rhs.opts_;
tok_ = rhs.tok_;
props_ = rhs.props_;
update_c_struct();
return *this;
}
void response_options::update_c_struct()
{
opts_.properties = props_.c_struct();
if (opts_.subscribeOptionsCount != 0)
opts_.subscribeOptionsList = const_cast<MQTTSubscribe_options*>(subOpts_.data());
}
void response_options::set_mqtt_version(int mqttVersion)
{
if (mqttVersion < MQTTVERSION_5) {
opts_.onSuccess = &token::on_success;
opts_.onFailure = &token::on_failure;
opts_.onSuccess5 = nullptr;
opts_.onFailure5 = nullptr;
}
else {
opts_.onSuccess5 = &token::on_success5;
opts_.onFailure5 = &token::on_failure5;
opts_.onSuccess = nullptr;
opts_.onFailure = nullptr;
}
}
void response_options::set_token(const token_ptr& tok)
{
tok_ = tok;
opts_.context = tok.get();
}
void response_options::set_subscribe_options(const subscribe_options& opts)
{
opts_.subscribeOptions = opts.opts_;
}
void response_options::set_subscribe_options(const std::vector<subscribe_options>& opts)
{
subOpts_.clear();
for (const auto& opt : opts)
subOpts_.push_back(opt.opts_);
opts_.subscribeOptionsCount = int(opts.size());
opts_.subscribeOptionsList = const_cast<MQTTSubscribe_options*>(subOpts_.data());
}
/////////////////////////////////////////////////////////////////////////////
delivery_response_options::delivery_response_options(int mqttVersion /*=MQTTVERSION_DEFAULT*/)
: opts_(MQTTAsync_responseOptions_initializer)
{
if (mqttVersion < MQTTVERSION_5) {
opts_.onSuccess = &delivery_token::on_success;
opts_.onFailure = &delivery_token::on_failure;
}
else {
opts_.onSuccess5 = &delivery_token::on_success5;
opts_.onFailure5 = &delivery_token::on_failure5;
}
}
delivery_response_options::delivery_response_options(const delivery_token_ptr& tok,
int mqttVersion /*=MQTTVERSION_DEFAULT*/)
: delivery_response_options(mqttVersion)
{
set_token(tok);
}
/////////////////////////////////////////////////////////////////////////////
// end namespace 'mqtt'
}