message.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // message.cpp
  2. /*******************************************************************************
  3. * Copyright (c) 2013-2017 Frank Pagliughi <fpagliughi@mindspring.com>
  4. *
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * and Eclipse Distribution License v1.0 which accompany this distribution.
  8. *
  9. * The Eclipse Public License is available at
  10. * http://www.eclipse.org/legal/epl-v10.html
  11. * and the Eclipse Distribution License is available at
  12. * http://www.eclipse.org/org/documents/edl-v10.php.
  13. *
  14. * Contributors:
  15. * Frank Pagliughi - initial implementation and documentation
  16. *******************************************************************************/
  17. #include "mqtt/message.h"
  18. #include <cstring>
  19. #include <utility>
  20. namespace mqtt {
  21. /////////////////////////////////////////////////////////////////////////////
  22. #if __cplusplus < 201703L
  23. constexpr int message::DFLT_QOS;
  24. constexpr bool message::DFLT_RETAINED;
  25. #endif
  26. const MQTTAsync_message message::DFLT_C_STRUCT = MQTTAsync_message_initializer;
  27. // --------------------------------------------------------------------------
  28. message::message() : msg_(DFLT_C_STRUCT)
  29. {
  30. }
  31. message::message(string_ref topic, const void* payload, size_t len, int qos, bool retained)
  32. : msg_(DFLT_C_STRUCT), topic_(std::move(topic))
  33. {
  34. set_payload(payload, len);
  35. set_qos(qos);
  36. set_retained(retained);
  37. }
  38. message::message(string_ref topic, binary_ref payload, int qos, bool retained)
  39. : msg_(DFLT_C_STRUCT), topic_(std::move(topic))
  40. {
  41. set_payload(std::move(payload));
  42. set_qos(qos);
  43. set_retained(retained);
  44. }
  45. message::message(string_ref topic, const MQTTAsync_message& msg)
  46. : msg_(msg), topic_(std::move(topic)), props_(msg.properties)
  47. {
  48. msg_.properties = props_.c_struct();
  49. set_payload(msg.payload, msg.payloadlen);
  50. }
  51. message::message(const message& other) : msg_(other.msg_), topic_(other.topic_)
  52. {
  53. set_payload(other.payload_);
  54. }
  55. message::message(message&& other) : msg_(other.msg_), topic_(std::move(other.topic_))
  56. {
  57. set_payload(std::move(other.payload_));
  58. other.msg_.payloadlen = 0;
  59. other.msg_.payload = nullptr;
  60. }
  61. message& message::operator=(const message& rhs)
  62. {
  63. if (&rhs != this) {
  64. msg_ = rhs.msg_;
  65. topic_ = rhs.topic_;
  66. set_payload(rhs.payload_);
  67. }
  68. return *this;
  69. }
  70. message& message::operator=(message&& rhs)
  71. {
  72. if (&rhs != this) {
  73. msg_ = rhs.msg_;
  74. topic_ = std::move(rhs.topic_);
  75. set_payload(std::move(rhs.payload_));
  76. rhs.msg_.payloadlen = 0;
  77. rhs.msg_.payload = nullptr;
  78. }
  79. return *this;
  80. }
  81. void message::clear_payload()
  82. {
  83. payload_.reset();
  84. msg_.payload = nullptr;
  85. msg_.payloadlen = 0;
  86. }
  87. void message::set_payload(binary_ref payload)
  88. {
  89. payload_ = std::move(payload);
  90. if (payload_.empty()) {
  91. msg_.payload = nullptr;
  92. msg_.payloadlen = 0;
  93. }
  94. else {
  95. msg_.payload = const_cast<binary_ref::value_type*>(payload_.data());
  96. msg_.payloadlen = payload_.length();
  97. }
  98. }
  99. /////////////////////////////////////////////////////////////////////////////
  100. // end namespace mqtt
  101. }