Commit 765dfbbd authored by dogmaphobic's avatar dogmaphobic

Make the fix dependent on the Qt version.

parent ac7e80d6
......@@ -28,6 +28,13 @@ This file is part of the QGROUNDCONTROL project
*
*/
#include <QtGlobal>
#if QT_VERSION > 0x050401
#define UDP_BROKEN_SIGNAL 1
#else
#define UDP_BROKEN_SIGNAL 0
#endif
#include <QTimer>
#include <QList>
#include <QDebug>
......@@ -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");
......
......@@ -219,7 +219,8 @@ private:
QMutex _mutex;
QQueue<QByteArray*> _outQueue;
bool _writeBytes ();
bool _dequeBytes ();
void _sendBytes (const char* data, qint64 size);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment