1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qmqttmessage.h"
6#include "qmqttmessage_p.h"
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 \class QMqttMessage
12
13 \inmodule QtMqtt
14 \brief The QMqttMessage class provides information about a message received
15 from a message broker based on a subscription.
16
17 An MQTT message is created inside the module and returned via the
18 \l QMqttSubscription::messageReceived() signal.
19*/
20
21/*!
22 \property QMqttMessage::topic
23 \brief This property holds the topic of a message.
24
25 In case a wildcard has been used for a subscription, describes the topic
26 matching this subscription. This property never contains wildcards.
27*/
28
29/*!
30 \property QMqttMessage::payload
31 \brief This property holds the payload of a message.
32*/
33
34/*!
35 \property QMqttMessage::id
36 \brief This property holds the ID of the message.
37
38 IDs are used for messages with a QoS level above zero.
39*/
40
41/*!
42 \property QMqttMessage::qos
43 \brief This property holds the QoS level of a message.
44*/
45
46/*!
47 \property QMqttMessage::duplicate
48 \brief This property holds whether the message is a duplicate.
49
50 Duplicate messages indicate that the message has been sent earlier, but it
51 has not been confirmed yet. Hence, the broker assumes that it needs to
52 resend to verify the transport of the message itself. Duplicate messages
53 can only occur if the QoS level is one or two.
54*/
55
56/*!
57 \property QMqttMessage::retain
58 \brief This property holds whether the message has been retained.
59
60 A retained message is kept on the broker for future clients to subscribe.
61 Consequently, a retained message has been created previously and is not a
62 live update. A broker can store only one retained message per topic.
63*/
64
65/*!
66 Creates a new MQTT message.
67*/
68QMqttMessage::QMqttMessage()
69 : d(new QMqttMessagePrivate())
70{
71}
72
73/*!
74 Constructs a new MQTT message that is a copy of \a other.
75*/
76QMqttMessage::QMqttMessage(const QMqttMessage &other)
77 : d(other.d)
78{
79}
80
81QMqttMessage::~QMqttMessage()
82{
83}
84
85/*!
86 Makes this object a copy of \a other and returns the new value of this object.
87*/
88QMqttMessage &QMqttMessage::operator=(const QMqttMessage &other)
89{
90 d = other.d;
91 return *this;
92}
93
94/*!
95 Returns \c true if the message and \a other are equal, otherwise returns \c false.
96*/
97bool QMqttMessage::operator==(const QMqttMessage &other) const
98{
99 return d == other.d;
100}
101
102/*!
103 Returns \c true if the message and \a other are not equal, otherwise returns \c false.
104*/
105bool QMqttMessage::operator!=(const QMqttMessage &other) const
106{
107 return !(*this == other);
108}
109
110const QByteArray &QMqttMessage::payload() const
111{
112 return d->m_payload;
113}
114
115quint8 QMqttMessage::qos() const
116{
117 return d->m_qos;
118}
119
120quint16 QMqttMessage::id() const
121{
122 return d->m_id;
123}
124
125QMqttTopicName QMqttMessage::topic() const
126{
127 return d->m_topic;
128}
129
130bool QMqttMessage::duplicate() const
131{
132 return d->m_duplicate;
133}
134
135bool QMqttMessage::retain() const
136{
137 return d->m_retain;
138}
139
140/*!
141 \since 5.12
142
143 Returns the publish properties received as part of the message.
144
145 \note This function only specifies the properties when a
146 publish message is received. Messages with a QoS value of
147 1 or 2 can contain additional properties when a message is released.
148 Those can be obtained by the QMqttClient::messageStatusChanged signal.
149
150 \note This function will only provide valid data when the client
151 specifies QMqttClient::MQTT_5_0 as QMqttClient::ProtocolVersion.
152*/
153QMqttPublishProperties QMqttMessage::publishProperties() const
154{
155 return d->m_publishProperties;
156}
157
158/*!
159 \internal
160 Constructs a new MQTT message with \a content on the topic \a topic.
161 Furthermore the properties \a id, \a qos, \a dup, \a retain must be specified.
162
163 This constructor is mostly used internally to construct a QMqttMessage at message
164 receival.
165*/
166QMqttMessage::QMqttMessage(const QMqttTopicName &topic, const QByteArray &content, quint16 id, quint8 qos, bool dup, bool retain)
167 : d(new QMqttMessagePrivate)
168{
169 d->m_topic = topic;
170 d->m_payload = content;
171 d->m_id = id;
172 d->m_qos = qos;
173 d->m_duplicate = dup;
174 d->m_retain = retain;
175}
176
177QT_END_NAMESPACE
178

source code of qtmqtt/src/mqtt/qmqttmessage.cpp