examples/network/network-chat: use QBasicTimer instead of raw timer IDs

Change-Id: I6ea464a6d19c775edca187fea1d7cd82f5f22d3f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2024-09-04 17:21:39 +03:00
parent f4076f1058
commit 401a840adb
2 changed files with 9 additions and 10 deletions

View File

@ -6,6 +6,8 @@
#include <QTimerEvent> #include <QTimerEvent>
using namespace std::chrono_literals;
static const int TransferTimeout = 30 * 1000; static const int TransferTimeout = 30 * 1000;
static const int PongTimeout = 60 * 1000; static const int PongTimeout = 60 * 1000;
static const int PingInterval = 5 * 1000; static const int PingInterval = 5 * 1000;
@ -85,10 +87,9 @@ bool Connection::sendMessage(const QString &message)
void Connection::timerEvent(QTimerEvent *timerEvent) void Connection::timerEvent(QTimerEvent *timerEvent)
{ {
if (timerEvent->timerId() == transferTimerId) { if (timerEvent->matches(transferTimer)) {
abort(); abort();
killTimer(transferTimerId); transferTimer.stop();
transferTimerId = -1;
} }
} }
@ -159,10 +160,7 @@ void Connection::processReadyRead()
// Next state: no command read // Next state: no command read
reader.leaveContainer(); reader.leaveContainer();
if (transferTimerId != -1) { transferTimer.stop();
killTimer(transferTimerId);
transferTimerId = -1;
}
processData(); processData();
} }
@ -171,8 +169,8 @@ void Connection::processReadyRead()
if (reader.lastError() != QCborError::EndOfFile) if (reader.lastError() != QCborError::EndOfFile)
abort(); // parse error abort(); // parse error
if (transferTimerId != -1 && reader.containerDepth() > 1) if (transferTimer.isActive() && reader.containerDepth() > 1)
transferTimerId = startTimer(TransferTimeout); transferTimer.start(TransferTimeout * 1ms, this);
} }
void Connection::sendPing() void Connection::sendPing()

View File

@ -4,6 +4,7 @@
#ifndef CONNECTION_H #ifndef CONNECTION_H
#define CONNECTION_H #define CONNECTION_H
#include <QBasicTimer>
#include <QCborStreamReader> #include <QCborStreamReader>
#include <QCborStreamWriter> #include <QCborStreamWriter>
#include <QElapsedTimer> #include <QElapsedTimer>
@ -69,7 +70,7 @@ private:
QByteArray peerUniqueId; QByteArray peerUniqueId;
ConnectionState state = WaitingForGreeting; ConnectionState state = WaitingForGreeting;
DataType currentDataType = Undefined; DataType currentDataType = Undefined;
int transferTimerId = -1; QBasicTimer transferTimer;
bool isGreetingMessageSent = false; bool isGreetingMessageSent = false;
}; };