From 7ff3b860826352fd1d80192116612b25a9f97d3b Mon Sep 17 00:00:00 2001 From: Phil Taylor Date: Fri, 11 Jun 2021 10:38:08 +0100 Subject: [PATCH] Use watchdog to cleanup lost server connections --- udpserver.cpp | 27 +++++++++++++++++---------- udpserver.h | 2 +- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/udpserver.cpp b/udpserver.cpp index 8db7d85..7cf5b5d 100644 --- a/udpserver.cpp +++ b/udpserver.cpp @@ -217,8 +217,8 @@ void udpServer::controlReceived() current->idleTimer->start(100); current->wdTimer = new QTimer(); - connect(current->wdTimer, &QTimer::timeout, this, std::bind(&udpServer::watchdog, this, current)); - //current->wdTimer->start(1000); + connect(current->wdTimer, &QTimer::timeout, this, std::bind(&udpServer::watchdog, this, &controlClients, current)); + current->wdTimer->start(1000); current->retransmitTimer = new QTimer(); connect(current->retransmitTimer, &QTimer::timeout, this, std::bind(&udpServer::sendRetransmitRequest, this, current)); @@ -503,8 +503,8 @@ void udpServer::civReceived() //current->idleTimer->start(100); // Start idleTimer after receiving iamready. current->wdTimer = new QTimer(); - connect(current->wdTimer, &QTimer::timeout, this, std::bind(&udpServer::watchdog, this, current)); - //current->wdTimer->start(1000); + connect(current->wdTimer, &QTimer::timeout, this, std::bind(&udpServer::watchdog, this, &civClients, current)); + current->wdTimer->start(1000); current->retransmitTimer = new QTimer(); connect(current->retransmitTimer, &QTimer::timeout, this, std::bind(&udpServer::sendRetransmitRequest, this, current)); @@ -649,8 +649,8 @@ void udpServer::audioReceived() current->pingTimer->start(100); current->wdTimer = new QTimer(); - connect(current->wdTimer, &QTimer::timeout, this, std::bind(&udpServer::watchdog, this, current)); - //current->wdTimer->start(1000); + connect(current->wdTimer, &QTimer::timeout, this, std::bind(&udpServer::watchdog, this, &audioClients, current)); + current->wdTimer->start(1000); current->retransmitTimer = new QTimer(); connect(current->retransmitTimer, &QTimer::timeout, this, std::bind(&udpServer::sendRetransmitRequest, this, current)); @@ -920,7 +920,7 @@ void udpServer::sendControl(CLIENT* c, quint8 type, quint16 seq) void udpServer::sendPing(QList* l, CLIENT* c, quint16 seq, bool reply) { - // Also use to detect "stale" connections + /* QDateTime now = QDateTime::currentDateTime(); if (c->lastHeard.secsTo(now) > STALE_CONNECTION) @@ -930,6 +930,7 @@ void udpServer::sendPing(QList* l, CLIENT* c, quint16 seq, bool reply) return; } + */ //qInfo(logUdpServer()) << c->ipAddress.toString() << ": Sending Ping"; @@ -1233,10 +1234,16 @@ void udpServer::sendTokenResponse(CLIENT* c, quint8 type) #define PURGE_SECONDS 60 -void udpServer::watchdog(CLIENT* c) +void udpServer::watchdog(QList* l, CLIENT* c) { - Q_UNUSED(c); - // Do something! + QDateTime now = QDateTime::currentDateTime(); + + if (c->lastHeard.secsTo(now) > STALE_CONNECTION) + { + qInfo(logUdpServer()) << c->ipAddress.toString() << "(" << c->type << "): Deleting stale connection "; + deleteConnection(l, c); + return; + } } diff --git a/udpserver.h b/udpserver.h index 26252d7..76da64e 100644 --- a/udpserver.h +++ b/udpserver.h @@ -135,7 +135,7 @@ private: void sendTokenResponse(CLIENT* c,quint8 type); void sendStatus(CLIENT* c); void sendRetransmitRequest(CLIENT* c); - void watchdog(CLIENT* c); + void watchdog(QList* l, CLIENT* c); void sendRxAudio(); void deleteConnection(QList *l, CLIENT* c);