Skip to content
Snippets Groups Projects
Commit 90e41336 authored by Lorenz Meier's avatar Lorenz Meier
Browse files

Merge pull request #1374 from dogmaphobic/udpFix

UPD Link Fix
parents 42040794 3ba09b4d
No related branches found
No related tags found
No related merge requests found
...@@ -94,7 +94,6 @@ QString UDPLink::getName() const ...@@ -94,7 +94,6 @@ QString UDPLink::getName() const
void UDPLink::addHost(const QString& host) void UDPLink::addHost(const QString& host)
{ {
qDebug() << "UDP:" << "ADDING HOST:" << host;
_config->addHost(host); _config->addHost(host);
} }
...@@ -176,8 +175,7 @@ void UDPLink::readBytes() ...@@ -176,8 +175,7 @@ void UDPLink::readBytes()
// added to the list and will start receiving datagrams from here. Even a port scanner // added to the list and will start receiving datagrams from here. Even a port scanner
// would trigger this. // would trigger this.
// Add host to broadcast list if not yet present, or update its port // Add host to broadcast list if not yet present, or update its port
QString host(sender.toString() + ":" + QString("%1").arg((int)senderPort)); _config->addHost(sender.toString(), (int)senderPort);
_config->addHost(host);
} }
} }
...@@ -285,7 +283,6 @@ UDPConfiguration::UDPConfiguration(UDPConfiguration* source) : LinkConfiguration ...@@ -285,7 +283,6 @@ UDPConfiguration::UDPConfiguration(UDPConfiguration* source) : LinkConfiguration
void UDPConfiguration::copyFrom(LinkConfiguration *source) void UDPConfiguration::copyFrom(LinkConfiguration *source)
{ {
_confMutex.lock();
LinkConfiguration::copyFrom(source); LinkConfiguration::copyFrom(source);
UDPConfiguration* usource = dynamic_cast<UDPConfiguration*>(source); UDPConfiguration* usource = dynamic_cast<UDPConfiguration*>(source);
Q_ASSERT(usource != NULL); Q_ASSERT(usource != NULL);
...@@ -297,7 +294,6 @@ void UDPConfiguration::copyFrom(LinkConfiguration *source) ...@@ -297,7 +294,6 @@ void UDPConfiguration::copyFrom(LinkConfiguration *source)
addHost(host, port); addHost(host, port);
} while(usource->nextHost(host, port)); } while(usource->nextHost(host, port));
} }
_confMutex.unlock();
} }
/** /**
...@@ -305,7 +301,6 @@ void UDPConfiguration::copyFrom(LinkConfiguration *source) ...@@ -305,7 +301,6 @@ void UDPConfiguration::copyFrom(LinkConfiguration *source)
*/ */
void UDPConfiguration::addHost(const QString& host) void UDPConfiguration::addHost(const QString& host)
{ {
qDebug() << "UDP:" << "ADDING HOST:" << host;
if (host.contains(":")) if (host.contains(":"))
{ {
QHostInfo info = QHostInfo::fromName(host.split(":").first()); QHostInfo info = QHostInfo::fromName(host.split(":").first());
...@@ -316,33 +311,44 @@ void UDPConfiguration::addHost(const QString& host) ...@@ -316,33 +311,44 @@ void UDPConfiguration::addHost(const QString& host)
QHostAddress address; QHostAddress address;
for (int i = 0; i < hostAddresses.size(); i++) for (int i = 0; i < hostAddresses.size(); i++)
{ {
// Exclude loopback IPv4 and all IPv6 addresses // Exclude all IPv6 addresses
if (!hostAddresses.at(i).toString().contains(":")) if (!hostAddresses.at(i).toString().contains(":"))
{ {
address = hostAddresses.at(i); address = hostAddresses.at(i);
} }
} }
_confMutex.lock();
_hosts[address.toString()] = host.split(":").last().toInt(); _hosts[address.toString()] = host.split(":").last().toInt();
_confMutex.unlock();
qDebug() << "UDP:" << "ADDING HOST:" << address.toString() << ":" << host.split(":").last();
} }
} }
else else
{ {
QHostInfo info = QHostInfo::fromName(host); addHost(host, (int)_localPort);
if (info.error() == QHostInfo::NoError)
{
// Set port according to default (same as local port)
_hosts[info.addresses().first().toString()] = (int)_localPort;
}
} }
} }
void UDPConfiguration::addHost(const QString& host, int port) void UDPConfiguration::addHost(const QString& host, int port)
{ {
_hosts[host.trimmed()] = port; QMutexLocker locker(&_confMutex);
if(_hosts.contains(host)) {
if(_hosts[host] != port) {
_hosts[host] = port;
}
} else {
QHostInfo info = QHostInfo::fromName(host);
if (info.error() == QHostInfo::NoError)
{
_hosts[info.addresses().first().toString()] = port;
qDebug() << "UDP:" << "ADDING HOST:" << info.addresses().first().toString() << ":" << port;
}
}
} }
void UDPConfiguration::removeHost(const QString& host) void UDPConfiguration::removeHost(const QString& host)
{ {
QMutexLocker locker(&_confMutex);
QString tHost = host; QString tHost = host;
if (tHost.contains(":")) { if (tHost.contains(":")) {
tHost = tHost.split(":").first(); tHost = tHost.split(":").first();
...@@ -356,15 +362,19 @@ void UDPConfiguration::removeHost(const QString& host) ...@@ -356,15 +362,19 @@ void UDPConfiguration::removeHost(const QString& host)
bool UDPConfiguration::firstHost(QString& host, int& port) bool UDPConfiguration::firstHost(QString& host, int& port)
{ {
_confMutex.lock();
_it = _hosts.begin(); _it = _hosts.begin();
if(_it == _hosts.end()) { if(_it == _hosts.end()) {
_confMutex.unlock();
return false; return false;
} }
_confMutex.unlock();
return nextHost(host, port); return nextHost(host, port);
} }
bool UDPConfiguration::nextHost(QString& host, int& port) bool UDPConfiguration::nextHost(QString& host, int& port)
{ {
QMutexLocker locker(&_confMutex);
if(_it != _hosts.end()) { if(_it != _hosts.end()) {
host = _it.key(); host = _it.key();
port = _it.value(); port = _it.value();
...@@ -402,8 +412,9 @@ void UDPConfiguration::saveSettings(QSettings& settings, const QString& root) ...@@ -402,8 +412,9 @@ void UDPConfiguration::saveSettings(QSettings& settings, const QString& root)
void UDPConfiguration::loadSettings(QSettings& settings, const QString& root) void UDPConfiguration::loadSettings(QSettings& settings, const QString& root)
{ {
_confMutex.lock(); _confMutex.lock();
settings.beginGroup(root);
_hosts.clear(); _hosts.clear();
_confMutex.unlock();
settings.beginGroup(root);
_localPort = (quint16)settings.value("port", QGC_UDP_LOCAL_PORT).toUInt(); _localPort = (quint16)settings.value("port", QGC_UDP_LOCAL_PORT).toUInt();
int hostCount = settings.value("hostCount", 0).toInt(); int hostCount = settings.value("hostCount", 0).toInt();
for(int i = 0; i < hostCount; i++) { for(int i = 0; i < hostCount; i++) {
...@@ -414,7 +425,6 @@ void UDPConfiguration::loadSettings(QSettings& settings, const QString& root) ...@@ -414,7 +425,6 @@ void UDPConfiguration::loadSettings(QSettings& settings, const QString& root)
} }
} }
settings.endGroup(); settings.endGroup();
_confMutex.unlock();
} }
void UDPConfiguration::updateSettings() void UDPConfiguration::updateSettings()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment