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;
67
69 MQTTAsync_message msg_{DFLT_C_STRUCT};
71 string_ref topic_;
73 binary_ref payload_;
75 properties props_;
76
78 friend class async_client;
79
84 void set_duplicate(bool dup) { msg_.dup = to_int(dup); }
85
86public:
88 using ptr_t = std::shared_ptr<message>;
90 using const_ptr_t = std::shared_ptr<const message>;
91
108 string_ref topic, const void* payload, size_t len, int qos, bool retained,
109 const properties& props = properties()
110 );
118 message(string_ref topic, const void* payload, size_t len)
119 : message(std::move(topic), payload, len, DFLT_QOS, DFLT_RETAINED) {}
120
130 string_ref topic, binary_ref payload, int qos, bool retained,
131 const properties& props = properties()
132 );
140 : message(std::move(topic), std::move(payload), DFLT_QOS, DFLT_RETAINED) {}
141
146 message(string_ref topic, const MQTTAsync_message& cmsg);
151 message(const message& other);
156 message(message&& other);
161
171 static ptr_t create(
172 string_ref topic, const void* payload, size_t len, int qos, bool retained,
173 const properties& props = properties()
174 ) {
175 return std::make_shared<message>(
176 std::move(topic), payload, len, qos, retained, props
177 );
178 }
179
186 static ptr_t create(string_ref topic, const void* payload, size_t len) {
187 return std::make_shared<message>(
188 std::move(topic), payload, len, DFLT_QOS, DFLT_RETAINED
189 );
190 }
191
200 static ptr_t create(
201 string_ref topic, binary_ref payload, int qos, bool retained,
202 const properties& props = properties()
203 ) {
204 return std::make_shared<message>(
205 std::move(topic), std::move(payload), qos, retained, props
206 );
207 }
208
215 return std::make_shared<message>(
216 std::move(topic), std::move(payload), DFLT_QOS, DFLT_RETAINED
217 );
218 }
219
224 static ptr_t create(string_ref topic, const MQTTAsync_message& msg) {
225 return std::make_shared<message>(std::move(topic), msg);
226 }
227
242#if defined(UNIT_TESTS)
243 const MQTTAsync_message& c_struct() const { return msg_; }
244#endif
250 topic_ = topic ? std::move(topic) : string_ref(string());
251 }
252
256 const string_ref& get_topic_ref() const { return topic_; }
261 const string& get_topic() const {
262 static const string EMPTY_STR;
263 return topic_ ? topic_.str() : EMPTY_STR;
264 }
265
272 const binary_ref& get_payload_ref() const { return payload_; }
276 const binary& get_payload() const {
277 static const binary EMPTY_BIN;
278 return payload_ ? payload_.str() : EMPTY_BIN;
279 }
280
283 const string& get_payload_str() const {
284 static const string EMPTY_STR;
285 return payload_ ? payload_.str() : EMPTY_STR;
286 }
287
291 int get_qos() const { return msg_.qos; }
298 bool is_duplicate() const { return to_bool(msg_.dup); }
305 bool is_retained() const { return to_bool(msg_.retained); }
313 void set_payload(binary_ref payload);
319 void set_payload(const void* payload, size_t n) {
320 set_payload(binary_ref(static_cast<const binary_ref::value_type*>(payload), n));
321 }
322
326 void set_qos(int qos) {
327 validate_qos(qos);
328 msg_.qos = qos;
329 }
330
335 static void validate_qos(int qos) {
336 if (qos < 0 || qos > 2)
337 throw exception(MQTTASYNC_BAD_QOS, "Bad QoS");
338 }
339
344 void set_retained(bool retained) { msg_.retained = to_int(retained); }
349 const properties& get_properties() const { return props_; }
354 void set_properties(const properties& props) {
355 props_ = props;
356 msg_.properties = props_.c_struct();
357 }
358
363 props_ = std::move(props);
364 msg_.properties = props_.c_struct();
365 }
366
370 string to_string() const { return get_payload_str(); }
371};
372
375
378
390 string_ref topic, const void* payload, size_t len, int qos, bool retained,
391 const properties& props = properties()
392) {
393 return mqtt::message::create(std::move(topic), payload, len, qos, retained, props);
394}
395
403inline message_ptr make_message(string_ref topic, const void* payload, size_t len) {
404 return mqtt::message::create(std::move(topic), payload, len);
405}
406
415 string_ref topic, binary_ref payload, int qos, bool retained,
416 const properties& props = properties()
417) {
418 return mqtt::message::create(std::move(topic), std::move(payload), qos, retained, props);
419}
420
428 return mqtt::message::create(std::move(topic), std::move(payload));
429}
430
432
437{
439 message_ptr msg_;
440
441public:
447 message_ptr_builder() : msg_{std::make_shared<message>()} {}
453 msg_->set_topic(topic);
454 return *this;
455 }
456
464 msg_->set_payload(payload);
465 return *this;
466 }
467
472 auto payload(const void* payload, size_t n) -> self& {
473 msg_->set_payload(payload, n);
474 return *this;
475 }
476
480 auto qos(int qos) -> self& {
481 msg_->set_qos(qos);
482 return *this;
483 }
484
489 auto retained(bool on) -> self& {
490 msg_->set_retained(on);
491 return *this;
492 }
493
497 auto properties(mqtt::properties&& props) -> self& {
498 msg_->set_properties(std::move(props));
499 return *this;
500 }
501
505 auto properties(const mqtt::properties& props) -> self& {
506 msg_->set_properties(props);
507 return *this;
508 }
509
513 message_ptr finalize() { return msg_; }
514};
515
517} // namespace mqtt
518
519#endif // __mqtt_message_h
char value_type
Definition buffer_ref.h:67
Definition exception.h:48
auto qos(int qos) -> self &
Definition message.h:480
auto properties(mqtt::properties &&props) -> self &
Definition message.h:497
auto payload(binary_ref payload) -> self &
Definition message.h:463
auto payload(const void *payload, size_t n) -> self &
Definition message.h:472
message_ptr finalize()
Definition message.h:513
auto topic(string_ref topic) -> self &
Definition message.h:452
auto properties(const mqtt::properties &props) -> self &
Definition message.h:505
message_ptr_builder()
Definition message.h:447
auto retained(bool on) -> self &
Definition message.h:489
message_ptr_builder self
Definition message.h:443
Definition message.h:57
void set_properties(properties &&props)
Definition message.h:362
message(string_ref topic, binary_ref payload)
Definition message.h:139
const properties & get_properties() const
Definition message.h:349
const string & get_payload_str() const
Definition message.h:283
message(string_ref topic, const void *payload, size_t len)
Definition message.h:118
message(const message &other)
static ptr_t create(string_ref topic, const void *payload, size_t len)
Definition message.h:186
message()
Definition message.h:96
static constexpr bool DFLT_RETAINED
Definition message.h:62
string to_string() const
Definition message.h:370
void set_retained(bool retained)
Definition message.h:344
void set_topic(string_ref topic)
Definition message.h:249
static void validate_qos(int qos)
Definition message.h:335
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:298
static constexpr int DFLT_QOS
Definition message.h:60
message(message &&other)
std::shared_ptr< const message > const_ptr_t
Definition message.h:90
const binary & get_payload() const
Definition message.h:276
const string_ref & get_topic_ref() const
Definition message.h:256
void set_properties(const properties &props)
Definition message.h:354
const string & get_topic() const
Definition message.h:261
message & operator=(const message &rhs)
friend class async_client
Definition message.h:78
bool is_retained() const
Definition message.h:305
~message()
Definition message.h:160
const binary_ref & get_payload_ref() const
Definition message.h:272
void set_payload(const void *payload, size_t n)
Definition message.h:319
static ptr_t create(string_ref topic, const void *payload, size_t len, int qos, bool retained, const properties &props=properties())
Definition message.h:171
static ptr_t create(string_ref topic, binary_ref payload, int qos, bool retained, const properties &props=properties())
Definition message.h:200
int get_qos() const
Definition message.h:291
std::shared_ptr< message > ptr_t
Definition message.h:88
static ptr_t create(string_ref topic, binary_ref payload)
Definition message.h:214
void set_qos(int qos)
Definition message.h:326
static ptr_t create(string_ref topic, const MQTTAsync_message &msg)
Definition message.h:224
Definition properties.h:293
properties()
Definition properties.h:358
Definition topic.h:45
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:377
std::string binary
Definition types.h:45
message::ptr_t message_ptr
Definition message.h:374
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:389