PahoMqttCpp
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-2024 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 <memory>
29
30#include "MQTTAsync.h"
31#include "mqtt/buffer_ref.h"
32#include "mqtt/exception.h"
33#include "mqtt/platform.h"
34#include "mqtt/properties.h"
35
36namespace mqtt {
37
39
57{
58public:
60 static constexpr int DFLT_QOS = 0;
62 static constexpr bool DFLT_RETAINED = false;
63
64private:
66 static constexpr MQTTAsync_message DFLT_C_STRUCT MQTTAsync_message_initializer;
68 static const string EMPTY_STR;
70 static const binary EMPTY_BIN;
71
73 MQTTAsync_message msg_{DFLT_C_STRUCT};
75 string_ref topic_;
77 binary_ref payload_;
79 properties props_;
80
82 friend class async_client;
83
88 void set_duplicate(bool dup) { msg_.dup = to_int(dup); }
89
90public:
92 using ptr_t = std::shared_ptr<message>;
94 using const_ptr_t = std::shared_ptr<const message>;
95
112 string_ref topic, const void* payload, size_t len, int qos, bool retained,
113 const properties& props = properties()
114 );
122 message(string_ref topic, const void* payload, size_t len)
123 : message(std::move(topic), payload, len, DFLT_QOS, DFLT_RETAINED) {}
134 string_ref topic, binary_ref payload, int qos, bool retained,
135 const properties& props = properties()
136 );
144 : message(std::move(topic), std::move(payload), DFLT_QOS, DFLT_RETAINED) {}
150 message(string_ref topic, const MQTTAsync_message& cmsg);
155 message(const message& other);
160 message(message&& other);
165
175 static ptr_t create(
176 string_ref topic, const void* payload, size_t len, int qos, bool retained,
177 const properties& props = properties()
178 ) {
179 return std::make_shared<message>(
180 std::move(topic), payload, len, qos, retained, props
181 );
182 }
190 static ptr_t create(string_ref topic, const void* payload, size_t len) {
191 return std::make_shared<message>(
192 std::move(topic), payload, len, DFLT_QOS, DFLT_RETAINED
193 );
194 }
204 static ptr_t create(
205 string_ref topic, binary_ref payload, int qos, bool retained,
206 const properties& props = properties()
207 ) {
208 return std::make_shared<message>(
209 std::move(topic), std::move(payload), qos, retained, props
210 );
211 }
219 return std::make_shared<message>(
220 std::move(topic), std::move(payload), DFLT_QOS, DFLT_RETAINED
221 );
222 }
228 static ptr_t create(string_ref topic, const MQTTAsync_message& msg) {
229 return std::make_shared<message>(std::move(topic), msg);
230 }
246#if defined(UNIT_TESTS)
247 const MQTTAsync_message& c_struct() const { return msg_; }
248#endif
254 topic_ = topic ? std::move(topic) : string_ref(string());
255 }
260 const string_ref& get_topic_ref() const { return topic_; }
265 const string& get_topic() const {
266 return topic_ ? topic_.str() : EMPTY_STR;
267 }
275 const binary_ref& get_payload_ref() const { return payload_; }
279 const binary& get_payload() const {
280 return payload_ ? payload_.str() : EMPTY_BIN;
281 }
285 const string& get_payload_str() const {
286 return payload_ ? payload_.str() : EMPTY_STR;
287 }
292 int get_qos() const { return msg_.qos; }
299 bool is_duplicate() const { return to_bool(msg_.dup); }
306 bool is_retained() const { return to_bool(msg_.retained); }
314 void set_payload(binary_ref payload);
320 void set_payload(const void* payload, size_t n) {
321 set_payload(binary_ref(static_cast<const binary_ref::value_type*>(payload), n));
322 }
327 void set_qos(int qos) {
328 validate_qos(qos);
329 msg_.qos = qos;
330 }
336 static void validate_qos(int qos) {
337 if (qos < 0 || qos > 2)
338 throw exception(MQTTASYNC_BAD_QOS, "Bad QoS");
339 }
345 void set_retained(bool retained) { msg_.retained = to_int(retained); }
350 const properties& get_properties() const { return props_; }
355 void set_properties(const properties& props) {
356 props_ = props;
357 msg_.properties = props_.c_struct();
358 }
364 props_ = std::move(props);
365 msg_.properties = props_.c_struct();
366 }
371 string to_string() const { return get_payload_str(); }
372};
373
376
379
391 string_ref topic, const void* payload, size_t len, int qos, bool retained,
392 const properties& props = properties()
393) {
394 return mqtt::message::create(std::move(topic), payload, len, qos, retained, props);
395}
396
404inline message_ptr make_message(string_ref topic, const void* payload, size_t len) {
405 return mqtt::message::create(std::move(topic), payload, len);
406}
407
416 string_ref topic, binary_ref payload, int qos, bool retained,
417 const properties& props = properties()
418) {
419 return mqtt::message::create(std::move(topic), std::move(payload), qos, retained, props);
420}
421
429 return mqtt::message::create(std::move(topic), std::move(payload));
430}
431
433
438{
440 message_ptr msg_;
441
442public:
448 message_ptr_builder() : msg_{std::make_shared<message>()} {}
454 msg_->set_topic(topic);
455 return *this;
456 }
465 msg_->set_payload(payload);
466 return *this;
467 }
473 auto payload(const void* payload, size_t n) -> self& {
474 msg_->set_payload(payload, n);
475 return *this;
476 }
481 auto qos(int qos) -> self& {
482 msg_->set_qos(qos);
483 return *this;
484 }
490 auto retained(bool on) -> self& {
491 msg_->set_retained(on);
492 return *this;
493 }
498 auto properties(mqtt::properties&& props) -> self& {
499 msg_->set_properties(std::move(props));
500 return *this;
501 }
506 auto properties(const mqtt::properties& props) -> self& {
507 msg_->set_properties(props);
508 return *this;
509 }
514 message_ptr finalize() { return msg_; }
515};
516
518} // namespace mqtt
519
520#endif // __mqtt_message_h
Definition async_client.h:121
char value_type
Definition buffer_ref.h:67
const blob & str() const
Definition buffer_ref.h:253
Definition exception.h:48
Definition message.h:438
auto qos(int qos) -> self &
Definition message.h:481
auto properties(mqtt::properties &&props) -> self &
Definition message.h:498
auto payload(binary_ref payload) -> self &
Definition message.h:464
auto payload(const void *payload, size_t n) -> self &
Definition message.h:473
message_ptr finalize()
Definition message.h:514
auto topic(string_ref topic) -> self &
Definition message.h:453
auto properties(const mqtt::properties &props) -> self &
Definition message.h:506
message_ptr_builder()
Definition message.h:448
auto retained(bool on) -> self &
Definition message.h:490
Definition message.h:57
void set_properties(properties &&props)
Definition message.h:363
message(string_ref topic, binary_ref payload)
Definition message.h:143
const properties & get_properties() const
Definition message.h:350
const string & get_payload_str() const
Definition message.h:285
message(string_ref topic, const void *payload, size_t len)
Definition message.h:122
message(const message &other)
static ptr_t create(string_ref topic, const void *payload, size_t len)
Definition message.h:190
message()
Definition message.h:100
static constexpr bool DFLT_RETAINED
Definition message.h:62
string to_string() const
Definition message.h:371
void set_retained(bool retained)
Definition message.h:345
void set_topic(string_ref topic)
Definition message.h:253
static void validate_qos(int qos)
Definition message.h:336
message & operator=(message &&rhs)
void set_payload(binary_ref payload)
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:299
static constexpr int DFLT_QOS
Definition message.h:60
message(message &&other)
std::shared_ptr< const message > const_ptr_t
Definition message.h:94
const binary & get_payload() const
Definition message.h:279
const string_ref & get_topic_ref() const
Definition message.h:260
void set_properties(const properties &props)
Definition message.h:355
const string & get_topic() const
Definition message.h:265
message & operator=(const message &rhs)
bool is_retained() const
Definition message.h:306
~message()
Definition message.h:164
const binary_ref & get_payload_ref() const
Definition message.h:275
void set_payload(const void *payload, size_t n)
Definition message.h:320
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
static ptr_t create(string_ref topic, binary_ref payload, int qos, bool retained, const properties &props=properties())
Definition message.h:204
int get_qos() const
Definition message.h:292
std::shared_ptr< message > ptr_t
Definition message.h:92
static ptr_t create(string_ref topic, binary_ref payload)
Definition message.h:218
void set_qos(int qos)
Definition message.h:327
static ptr_t create(string_ref topic, const MQTTAsync_message &msg)
Definition message.h:228
Definition properties.h:293
const MQTTProperties & c_struct() const
Definition properties.h:389
Definition topic.h:45
Definition async_client.h:60
message::ptr_t message_ptr
Definition message.h:375
bool to_bool(int n)
Definition types.h:107
std::string binary
Definition types.h:45
buffer_ref< char > binary_ref
Definition buffer_ref.h:305
message::const_ptr_t const_message_ptr
Definition message.h:378
buffer_ref< char > string_ref
Definition buffer_ref.h:297
int to_int(bool b)
Definition types.h:113
message_ptr make_message(string_ref topic, const void *payload, size_t len, int qos, bool retained, const properties &props=properties())
Definition message.h:390