summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/mqtt/qmqttclient.cpp3
-rw-r--r--src/mqtt/qmqttconnection.cpp12
-rw-r--r--src/mqtt/qmqttconnection_p.h1
3 files changed, 16 insertions, 0 deletions
diff --git a/src/mqtt/qmqttclient.cpp b/src/mqtt/qmqttclient.cpp
index 57050a0..e233ae7 100644
--- a/src/mqtt/qmqttclient.cpp
+++ b/src/mqtt/qmqttclient.cpp
@@ -93,6 +93,9 @@ Q_LOGGING_CATEGORY(lcMqttClient, "qt.mqtt.client")
The interval is specified in milliseconds. However, most brokers are not
capable of using such a high granularity and will fall back to an interval
specified in seconds.
+
+ If the broker does not respond within a grace period the connection will be
+ closed.
*/
/*!
diff --git a/src/mqtt/qmqttconnection.cpp b/src/mqtt/qmqttconnection.cpp
index 8ef8fff..6759306 100644
--- a/src/mqtt/qmqttconnection.cpp
+++ b/src/mqtt/qmqttconnection.cpp
@@ -578,11 +578,19 @@ bool QMqttConnection::sendControlPingRequest()
if (m_internalState != QMqttConnection::BrokerConnected)
return false;
+ // 3.1.2.10 If a Client does not receive a PINGRESP packet within a reasonable amount of time
+ // after it has sent a PINGREQ, it SHOULD close the Network Connection to the Server
+ // Consider two pending PINGRESP as reasonable.
+ if (m_pingTimeout > 1) {
+ closeConnection(QMqttClient::ServerUnavailable);
+ return false;
+ }
const QMqttControlPacket packet(QMqttControlPacket::PINGREQ);
if (!writePacketToTransport(packet)) {
qCDebug(lcMqttConnection) << "Failed to write PINGREQ to transport.";
return false;
}
+ m_pingTimeout++;
return true;
}
@@ -591,6 +599,7 @@ bool QMqttConnection::sendControlDisconnect()
qCDebug(lcMqttConnection) << Q_FUNC_INFO;
m_pingTimer.stop();
+ m_pingTimeout = 0;
m_activeSubscriptions.clear();
@@ -676,6 +685,7 @@ void QMqttConnection::transportConnectionClosed()
m_readBuffer.clear();
m_readPosition = 0;
m_pingTimer.stop();
+ m_pingTimeout = 0;
if (m_internalState == BrokerDisconnected) // We manually disconnected
m_clientPrivate->setStateAndError(QMqttClient::Disconnected, QMqttClient::NoError);
else
@@ -730,6 +740,7 @@ void QMqttConnection::closeConnection(QMqttClient::ClientError error)
m_readBuffer.clear();
m_readPosition = 0;
m_pingTimer.stop();
+ m_pingTimeout = 0;
m_activeSubscriptions.clear();
m_internalState = BrokerDisconnected;
m_transport->disconnect();
@@ -1620,6 +1631,7 @@ void QMqttConnection::finalize_pingresp()
closeConnection(QMqttClient::ProtocolViolation);
return;
}
+ m_pingTimeout--;
emit m_clientPrivate->m_client->pingResponseReceived();
}
diff --git a/src/mqtt/qmqttconnection_p.h b/src/mqtt/qmqttconnection_p.h
index 933d1dc..626addc 100644
--- a/src/mqtt/qmqttconnection_p.h
+++ b/src/mqtt/qmqttconnection_p.h
@@ -157,6 +157,7 @@ private:
QMap<quint16, QSharedPointer<QMqttControlPacket>> m_pendingReleaseMessages;
InternalConnectionState m_internalState{BrokerDisconnected};
QTimer m_pingTimer;
+ int m_pingTimeout{0};
QVector<QMqttTopicName> m_receiveAliases;
QVector<QMqttTopicName> m_publishAliases;