summaryrefslogtreecommitdiffstats
path: root/src/mqtt/qmqttclient.cpp
diff options
context:
space:
mode:
authorMaurice Kalinowski <maurice.kalinowski@qt.io>2019-07-02 12:42:40 +0200
committerMaurice Kalinowski <maurice.kalinowski@qt.io>2019-08-20 15:21:36 +0200
commitdcf3bbb9802f9adf79ae08d5d697b3ded3f0cb97 (patch)
tree8a11e22ba5340aa3c32a2ecf199120975e9907b6 /src/mqtt/qmqttclient.cpp
parent9e7eba636ebb5ca42822f682be2b442be50cde6e (diff)
Add autoKeepAlive to QMqttClient
In certain scenarios, a user wants to manually handle the keepAlive. Introduce autoKeepAlive as a property. If set to false, then the user needs to handle the keepAlive by invoking requestPing. The default for this property is true and keeps the current behavior. Task-number: QTBUG-76782 Change-Id: I073874e6bdacf3c8d128c20431606affe1d9eeb1 Reviewed-by: Alex Blasche <alexander.blasche@qt.io> Reviewed-by: Karsten Heimrich <karsten.heimrich@qt.io>
Diffstat (limited to 'src/mqtt/qmqttclient.cpp')
-rw-r--r--src/mqtt/qmqttclient.cpp56
1 files changed, 52 insertions, 4 deletions
diff --git a/src/mqtt/qmqttclient.cpp b/src/mqtt/qmqttclient.cpp
index bd5bb12..a13ce1e 100644
--- a/src/mqtt/qmqttclient.cpp
+++ b/src/mqtt/qmqttclient.cpp
@@ -94,6 +94,8 @@ Q_LOGGING_CATEGORY(lcMqttClient, "qt.mqtt.client")
If the broker does not respond within a grace period the connection will be
closed.
+
+ \sa autoKeepAlive(), requestPing(), pingResponseReceived()
*/
/*!
@@ -157,6 +159,24 @@ Q_LOGGING_CATEGORY(lcMqttClient, "qt.mqtt.client")
*/
/*!
+ \property QMqttClient::autoKeepAlive
+ \since 5.14
+ \brief This property holds whether the client will automatically manage
+ keep alive messages to the server.
+
+ If this property is \c true, then the client will automatically send a
+ ping message to the server at the keepAlive interval.
+
+ Otherwise, a user will have to manually invoke requestPing
+ within the specified interval of the connection. If no ping has been
+ sent within the interval, the server will disconnect.
+
+ The default of this property is \c true.
+
+ \sa keepAlive(), requestPing(), serverConnectionProperties(), pingResponseReceived()
+*/
+
+/*!
\enum QMqttClient::TransportType
This enum type specifies the connection method to be used to instantiate a
@@ -471,19 +491,25 @@ qint32 QMqttClient::publish(const QMqttTopicName &topic, const QMqttPublishPrope
}
/*!
- Sends a ping message to the broker and expects a reply. If the connection
- is active, the MQTT client will automatically send a ping message at
- \l keepAlive intervals.
+ Sends a ping message to the broker and expects a reply.
+
+ If the connection is active and \l autoKeepAlive is \c true, then calling this
+ function will fail as the client is responsible for managing this process.
+
+ Using \c requestPing() manually requires a call every time within the \l keepAlive
+ interval as long as the connection is active.
To check whether the ping is successful, connect to the
\l pingResponseReceived() signal.
Returns \c true if the ping request could be sent.
+
+ \sa pingResponseReceived(), autoKeepAlive(), keepAlive()
*/
bool QMqttClient::requestPing()
{
Q_D(QMqttClient);
- return d->m_connection.sendControlPingRequest();
+ return d->m_connection.sendControlPingRequest(false);
}
QString QMqttClient::hostname() const
@@ -642,6 +668,12 @@ bool QMqttClient::willRetain() const
return d->m_willRetain;
}
+bool QMqttClient::autoKeepAlive() const
+{
+ Q_D(const QMqttClient);
+ return d->m_autoKeepAlive;
+}
+
/*!
\since 5.12
@@ -985,6 +1017,22 @@ void QMqttClient::setWillRetain(bool willRetain)
emit willRetainChanged(willRetain);
}
+void QMqttClient::setAutoKeepAlive(bool autoKeepAlive)
+{
+ Q_D(QMqttClient);
+
+ if (state() != QMqttClient::Disconnected) {
+ qCDebug(lcMqttClient) << "Changing autoKeepAlive while connected is not possible.";
+ return;
+ }
+
+ if (d->m_autoKeepAlive == autoKeepAlive)
+ return;
+
+ d->m_autoKeepAlive = autoKeepAlive;
+ emit autoKeepAliveChanged(d->m_autoKeepAlive);
+}
+
void QMqttClient::setError(ClientError e)
{
Q_D(QMqttClient);