summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKonstantin Ritt <ritt.ks@gmail.com>2019-06-11 14:00:40 +0300
committerMaurice Kalinowski <maurice.kalinowski@qt.io>2019-07-03 13:09:09 +0200
commitfbc19ff4227e55e684059ca05618940650ec3883 (patch)
tree3a2092413d337eda55a19a03d363c4bbe4a73f29
parent234de63b6b0f1c35bdc255ea260e4b4bcdf26b35 (diff)
Prefer somewhat cheaper QBasicTimer over QTimer for periodic PING
Change-Id: I79ef0c0160894b0179ee347468d85a04809ef6fc Reviewed-by: Maurice Kalinowski <maurice.kalinowski@qt.io>
-rw-r--r--src/mqtt/qmqttconnection.cpp15
-rw-r--r--src/mqtt/qmqttconnection_p.h7
2 files changed, 16 insertions, 6 deletions
diff --git a/src/mqtt/qmqttconnection.cpp b/src/mqtt/qmqttconnection.cpp
index caf2be9..e0b9913 100644
--- a/src/mqtt/qmqttconnection.cpp
+++ b/src/mqtt/qmqttconnection.cpp
@@ -94,8 +94,6 @@ QByteArray QMqttConnection::readBufferTyped(qint64 *dataSize)
QMqttConnection::QMqttConnection(QObject *parent) : QObject(parent)
{
- m_pingTimer.setSingleShot(false);
- m_pingTimer.connect(&m_pingTimer, &QTimer::timeout, this, &QMqttConnection::sendControlPingRequest);
}
QMqttConnection::~QMqttConnection()
@@ -107,6 +105,16 @@ QMqttConnection::~QMqttConnection()
delete m_transport;
}
+void QMqttConnection::timerEvent(QTimerEvent *event)
+{
+ if (Q_LIKELY(event->timerId() == m_pingTimer.timerId())) {
+ sendControlPingRequest();
+ return;
+ }
+
+ QObject::timerEvent(event);
+}
+
void QMqttConnection::setTransport(QIODevice *device, QMqttClient::TransportType transport)
{
qCDebug(lcMqttConnection) << Q_FUNC_INFO << device << " Type:" << transport;
@@ -1462,8 +1470,7 @@ void QMqttConnection::finalize_connack()
m_internalState = BrokerConnected;
m_clientPrivate->setStateAndError(QMqttClient::Connected);
- m_pingTimer.setInterval(m_clientPrivate->m_keepAlive * 1000);
- m_pingTimer.start();
+ m_pingTimer.start(m_clientPrivate->m_keepAlive * 1000, this);
}
void QMqttConnection::finalize_suback()
diff --git a/src/mqtt/qmqttconnection_p.h b/src/mqtt/qmqttconnection_p.h
index 05b7ae4..8ae28a1 100644
--- a/src/mqtt/qmqttconnection_p.h
+++ b/src/mqtt/qmqttconnection_p.h
@@ -45,11 +45,11 @@
#include "qmqttcontrolpacket_p.h"
#include "qmqttmessage.h"
#include "qmqttsubscription.h"
+#include <QtCore/QBasicTimer>
#include <QtCore/QBuffer>
#include <QtCore/QHash>
#include <QtCore/QObject>
#include <QtCore/QSharedPointer>
-#include <QtCore/QTimer>
#include <QtCore/QtEndian>
QT_BEGIN_NAMESPACE
@@ -102,6 +102,9 @@ public Q_SLOTS:
void transportReadReady();
void transportError(QAbstractSocket::SocketError e);
+protected:
+ void timerEvent(QTimerEvent *event) override;
+
public:
QIODevice *m_transport{nullptr};
QMqttClient::TransportType m_transportType{QMqttClient::IODevice};
@@ -153,7 +156,7 @@ private:
QHash<quint16, QSharedPointer<QMqttControlPacket>> m_pendingMessages;
QHash<quint16, QSharedPointer<QMqttControlPacket>> m_pendingReleaseMessages;
InternalConnectionState m_internalState{BrokerDisconnected};
- QTimer m_pingTimer;
+ QBasicTimer m_pingTimer;
int m_pingTimeout{0};
QVector<QMqttTopicName> m_receiveAliases;