From ee9517159a758f42f31eef8719995f3403731f62 Mon Sep 17 00:00:00 2001 From: Phil Taylor Date: Sat, 6 Feb 2021 10:54:20 +0000 Subject: [PATCH] Fix for pkt7 (ping) requests and improve rtt counter --- udphandler.cpp | 20 +++++++++++--------- udphandler.h | 9 +++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/udphandler.cpp b/udphandler.cpp index c55f4fc..30244e1 100644 --- a/udphandler.cpp +++ b/udphandler.cpp @@ -95,10 +95,11 @@ void udpHandler::DataReceived() } break; case (21): // pkt7, - if (r.mid(1, 5) == QByteArrayLiteral("\x00\x00\x00\x07\x00") && serialAndAudioOpened && lastPacket7Sent>0) + if (r.mid(1, 5) == QByteArrayLiteral("\x00\x00\x00\x07\x00") && r[16] == (char)0x01 && serialAndAudioOpened) { + //qDebug("Got response!"); // This is a response to our pkt7 request so measure latency (only once fully connected though. - latency += (time(NULL) - lastPacket7Sent); + latency += lastPacket7Sent.msecsTo(QDateTime::currentDateTime()); latency /= 2; emit haveNetworkStatus("rtt: " + QString::number(latency) + " ms"); } @@ -596,7 +597,7 @@ void udpAudio::DataReceived() lastReceivedSeq = gotSeq; } - for (uint16_t f = lastReceivedSeq + 1; f < gotSeq; f++) { + for (uint16_t f = lastReceivedSeq+1 ; f < gotSeq; f++) { // Do we need to request a retransmit? qDebug() << this->metaObject()->className() << ": Missing Sequence: (" << r.length() << ") " << f; } @@ -607,9 +608,9 @@ void udpAudio::DataReceived() // Delete contents of buffer up to existing pos() buffer->buffer().remove(0,buffer->pos()); // Seek to end of curent buffer - buffer->seek(buffer->size()); + buffer->seek(buffer->size()); // Append to end of buffer - buffer->write(r.mid(24).constData(), r.mid(24).length()); + buffer->write(r.mid(24).constData(), r.mid(24).length()); // Seek to start of buffer. buffer->seek(0); } @@ -750,7 +751,7 @@ void udpBase::SendPkt0Idle(bool tracked=true,quint16 seq=0) static_cast(remoteSID >> 24 & 0xff), static_cast(remoteSID >> 16 & 0xff), static_cast(remoteSID >> 8 & 0xff), static_cast(remoteSID & 0xff) }; - lastPacket0Sent = time(NULL); // Is this used? + lastPacket0Sent = QDateTime::currentDateTime(); // Is this used? if (!tracked) { p[6] = seq & 0xff; @@ -772,12 +773,13 @@ void udpBase::SendPkt7Idle() const unsigned char p[] = { 0x15, 0x00, 0x00, 0x00, 0x07, 0x00, static_cast(pkt7SendSeq & 0xff),static_cast(pkt7SendSeq >> 8 & 0xff), static_cast(localSID >> 24 & 0xff), static_cast(localSID >> 16 & 0xff), static_cast(localSID >> 8 & 0xff), static_cast(localSID & 0xff), static_cast(remoteSID >> 24 & 0xff), static_cast(remoteSID >> 16 & 0xff), static_cast(remoteSID >> 8 & 0xff), static_cast(remoteSID & 0xff), - static_cast(rand()),static_cast(innerSendSeq & 0xff),static_cast(innerSendSeq >> 8 & 0xff), 0x06 + 0x00, static_cast(rand()),static_cast(innerSendSeq & 0xff),static_cast(innerSendSeq >> 8 & 0xff), 0x06 }; - - lastPacket7Sent = time(NULL); + //qDebug() << this->metaObject()->className() << ": Send pkt7: " << QByteArray::fromRawData((const char*)p, sizeof(p)); + lastPacket7Sent = QDateTime::currentDateTime(); udp->writeDatagram(QByteArray::fromRawData((const char*)p, sizeof(p)), radioIP, port); pkt7SendSeq++; + innerSendSeq++; return; } diff --git a/udphandler.h b/udphandler.h index c929d64..4d95ae4 100644 --- a/udphandler.h +++ b/udphandler.h @@ -7,6 +7,7 @@ #include #include #include +#include // Allow easy endian-ness conversions #include @@ -45,16 +46,16 @@ public: char authID[6] = { 0, 0, 0, 0, 0, 0 }; char a8replyID[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; uint16_t authInnerSendSeq = 0; - uint16_t innerSendSeq = 0; + uint16_t innerSendSeq = 0x8304; // Not sure why? uint16_t sendSeqB = 0; uint16_t sendSeq = 1; uint16_t lastReceivedSeq = 0; uint16_t pkt0SendSeq = 0; uint16_t pkt7SendSeq = 0; uint16_t periodicSeq = 0; - time_t lastPacket0Sent = 0; - time_t lastPacket7Sent = 0; - int latency = 0; + QDateTime lastPacket0Sent; + QDateTime lastPacket7Sent; + quint64 latency = 0; QString username = ""; QString password = "";