1
0
mirror of https://github.com/qt/qtmqtt.git synced 2026-10-25 16:16:26 +00:00

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.
This commit is contained in:
Maurice Kalinowski 2017-01-23 14:50:49 +01:00
parent c7153f44e0
commit 2e5d5e3cfc
2 changed files with 11 additions and 2 deletions

View File

@ -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: {

View File

@ -43,7 +43,8 @@
#include "qmqttclient.h"
#include "qmqttcontrolpacket_p.h"
#include <QObject>
#include <QtCore/QObject>
#include <QtCore/QTimer>
QT_BEGIN_NAMESPACE
@ -99,6 +100,7 @@ private:
bool writePacketToTransport(const QMqttControlPacket &p);
QSet<quint16> m_pendingSubscriptionAck;
InternalConnectionState m_internalState{BrokerDisconnected};
QTimer m_pingTimer;
};
QT_END_NAMESPACE