From 765dfbbdef5c463ca2cb9219ed07b3d66c14e6c2 Mon Sep 17 00:00:00 2001 From: dogmaphobic Date: Sat, 27 Jun 2015 08:01:02 -0400 Subject: [PATCH] Make the fix dependent on the Qt version. --- src/comm/UDPLink.cc | 110 +++++++++++++++++++++++++++----------------- src/comm/UDPLink.h | 3 +- 2 files changed, 71 insertions(+), 42 deletions(-) diff --git a/src/comm/UDPLink.cc b/src/comm/UDPLink.cc index 3fae15920..5a9b15d90 100644 --- a/src/comm/UDPLink.cc +++ b/src/comm/UDPLink.cc @@ -28,6 +28,13 @@ This file is part of the QGROUNDCONTROL project * */ +#include +#if QT_VERSION > 0x050401 +#define UDP_BROKEN_SIGNAL 1 +#else +#define UDP_BROKEN_SIGNAL 0 +#endif + #include #include #include @@ -100,6 +107,7 @@ UDPLink::~UDPLink() _disconnect(); // Tell the thread to exit _running = false; + quit(); // Wait for it to exit wait(); while(_outQueue.count() > 0) { @@ -115,13 +123,24 @@ UDPLink::~UDPLink() void UDPLink::run() { if(_hardwareConnect()) { - while(true) { - readBytes(); - if(_writeBytes() && _running) - continue; - if(!_running) - break; - this->msleep(50); + if(UDP_BROKEN_SIGNAL) { + bool loop = false; + while(true) { + //-- Anything to read? + loop = _socket->hasPendingDatagrams(); + if(loop) { + readBytes(); + } + //-- Loop right away if busy + if((_dequeBytes() || loop) && _running) + continue; + if(!_running) + break; + //-- Settle down (it gets here if there is nothing to read or write) + this->msleep(50); + } + } else { + exec(); } } if (_socket) { @@ -154,51 +173,58 @@ void UDPLink::removeHost(const QString& host) _config->removeHost(host); } -#define UDPLINK_DEBUG 0 - void UDPLink::writeBytes(const char* data, qint64 size) { if (!_socket) { return; } - QByteArray* qdata = new QByteArray(data, size); - QMutexLocker lock(&_mutex); - _outQueue.enqueue(qdata); + if(UDP_BROKEN_SIGNAL) { + QByteArray* qdata = new QByteArray(data, size); + QMutexLocker lock(&_mutex); + _outQueue.enqueue(qdata); + } else { + _sendBytes(data, size); + } } -bool UDPLink::_writeBytes() +bool UDPLink::_dequeBytes() { QMutexLocker lock(&_mutex); if(_outQueue.count() > 0) { QByteArray* qdata = _outQueue.dequeue(); lock.unlock(); - QStringList goneHosts; - // Send to all connected systems - QString host; - int port; - if(_config->firstHost(host, port)) { - do { - QHostAddress currentHost(host); - if(_socket->writeDatagram(qdata->data(), qdata->size(), currentHost, (quint16)port) < 0) { - // This host is gone. Add to list to be removed - goneHosts.append(host); - } - // Log the amount and time written out for future data rate calculations. - QMutexLocker dataRateLocker(&dataRateMutex); - logDataRateToBuffer(outDataWriteAmounts, outDataWriteTimes, &outDataIndex, qdata->size(), QDateTime::currentMSecsSinceEpoch()); - } while (_config->nextHost(host, port)); - //-- Remove hosts that are no longer there - foreach (QString ghost, goneHosts) { - _config->removeHost(ghost); - } - } + _sendBytes(qdata->data(), qdata->size()); delete qdata; lock.relock(); } return (_outQueue.count() > 0); } +void UDPLink::_sendBytes(const char* data, qint64 size) +{ + QStringList goneHosts; + // Send to all connected systems + QString host; + int port; + if(_config->firstHost(host, port)) { + do { + QHostAddress currentHost(host); + if(_socket->writeDatagram(data, size, currentHost, (quint16)port) < 0) { + // This host is gone. Add to list to be removed + goneHosts.append(host); + } + // Log the amount and time written out for future data rate calculations. + QMutexLocker dataRateLocker(&dataRateMutex); + logDataRateToBuffer(outDataWriteAmounts, outDataWriteTimes, &outDataIndex, size, QDateTime::currentMSecsSinceEpoch()); + } while (_config->nextHost(host, port)); + //-- Remove hosts that are no longer there + foreach (QString ghost, goneHosts) { + _config->removeHost(ghost); + } + } +} + /** * @brief Read a number of bytes from the interface. **/ @@ -235,7 +261,7 @@ void UDPLink::readBytes() // would trigger this. // Add host to broadcast list if not yet present, or update its port _config->addHost(sender.toString(), (int)senderPort); - if(!_running) + if(UDP_BROKEN_SIGNAL && !_running) break; } } @@ -248,6 +274,7 @@ void UDPLink::readBytes() bool UDPLink::_disconnect(void) { _running = false; + quit(); wait(); if (_socket) { // Make sure delete happen on correct thread @@ -255,9 +282,8 @@ bool UDPLink::_disconnect(void) _socket = NULL; emit disconnected(); } - // TODO When would this ever return false? _connectState = false; - return !_connectState; + return true; } /** @@ -267,17 +293,15 @@ bool UDPLink::_disconnect(void) **/ bool UDPLink::_connect(void) { - if(this->isRunning()) + if(this->isRunning() || _running) { _running = false; + quit(); wait(); } - // TODO When would this ever return false? - bool connected = true; - // I see no reason to run this in "HighPriority" _running = true; start(NormalPriority); - return connected; + return true; } bool UDPLink::_hardwareConnect() @@ -292,6 +316,10 @@ bool UDPLink::_hardwareConnect() _connectState = _socket->bind(host, _config->localPort(), QAbstractSocket::ReuseAddressHint); if (_connectState) { _registerZeroconf(_config->localPort(), kZeroconfRegistration); + //-- Connect signal if this version of Qt is not broken + if(!UDP_BROKEN_SIGNAL) { + QObject::connect(_socket, SIGNAL(readyRead()), this, SLOT(readBytes())); + } emit connected(); } else { emit communicationError("UDP Link Error", "Error binding UDP port"); diff --git a/src/comm/UDPLink.h b/src/comm/UDPLink.h index 4c2bd258d..d83058a91 100644 --- a/src/comm/UDPLink.h +++ b/src/comm/UDPLink.h @@ -219,7 +219,8 @@ private: QMutex _mutex; QQueue _outQueue; - bool _writeBytes (); + bool _dequeBytes (); + void _sendBytes (const char* data, qint64 size); }; -- 2.22.0