From 2e5d5e3cfcf87c27f912d85dac38ff926f2413d7 Mon Sep 17 00:00:00 2001 From: Maurice Kalinowski Date: Mon, 23 Jan 2017 14:50:49 +0100 Subject: [PATCH] Send ping on keepAlive The broker expects a ping from each client to be received within KeepAlive * 1.5 timeframe. If there is non received, the broker will disconnect. --- src/mqtt/qmqttconnection.cpp | 9 ++++++++- src/mqtt/qmqttconnection_p.h | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/mqtt/qmqttconnection.cpp b/src/mqtt/qmqttconnection.cpp index b42ed55..ace8757 100644 --- a/src/mqtt/qmqttconnection.cpp +++ b/src/mqtt/qmqttconnection.cpp @@ -35,7 +35,8 @@ QT_BEGIN_NAMESPACE QMqttConnection::QMqttConnection(QObject *parent) : QObject(parent) { - + m_pingTimer.setSingleShot(false); + m_pingTimer.connect(&m_pingTimer, &QTimer::timeout, this, &QMqttConnection::sendControlPingRequest); } QMqttConnection::~QMqttConnection() @@ -214,6 +215,8 @@ bool QMqttConnection::sendControlPingRequest() bool QMqttConnection::sendControlDisconnect() { + m_pingTimer.stop(); + const QMqttControlPacket packet(QMqttControlPacket::DISCONNECT); if (!writePacketToTransport(packet)) { qWarning("Could not write DISCONNECT frame to transport"); @@ -237,6 +240,7 @@ void QMqttConnection::setClient(QMqttClient *client) void QMqttConnection::transportConnectionClosed() { qWarning("Transport Connection closed!"); + m_pingTimer.stop(); m_client->setState(QMqttClient::Disconnected); } @@ -282,6 +286,9 @@ void QMqttConnection::transportReadReady() } m_internalState = BrokerConnected; m_client->setState(QMqttClient::Connected); + + m_pingTimer.setInterval(m_client->keepAlive() * 1000); + m_pingTimer.start(); break; } case QMqttControlPacket::SUBACK: { diff --git a/src/mqtt/qmqttconnection_p.h b/src/mqtt/qmqttconnection_p.h index f419cf1..cdbb315 100644 --- a/src/mqtt/qmqttconnection_p.h +++ b/src/mqtt/qmqttconnection_p.h @@ -43,7 +43,8 @@ #include "qmqttclient.h" #include "qmqttcontrolpacket_p.h" -#include +#include +#include QT_BEGIN_NAMESPACE @@ -99,6 +100,7 @@ private: bool writePacketToTransport(const QMqttControlPacket &p); QSet m_pendingSubscriptionAck; InternalConnectionState m_internalState{BrokerDisconnected}; + QTimer m_pingTimer; }; QT_END_NAMESPACE