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 PAHO_MQTTPP_EXPORT static const string EMPTY_STR;
70 PAHO_MQTTPP_EXPORT 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 { return topic_ ? topic_.str() : EMPTY_STR; }
273 const binary_ref& get_payload_ref() const { return payload_; }
277 const binary& get_payload() const { return payload_ ? payload_.str() : EMPTY_BIN; }
281 const string& get_payload_str() const { return payload_ ? payload_.str() : EMPTY_STR; }
286 int get_qos() const { return msg_.qos; }
293 bool is_duplicate() const { return to_bool(msg_.dup); }
300 bool is_retained() const { return to_bool(msg_.retained); }
308 void set_payload(binary_ref payload);
314 void set_payload(const void* payload, size_t n) {
315 set_payload(binary_ref(static_cast<const binary_ref::value_type*>(payload), n));
316 }
321 void set_qos(int qos) {
322 validate_qos(qos);
323 msg_.qos = qos;
324 }
330 static void validate_qos(int qos) {
331 if (qos < 0 || qos > 2)
332 throw exception(MQTTASYNC_BAD_QOS, "Bad QoS");
333 }
339 void set_retained(bool retained) { msg_.retained = to_int(retained); }
344 const properties& get_properties() const { return props_; }
349 void set_properties(const properties& props) {
350 props_ = props;
351 msg_.properties = props_.c_struct();
352 }
358 props_ = std::move(props);
359 msg_.properties = props_.c_struct();
360 }
365 string to_string() const { return get_payload_str(); }
366};
367
370
373
385 string_ref topic, const void* payload, size_t len, int qos, bool retained,
386 const properties& props = properties()
387) {
388 return mqtt::message::create(std::move(topic), payload, len, qos, retained, props);
389}
390
398inline message_ptr make_message(string_ref topic, const void* payload, size_t len) {
399 return mqtt::message::create(std::move(topic), payload, len);
400}
401
410 string_ref topic, binary_ref payload, int qos, bool retained,
411 const properties& props = properties()
412) {
413 return mqtt::message::create(std::move(topic), std::move(payload), qos, retained, props);
414}
415
423 return mqtt::message::create(std::move(topic), std::move(payload));
424}
425
427
432{
434 message_ptr msg_;
435
436public:
442 message_ptr_builder() : msg_{std::make_shared<message>()} {}
448 msg_->set_topic(topic);
449 return *this;
450 }
459 msg_->set_payload(payload);
460 return *this;
461 }
467 auto payload(const void* payload, size_t n) -> self& {
468 msg_->set_payload(payload, n);
469 return *this;
470 }
475 auto qos(int qos) -> self& {
476 msg_->set_qos(qos);
477 return *this;
478 }
484 auto retained(bool on) -> self& {
485 msg_->set_retained(on);
486 return *this;
487 }
492 auto properties(mqtt::properties&& props) -> self& {
493 msg_->set_properties(std::move(props));
494 return *this;
495 }
500 auto properties(const mqtt::properties& props) -> self& {
501 msg_->set_properties(props);
502 return *this;
503 }
508 message_ptr finalize() { return msg_; }
509};
510
512} // namespace mqtt
513
514#endif // __mqtt_message_h
Definition async_client.h:137
char value_type
Definition buffer_ref.h:67
const blob & str() const
Definition buffer_ref.h:253
Definition exception.h:48
Definition message.h:432
auto qos(int qos) -> self &
Definition message.h:475
auto properties(mqtt::properties &&props) -> self &
Definition message.h:492
auto payload(binary_ref payload) -> self &
Definition message.h:458
auto payload(const void *payload, size_t n) -> self &
Definition message.h:467
message_ptr finalize()
Definition message.h:508
auto topic(string_ref topic) -> self &
Definition message.h:447
auto properties(const mqtt::properties &props) -> self &
Definition message.h:500
message_ptr_builder()
Definition message.h:442
auto retained(bool on) -> self &
Definition message.h:484
Definition message.h:57
void set_properties(properties &&props)
Definition message.h:357
message(string_ref topic, binary_ref payload)
Definition message.h:143
const properties & get_properties() const
Definition message.h:344
const string & get_payload_str() const
Definition message.h:281
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:365
void set_retained(bool retained)
Definition message.h:339
void set_topic(string_ref topic)
Definition message.h:253
static void validate_qos(int qos)
Definition message.h:330
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:293
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:277
const string_ref & get_topic_ref() const
Definition message.h:260
void set_properties(const properties &props)
Definition message.h:349
const string & get_topic() const
Definition message.h:265
message & operator=(const message &rhs)
bool is_retained() const
Definition message.h:300
~message()
Definition message.h:164
const binary_ref & get_payload_ref() const
Definition message.h:273
void set_payload(const void *payload, size_t n)
Definition message.h:314
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:286
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:321
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
#define PAHO_MQTTPP_EXPORT
Definition export.h:40
Definition async_client.h:60
buffer_ref< char > binary_ref
Definition buffer_ref.h:305
bool to_bool(int n)
Definition types.h:107
message::const_ptr_t const_message_ptr
Definition message.h:372
std::string binary
Definition types.h:45
message::ptr_t message_ptr
Definition message.h:369
int to_int(bool b)
Definition types.h:113
buffer_ref< char > string_ref
Definition buffer_ref.h:297
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