24 #ifndef __mqtt_message_h
25 #define __mqtt_message_h
27 #include "MQTTAsync.h"
62 static const MQTTAsync_message DFLT_C_STRUCT;
65 static const string EMPTY_STR;
67 static const binary EMPTY_BIN;
70 MQTTAsync_message msg_;
78 friend class message_test;
84 void set_duplicate(
bool dup) { msg_.dup = to_int(dup); }
88 using ptr_t = std::shared_ptr<message>;
107 int qos,
bool retained);
165 int qos,
bool retained) {
166 return std::make_shared<message>(std::move(topic), payload, len,
177 return std::make_shared<message>(std::move(topic), payload, len,
189 return std::make_shared<message>(std::move(topic), std::move(payload),
199 return std::make_shared<message>(std::move(topic), std::move(payload),
208 return std::make_shared<message>(std::move(topic), msg);
227 topic_ = topic_ ? std::move(topic) :
string_ref(
string());
239 return topic_ ? topic_.
str() : EMPTY_STR;
253 return payload_ ? payload_.
str() : EMPTY_BIN;
259 return payload_ ? payload_.
str() : EMPTY_STR;
321 if (qos < 0 || qos > 2)
322 throw exception(MQTTASYNC_BAD_QOS,
"Bad QoS");
339 inline message_ptr make_message(string_ref topic,
const void* payload,
size_t len) {
352 inline message_ptr make_message(string_ref topic,
const void* payload,
size_t len,
353 int qos,
bool retained) {
363 inline message_ptr make_message(string_ref topic, binary_ref payload) {
374 inline message_ptr make_message(string_ref topic, binary_ref payload,
375 int qos,
bool retained) {
383 #endif // __mqtt_message_h
const string & get_topic() const
Gets the topic for the message.
Definition: message.h:238
void set_payload(const void *payload, size_t n)
Sets the payload of this message to be the specified byte array.
Definition: message.h:293
void set_topic(string_ref topic)
Sets the topic string.
Definition: message.h:226
Lightweight client for talking to an MQTT server using non-blocking methods that allow an operation t...
Definition: async_client.h:60
void set_qos(int qos)
Sets the quality of service for this message.
Definition: message.h:300
static ptr_t create(string_ref topic, binary_ref payload, int qos, bool retained)
Constructs a message from a byte buffer.
Definition: message.h:188
void set_payload(binary_ref payload)
Sets the payload of this message to be the specified buffer.
bool is_duplicate() const
Returns whether or not this message might be a duplicate of one which has already been received...
Definition: message.h:272
string to_string() const
Returns a string representation of this messages payload.
Definition: message.h:314
static ptr_t create(string_ref topic, const MQTTAsync_message &msg)
Constructs a message as a copy of the C message struct.
Definition: message.h:207
const blob & str() const
Gets the data buffer as a string.
Definition: buffer_ref.h:246
message(string_ref topic, const void *payload, size_t len)
Constructs a message with the specified array as a payload, and all other values set to defaults...
Definition: message.h:115
Represents a topic destination, used for publish/subscribe messaging.
Definition: topic.h:42
int get_qos() const
Returns the quality of service for this message.
Definition: message.h:265
Declaration of MQTT exception class.
void set_retained(bool retained)
Whether or not the publish message should be retained by the broker.
Definition: message.h:309
std::shared_ptr< message > ptr_t
Smart/shared pointer to this class.
Definition: message.h:88
message & operator=(const message &rhs)
Copies another message to this one.
Base mqtt::exception.
Definition: exception.h:42
const string & get_payload_str() const
Gets the payload as a string.
Definition: message.h:258
message(string_ref topic, binary_ref payload)
Constructs a message from a byte buffer.
Definition: message.h:132
const binary_ref & get_payload_ref() const
Gets the payload reference.
Definition: message.h:248
An MQTT message holds everything required for an MQTT PUBLISH message.
Definition: message.h:52
const string_ref & get_topic_ref() const
Gets the topic reference for the message.
Definition: message.h:233
Buffer reference type for the Paho MQTT C++ library.
static constexpr bool DFLT_RETAINED
The default retained flag.
Definition: message.h:58
~message()
Destroys a message and frees all associated resources.
Definition: message.h:153
static ptr_t create(string_ref topic, const void *payload, size_t len)
Constructs a message with the specified array as a payload, and all other values set to defaults...
Definition: message.h:176
bool is_retained() const
Returns whether or not this message should be/was retained by the server.
Definition: message.h:279
static void validate_qos(int qos)
Determines if the QOS value is a valid one.
Definition: message.h:320
const binary & get_payload() const
Gets the payload.
Definition: message.h:252
static ptr_t create(string_ref topic, binary_ref payload)
Constructs a message from a byte buffer.
Definition: message.h:198
message()
Constructs a message with an empty payload, and all other values set to defaults. ...
static constexpr int DFLT_QOS
The default QoS for a message.
Definition: message.h:56
static ptr_t create(string_ref topic, const void *payload, size_t len, int qos, bool retained)
Constructs a message with the specified array as a payload, and all other values set to defaults...
Definition: message.h:164
std::shared_ptr< const message > const_ptr_t
Smart/shared pointer to this class.
Definition: message.h:90
void clear_payload()
Clears the payload, resetting it to be empty.