paho-mqtt-cpp
MQTT C++ Client for POSIX and Windows
Loading...
Searching...
No Matches
message.h
Go to the documentation of this file.
1
7
8/*******************************************************************************
9 * Copyright (c) 2013-2023 Frank Pagliughi <fpagliughi@mindspring.com>
10 *
11 * All rights reserved. This program and the accompanying materials
12 * are made available under the terms of the Eclipse Public License v2.0
13 * and Eclipse Distribution License v1.0 which accompany this distribution.
14 *
15 * The Eclipse Public License is available at
16 * http://www.eclipse.org/legal/epl-v20.html
17 * and the Eclipse Distribution License is available at
18 * http://www.eclipse.org/org/documents/edl-v10.php.
19 *
20 * Contributors:
21 * Frank Pagliughi - initial implementation and documentation
22 * Frank Pagliughi - MQTT v5 support (properties)
23 *******************************************************************************/
24
25#ifndef __mqtt_message_h
26#define __mqtt_message_h
27
28#include "MQTTAsync.h"
29#include "mqtt/buffer_ref.h"
30#include "mqtt/properties.h"
31#include "mqtt/exception.h"
32#include "mqtt/platform.h"
33#include <memory>
34
35namespace mqtt {
36
38
56{
57public:
59 PAHO_MQTTPP_EXPORT static const int DFLT_QOS; // =0
61 PAHO_MQTTPP_EXPORT static const bool DFLT_RETAINED; // =false
62
63private:
65 PAHO_MQTTPP_EXPORT static const MQTTAsync_message DFLT_C_STRUCT;
66
68 MQTTAsync_message msg_;
70 string_ref topic_;
72 binary_ref payload_;
74 properties props_;
75
77 friend class async_client;
78
83 void set_duplicate(bool dup) { msg_.dup = to_int(dup); }
84
85public:
87 using ptr_t = std::shared_ptr<message>;
89 using const_ptr_t = std::shared_ptr<const message>;
90
106 message(string_ref topic, const void* payload, size_t len,
107 int qos, bool retained,
108 const properties& props=properties());
116 message(string_ref topic, const void* payload, size_t len)
117 : message(std::move(topic), payload, len, DFLT_QOS, DFLT_RETAINED) {}
127 message(string_ref topic, binary_ref payload, int qos, bool retained,
128 const properties& props=properties());
136 : message(std::move(topic), std::move(payload), DFLT_QOS, DFLT_RETAINED) {}
142 message(string_ref topic, const MQTTAsync_message& cmsg);
147 message(const message& other);
152 message(message&& other);
157
168 static ptr_t create(string_ref topic, const void* payload, size_t len,
169 int qos, bool retained, const properties& props=properties()) {
170 return std::make_shared<message>(std::move(topic), payload, len,
171 qos, retained, props);
172 }
180 static ptr_t create(string_ref topic, const void* payload, size_t len) {
181 return std::make_shared<message>(std::move(topic), payload, len,
183 }
193 static ptr_t create(string_ref topic, binary_ref payload, int qos, bool retained,
194 const properties& props=properties()) {
195 return std::make_shared<message>(std::move(topic), std::move(payload),
196 qos, retained, props);
197 }
205 return std::make_shared<message>(std::move(topic), std::move(payload),
207 }
213 static ptr_t create(string_ref topic, const MQTTAsync_message& msg) {
214 return std::make_shared<message>(std::move(topic), msg);
215 }
231 #if defined(UNIT_TESTS)
232 const MQTTAsync_message& c_struct() const { return msg_; }
233 #endif
239 topic_ = topic ? std::move(topic) : string_ref(string());
240 }
245 const string_ref& get_topic_ref() const { return topic_; }
250 const string& get_topic() const {
251 static const string EMPTY_STR;
252 return topic_ ? topic_.str() : EMPTY_STR;
253 }
261 const binary_ref& get_payload_ref() const { return payload_; }
265 const binary& get_payload() const {
266 static const binary EMPTY_BIN;
267 return payload_ ? payload_.str() : EMPTY_BIN;
268 }
272 const string& get_payload_str() const {
273 static const string EMPTY_STR;
274 return payload_ ? payload_.str() : EMPTY_STR;
275 }
280 int get_qos() const { return msg_.qos; }
287 bool is_duplicate() const { return to_bool(msg_.dup); }
294 bool is_retained() const { return to_bool(msg_.retained); }
302 void set_payload(binary_ref payload);
308 void set_payload(const void* payload, size_t n) {
309 set_payload(binary_ref(static_cast<const binary_ref::value_type*>(payload), n));
310 }
315 void set_qos(int qos) {
316 validate_qos(qos);
317 msg_.qos = qos;
318 }
324 static void validate_qos(int qos) {
325 if (qos < 0 || qos > 2)
326 throw exception(MQTTASYNC_BAD_QOS, "Bad QoS");
327 }
333 void set_retained(bool retained) { msg_.retained = to_int(retained); }
338 const properties& get_properties() const {
339 return props_;
340 }
345 void set_properties(const properties& props) {
346 props_ = props;
347 msg_.properties = props_.c_struct();
348 }
354 props_ = std::move(props);
355 msg_.properties = props_.c_struct();
356 }
361 string to_string() const { return get_payload_str(); }
362};
363
366
369
377inline message_ptr make_message(string_ref topic, const void* payload, size_t len) {
378 return mqtt::message::create(std::move(topic), payload, len);
379}
380
390inline message_ptr make_message(string_ref topic, const void* payload, size_t len,
391 int qos, bool retained) {
392 return mqtt::message::create(std::move(topic), payload, len, qos, retained);
393}
394
402 return mqtt::message::create(std::move(topic), std::move(payload));
403}
404
413 int qos, bool retained) {
414 return mqtt::message::create(std::move(topic), std::move(payload), qos, retained);
415}
416
418
423{
425 message_ptr msg_;
426
427public:
433 message_ptr_builder() : msg_{ std::make_shared<message>() } {}
439 msg_->set_topic(topic);
440 return *this;
441 }
450 msg_->set_payload(payload);
451 return *this;
452 }
458 auto payload(const void* payload, size_t n) -> self& {
459 msg_->set_payload(payload, n);
460 return *this;
461 }
466 auto qos(int qos) -> self& {
467 msg_->set_qos(qos);
468 return *this;
469 }
475 auto retained(bool on) -> self& {
476 msg_->set_retained(on);
477 return *this;
478 }
483 auto properties(mqtt::properties&& props) -> self& {
484 msg_->set_properties(std::move(props));
485 return *this;
486 }
491 auto properties(const mqtt::properties& props) -> self& {
492 msg_->set_properties(props);
493 return *this;
494 }
499 message_ptr finalize() { return msg_; }
500};
501
503// end namespace mqtt
504}
505
506#endif // __mqtt_message_h
507
Definition async_client.h:108
char value_type
Definition buffer_ref.h:66
const blob & str() const
Definition buffer_ref.h:246
Definition exception.h:47
Definition message.h:423
auto qos(int qos) -> self &
Definition message.h:466
auto properties(mqtt::properties &&props) -> self &
Definition message.h:483
auto payload(binary_ref payload) -> self &
Definition message.h:449
auto payload(const void *payload, size_t n) -> self &
Definition message.h:458
message_ptr finalize()
Definition message.h:499
auto topic(string_ref topic) -> self &
Definition message.h:438
auto properties(const mqtt::properties &props) -> self &
Definition message.h:491
message_ptr_builder()
Definition message.h:433
auto retained(bool on) -> self &
Definition message.h:475
Definition message.h:56
void set_properties(properties &&props)
Definition message.h:353
message(string_ref topic, binary_ref payload)
Definition message.h:135
const properties & get_properties() const
Definition message.h:338
const string & get_payload_str() const
Definition message.h:272
message(string_ref topic, const void *payload, size_t len)
Definition message.h:116
message(const message &other)
static ptr_t create(string_ref topic, const void *payload, size_t len)
Definition message.h:180
static PAHO_MQTTPP_EXPORT const int DFLT_QOS
Definition message.h:59
string to_string() const
Definition message.h:361
void set_retained(bool retained)
Definition message.h:333
void set_topic(string_ref topic)
Definition message.h:238
static void validate_qos(int qos)
Definition message.h:324
message & operator=(message &&rhs)
void set_payload(binary_ref payload)
static PAHO_MQTTPP_EXPORT const bool DFLT_RETAINED
Definition message.h:61
void clear_payload()
message(string_ref topic, const void *payload, size_t len, int qos, bool retained, const properties &props=properties())
message(string_ref topic, binary_ref payload, int qos, bool retained, const properties &props=properties())
message(string_ref topic, const MQTTAsync_message &cmsg)
bool is_duplicate() const
Definition message.h:287
message(message &&other)
std::shared_ptr< const message > const_ptr_t
Definition message.h:89
const binary & get_payload() const
Definition message.h:265
const string_ref & get_topic_ref() const
Definition message.h:245
void set_properties(const properties &props)
Definition message.h:345
const string & get_topic() const
Definition message.h:250
message & operator=(const message &rhs)
bool is_retained() const
Definition message.h:294
~message()
Definition message.h:156
const binary_ref & get_payload_ref() const
Definition message.h:261
void set_payload(const void *payload, size_t n)
Definition message.h:308
static ptr_t create(string_ref topic, const void *payload, size_t len, int qos, bool retained, const properties &props=properties())
Definition message.h:168
static ptr_t create(string_ref topic, binary_ref payload, int qos, bool retained, const properties &props=properties())
Definition message.h:193
int get_qos() const
Definition message.h:280
std::shared_ptr< message > ptr_t
Definition message.h:87
static ptr_t create(string_ref topic, binary_ref payload)
Definition message.h:204
void set_qos(int qos)
Definition message.h:315
static ptr_t create(string_ref topic, const MQTTAsync_message &msg)
Definition message.h:213
Definition properties.h:256
const MQTTProperties & c_struct() const
Definition properties.h:308
Definition topic.h:44
#define PAHO_MQTTPP_EXPORT
Definition export.h:40
Definition async_client.h:49
message::ptr_t message_ptr
Definition message.h:365
bool to_bool(int n)
Definition types.h:161
message_ptr make_message(string_ref topic, const void *payload, size_t len)
Definition message.h:377
std::string binary
Definition types.h:42
buffer_ref< char > binary_ref
Definition buffer_ref.h:298
message::const_ptr_t const_message_ptr
Definition message.h:368
buffer_ref< char > string_ref
Definition buffer_ref.h:290
int to_int(bool b)
Definition types.h:167