Loading...
Searching...
No Matches
message.h
Go to the documentation of this file.
/*******************************************************************************
* Copyright (c) 2013-2024 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
* Frank Pagliughi - MQTT v5 support (properties)
*******************************************************************************/
#ifndef __mqtt_message_h
#define __mqtt_message_h
#include <memory>
#include "MQTTAsync.h"
#include "mqtt/buffer_ref.h"
#include "mqtt/exception.h"
#include "mqtt/platform.h"
#include "mqtt/properties.h"
namespace mqtt {
class message
{
public:
static constexpr int DFLT_QOS = 0;
static constexpr bool DFLT_RETAINED = false;
private:
static constexpr MQTTAsync_message DFLT_C_STRUCT MQTTAsync_message_initializer;
PAHO_MQTTPP_EXPORT static const string EMPTY_STR;
PAHO_MQTTPP_EXPORT static const binary EMPTY_BIN;
MQTTAsync_message msg_{DFLT_C_STRUCT};
string_ref topic_;
binary_ref payload_;
properties props_;
friend class async_client;
void set_duplicate(bool dup) { msg_.dup = to_int(dup); }
public:
using ptr_t = std::shared_ptr<message>;
using const_ptr_t = std::shared_ptr<const message>;
message() {}
message(
string_ref topic, const void* payload, size_t len, int qos, bool retained,
const properties& props = properties()
);
message(string_ref topic, const void* payload, size_t len)
: message(std::move(topic), payload, len, DFLT_QOS, DFLT_RETAINED) {}
message(
string_ref topic, binary_ref payload, int qos, bool retained,
const properties& props = properties()
);
message(string_ref topic, binary_ref payload)
: message(std::move(topic), std::move(payload), DFLT_QOS, DFLT_RETAINED) {}
message(string_ref topic, const MQTTAsync_message& cmsg);
message(const message& other);
message(message&& other);
~message() {}
static ptr_t create(
string_ref topic, const void* payload, size_t len, int qos, bool retained,
const properties& props = properties()
) {
return std::make_shared<message>(
std::move(topic), payload, len, qos, retained, props
);
}
static ptr_t create(string_ref topic, const void* payload, size_t len) {
return std::make_shared<message>(
std::move(topic), payload, len, DFLT_QOS, DFLT_RETAINED
);
}
static ptr_t create(
string_ref topic, binary_ref payload, int qos, bool retained,
const properties& props = properties()
) {
return std::make_shared<message>(
std::move(topic), std::move(payload), qos, retained, props
);
}
static ptr_t create(string_ref topic, binary_ref payload) {
return std::make_shared<message>(
std::move(topic), std::move(payload), DFLT_QOS, DFLT_RETAINED
);
}
static ptr_t create(string_ref topic, const MQTTAsync_message& msg) {
return std::make_shared<message>(std::move(topic), msg);
}
message& operator=(const message& rhs);
message& operator=(message&& rhs);
#if defined(UNIT_TESTS)
const MQTTAsync_message& c_struct() const { return msg_; }
#endif
void set_topic(string_ref topic) {
topic_ = topic ? std::move(topic) : string_ref(string());
}
const string_ref& get_topic_ref() const { return topic_; }
const string& get_topic() const { return topic_ ? topic_.str() : EMPTY_STR; }
void clear_payload();
const binary_ref& get_payload_ref() const { return payload_; }
const binary& get_payload() const { return payload_ ? payload_.str() : EMPTY_BIN; }
const string& get_payload_str() const { return payload_ ? payload_.str() : EMPTY_STR; }
int get_qos() const { return msg_.qos; }
bool is_duplicate() const { return to_bool(msg_.dup); }
bool is_retained() const { return to_bool(msg_.retained); }
void set_payload(binary_ref payload);
void set_payload(const void* payload, size_t n) {
set_payload(binary_ref(static_cast<const binary_ref::value_type*>(payload), n));
}
void set_qos(int qos) {
validate_qos(qos);
msg_.qos = qos;
}
static void validate_qos(int qos) {
if (qos < 0 || qos > 2)
throw exception(MQTTASYNC_BAD_QOS, "Bad QoS");
}
void set_retained(bool retained) { msg_.retained = to_int(retained); }
const properties& get_properties() const { return props_; }
void set_properties(const properties& props) {
props_ = props;
msg_.properties = props_.c_struct();
}
void set_properties(properties&& props) {
props_ = std::move(props);
msg_.properties = props_.c_struct();
}
string to_string() const { return get_payload_str(); }
};
using message_ptr = message::ptr_t;
using const_message_ptr = message::const_ptr_t;
inline message_ptr make_message(
string_ref topic, const void* payload, size_t len, int qos, bool retained,
const properties& props = properties()
) {
return mqtt::message::create(std::move(topic), payload, len, qos, retained, props);
}
inline message_ptr make_message(string_ref topic, const void* payload, size_t len) {
return mqtt::message::create(std::move(topic), payload, len);
}
inline message_ptr make_message(
string_ref topic, binary_ref payload, int qos, bool retained,
const properties& props = properties()
) {
return mqtt::message::create(std::move(topic), std::move(payload), qos, retained, props);
}
inline message_ptr make_message(string_ref topic, binary_ref payload) {
return mqtt::message::create(std::move(topic), std::move(payload));
}
class message_ptr_builder
{
message_ptr msg_;
public:
using self = message_ptr_builder;
message_ptr_builder() : msg_{std::make_shared<message>()} {}
auto topic(string_ref topic) -> self& {
msg_->set_topic(topic);
return *this;
}
auto payload(binary_ref payload) -> self& {
msg_->set_payload(payload);
return *this;
}
auto payload(const void* payload, size_t n) -> self& {
msg_->set_payload(payload, n);
return *this;
}
auto qos(int qos) -> self& {
msg_->set_qos(qos);
return *this;
}
auto retained(bool on) -> self& {
msg_->set_retained(on);
return *this;
}
auto properties(mqtt::properties&& props) -> self& {
msg_->set_properties(std::move(props));
return *this;
}
auto properties(const mqtt::properties& props) -> self& {
msg_->set_properties(props);
return *this;
}
message_ptr finalize() { return msg_; }
};
} // namespace mqtt
#endif // __mqtt_message_h
buffer_ref.h
mqtt::async_client
Definition async_client.h:137
mqtt::buffer_ref< char >
mqtt::buffer_ref< char >::value_type
char value_type
Definition buffer_ref.h:67
mqtt::buffer_ref::str
const blob & str() const
Definition buffer_ref.h:253
mqtt::exception
Definition exception.h:48
mqtt::message_ptr_builder
Definition message.h:432
mqtt::message_ptr_builder::qos
auto qos(int qos) -> self &
Definition message.h:475
mqtt::message_ptr_builder::properties
auto properties(mqtt::properties &&props) -> self &
Definition message.h:492
mqtt::message_ptr_builder::payload
auto payload(binary_ref payload) -> self &
Definition message.h:458
mqtt::message_ptr_builder::payload
auto payload(const void *payload, size_t n) -> self &
Definition message.h:467
mqtt::message_ptr_builder::finalize
message_ptr finalize()
Definition message.h:508
mqtt::message_ptr_builder::topic
auto topic(string_ref topic) -> self &
Definition message.h:447
mqtt::message_ptr_builder::properties
auto properties(const mqtt::properties &props) -> self &
Definition message.h:500
mqtt::message_ptr_builder::message_ptr_builder
message_ptr_builder()
Definition message.h:442
mqtt::message_ptr_builder::retained
auto retained(bool on) -> self &
Definition message.h:484
mqtt::message
Definition message.h:57
mqtt::message::set_properties
void set_properties(properties &&props)
Definition message.h:357
mqtt::message::message
message(string_ref topic, binary_ref payload)
Definition message.h:143
mqtt::message::get_properties
const properties & get_properties() const
Definition message.h:344
mqtt::message::get_payload_str
const string & get_payload_str() const
Definition message.h:281
mqtt::message::message
message(string_ref topic, const void *payload, size_t len)
Definition message.h:122
mqtt::message::message
message(const message &other)
mqtt::message::create
static ptr_t create(string_ref topic, const void *payload, size_t len)
Definition message.h:190
mqtt::message::message
message()
Definition message.h:100
mqtt::message::DFLT_RETAINED
static constexpr bool DFLT_RETAINED
Definition message.h:62
mqtt::message::to_string
string to_string() const
Definition message.h:365
mqtt::message::set_retained
void set_retained(bool retained)
Definition message.h:339
mqtt::message::set_topic
void set_topic(string_ref topic)
Definition message.h:253
mqtt::message::validate_qos
static void validate_qos(int qos)
Definition message.h:330
mqtt::message::operator=
message & operator=(message &&rhs)
mqtt::message::set_payload
void set_payload(binary_ref payload)
mqtt::message::clear_payload
void clear_payload()
mqtt::message::message
message(string_ref topic, const void *payload, size_t len, int qos, bool retained, const properties &props=properties())
mqtt::message::message
message(string_ref topic, binary_ref payload, int qos, bool retained, const properties &props=properties())
mqtt::message::message
message(string_ref topic, const MQTTAsync_message &cmsg)
mqtt::message::is_duplicate
bool is_duplicate() const
Definition message.h:293
mqtt::message::DFLT_QOS
static constexpr int DFLT_QOS
Definition message.h:60
mqtt::message::message
message(message &&other)
mqtt::message::const_ptr_t
std::shared_ptr< const message > const_ptr_t
Definition message.h:94
mqtt::message::get_payload
const binary & get_payload() const
Definition message.h:277
mqtt::message::get_topic_ref
const string_ref & get_topic_ref() const
Definition message.h:260
mqtt::message::set_properties
void set_properties(const properties &props)
Definition message.h:349
mqtt::message::get_topic
const string & get_topic() const
Definition message.h:265
mqtt::message::operator=
message & operator=(const message &rhs)
mqtt::message::is_retained
bool is_retained() const
Definition message.h:300
mqtt::message::~message
~message()
Definition message.h:164
mqtt::message::get_payload_ref
const binary_ref & get_payload_ref() const
Definition message.h:273
mqtt::message::set_payload
void set_payload(const void *payload, size_t n)
Definition message.h:314
mqtt::message::create
static ptr_t create(string_ref topic, const void *payload, size_t len, int qos, bool retained, const properties &props=properties())
Definition message.h:175
mqtt::message::create
static ptr_t create(string_ref topic, binary_ref payload, int qos, bool retained, const properties &props=properties())
Definition message.h:204
mqtt::message::get_qos
int get_qos() const
Definition message.h:286
mqtt::message::ptr_t
std::shared_ptr< message > ptr_t
Definition message.h:92
mqtt::message::create
static ptr_t create(string_ref topic, binary_ref payload)
Definition message.h:218
mqtt::message::set_qos
void set_qos(int qos)
Definition message.h:321
mqtt::message::create
static ptr_t create(string_ref topic, const MQTTAsync_message &msg)
Definition message.h:228
mqtt::properties
Definition properties.h:293
mqtt::properties::c_struct
const MQTTProperties & c_struct() const
Definition properties.h:389
mqtt::topic
Definition topic.h:45
exception.h
PAHO_MQTTPP_EXPORT
#define PAHO_MQTTPP_EXPORT
Definition export.h:40
mqtt
Definition async_client.h:60
mqtt::message_ptr
message::ptr_t message_ptr
Definition message.h:369
mqtt::to_bool
bool to_bool(int n)
Definition types.h:107
mqtt::binary
std::string binary
Definition types.h:45
mqtt::binary_ref
buffer_ref< char > binary_ref
Definition buffer_ref.h:305
mqtt::const_message_ptr
message::const_ptr_t const_message_ptr
Definition message.h:372
mqtt::string_ref
buffer_ref< char > string_ref
Definition buffer_ref.h:297
mqtt::to_int
int to_int(bool b)
Definition types.h:113
mqtt::make_message
message_ptr make_message(string_ref topic, const void *payload, size_t len, int qos, bool retained, const properties &props=properties())
Definition message.h:384
platform.h
properties.h