paho-mqtt-cpp
MQTT C++ Client for POSIX and Windows
message.h
Go to the documentation of this file.
1 
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 
35 namespace mqtt {
36 
38 
55 class message
56 {
57 public:
59  PAHO_MQTTPP_EXPORT static const int DFLT_QOS; // =0
61  PAHO_MQTTPP_EXPORT static const bool DFLT_RETAINED; // =false
62 
63 private:
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 
85 public:
87  using ptr_t = std::shared_ptr<message>;
89  using const_ptr_t = std::shared_ptr<const message>;
90 
95  message();
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);
156  ~message() {}
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  }
221  message& operator=(const message& rhs);
227  message& operator=(message&& rhs);
231  #if defined(UNIT_TESTS)
232  const MQTTAsync_message& c_struct() const { return msg_; }
233  #endif
234 
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  }
257  void clear_payload();
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  }
353  void set_properties(properties&& props) {
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 
377 inline 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 
390 inline 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 
427 public:
429  using self = message_ptr_builder;
433  message_ptr_builder() : msg_{ std::make_shared<message>() } {}
438  auto topic(string_ref topic) -> self& {
439  msg_->set_topic(topic);
440  return *this;
441  }
449  auto payload(binary_ref payload) -> self& {
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 
message::const_ptr_t const_message_ptr
Definition: message.h:368
void set_payload(const void *payload, size_t n)
Definition: message.h:308
void set_topic(string_ref topic)
Definition: message.h:238
Definition: async_client.h:107
void set_qos(int qos)
Definition: message.h:315
auto properties(const mqtt::properties &props) -> self &
Definition: message.h:491
buffer_ref< char > binary_ref
Definition: buffer_ref.h:298
bool to_bool(int n)
Definition: types.h:161
void set_payload(binary_ref payload)
void set_properties(const properties &props)
Definition: message.h:345
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
string to_string() const
Definition: message.h:361
auto payload(binary_ref payload) -> self &
Definition: message.h:449
static ptr_t create(string_ref topic, const MQTTAsync_message &msg)
Definition: message.h:213
bool is_duplicate() const
Definition: message.h:287
message_ptr_builder()
Definition: message.h:433
message(string_ref topic, const void *payload, size_t len)
Definition: message.h:116
Definition: topic.h:43
auto topic(string_ref topic) -> self &
Definition: message.h:438
message_ptr make_message(string_ref topic, const void *payload, size_t len)
Definition: message.h:377
auto payload(const void *payload, size_t n) -> self &
Definition: message.h:458
void set_retained(bool retained)
Definition: message.h:333
const string & get_payload_str() const
Definition: message.h:272
auto properties(mqtt::properties &&props) -> self &
Definition: message.h:483
bool is_retained() const
Definition: message.h:294
std::shared_ptr< message > ptr_t
Definition: message.h:87
message & operator=(const message &rhs)
Definition: exception.h:46
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
Definition: properties.h:255
const properties & get_properties() const
Definition: message.h:338
std::string binary
Definition: types.h:42
message(string_ref topic, binary_ref payload)
Definition: message.h:135
Definition: message.h:422
const MQTTProperties & c_struct() const
Definition: properties.h:308
Definition: message.h:55
auto qos(int qos) -> self &
Definition: message.h:466
const string_ref & get_topic_ref() const
Definition: message.h:245
const string & get_topic() const
Definition: message.h:250
static PAHO_MQTTPP_EXPORT const int DFLT_QOS
Definition: message.h:59
#define PAHO_MQTTPP_EXPORT
Definition: export.h:40
~message()
Definition: message.h:156
void set_properties(properties &&props)
Definition: message.h:353
static ptr_t create(string_ref topic, const void *payload, size_t len)
Definition: message.h:180
const binary_ref & get_payload_ref() const
Definition: message.h:261
static void validate_qos(int qos)
Definition: message.h:324
int to_int(bool b)
Definition: types.h:167
static ptr_t create(string_ref topic, binary_ref payload)
Definition: message.h:204
message_ptr finalize()
Definition: message.h:499
Definition: async_client.h:49
auto retained(bool on) -> self &
Definition: message.h:475
static PAHO_MQTTPP_EXPORT const bool DFLT_RETAINED
Definition: message.h:61
buffer_ref< char > string_ref
Definition: buffer_ref.h:290
message::ptr_t message_ptr
Definition: message.h:365
std::shared_ptr< const message > const_ptr_t
Definition: message.h:89
const blob & str() const
Definition: buffer_ref.h:246
void clear_payload()
const binary & get_payload() const
Definition: message.h:265