Browse Source

Merge pull request #8939 from DonLakeFlyer/UDPMutexMaster

Add mutex for _sessionTargets access. Remove range_for usage. General…
QGC4.4
Don Gagne 5 years ago committed by GitHub
parent
commit
348feb18dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 58
      src/comm/UDPLink.cc
  2. 4
      src/comm/UDPLink.h

58
src/comm/UDPLink.cc

@ -7,14 +7,6 @@ @@ -7,14 +7,6 @@
*
****************************************************************************/
/**
* @file
* @brief Definition of UDP connection (server) for unmanned vehicles
* @author Lorenz Meier <mavteam@student.ethz.ch>
*
*/
#include <QtGlobal>
#include <QTimer>
#include <QList>
@ -31,15 +23,12 @@ @@ -31,15 +23,12 @@
#include "SettingsManager.h"
#include "AutoConnectSettings.h"
#define REMOVE_GONE_HOSTS 0
static const char* kZeroconfRegistration = "_qgroundcontrol._udp";
static bool is_ip(const QString& address)
{
int a,b,c,d;
if (sscanf(address.toStdString().c_str(), "%d.%d.%d.%d", &a, &b, &c, &d) != 4
&& strcmp("::1", address.toStdString().c_str())) {
if (sscanf(address.toStdString().c_str(), "%d.%d.%d.%d", &a, &b, &c, &d) != 4 && strcmp("::1", address.toStdString().c_str())) {
return false;
} else {
return true;
@ -48,28 +37,27 @@ static bool is_ip(const QString& address) @@ -48,28 +37,27 @@ static bool is_ip(const QString& address)
static QString get_ip_address(const QString& address)
{
if(is_ip(address))
if (is_ip(address)) {
return address;
}
// Need to look it up
QHostInfo info = QHostInfo::fromName(address);
if (info.error() == QHostInfo::NoError)
{
if (info.error() == QHostInfo::NoError) {
QList<QHostAddress> hostAddresses = info.addresses();
for (int i = 0; i < hostAddresses.size(); i++)
{
for (int i=0; i<hostAddresses.size(); i++) {
// Exclude all IPv6 addresses
if (!hostAddresses.at(i).toString().contains(":"))
{
if (!hostAddresses.at(i).toString().contains(":")) {
return hostAddresses.at(i).toString();
}
}
}
return {};
return QString();
}
static bool contains_target(const QList<UDPCLient*> list, const QHostAddress& address, quint16 port)
{
for(UDPCLient* target: list) {
for (int i=0; i<list.count(); i++) {
UDPCLient* target = list[i];
if (target->address == address && target->port == port) {
return true;
}
@ -90,8 +78,10 @@ UDPLink::UDPLink(SharedLinkConfigurationPointer& config) @@ -90,8 +78,10 @@ UDPLink::UDPLink(SharedLinkConfigurationPointer& config)
if (!_udpConfig) {
qWarning() << "Internal error";
}
for (const QHostAddress &address: QNetworkInterface::allAddresses()) {
_localAddress.append(QHostAddress(address));
auto allAddresses = QNetworkInterface::allAddresses();
for (int i=0; i<allAddresses.count(); i++) {
QHostAddress &address = allAddresses[i];
_localAddresses.append(QHostAddress(address));
}
moveToThread(this);
}
@ -127,8 +117,7 @@ void UDPLink::run() @@ -127,8 +117,7 @@ void UDPLink::run()
void UDPLink::_restartConnection()
{
if(this->isConnected())
{
if (this->isConnected()) {
_disconnect();
_connect();
}
@ -155,7 +144,8 @@ bool UDPLink::_isIpLocal(const QHostAddress& add) @@ -155,7 +144,8 @@ bool UDPLink::_isIpLocal(const QHostAddress& add)
// On Windows, this is a very expensive call only Redmond would know
// why. As such, we make it once and keep the list locally. If a new
// interface shows up after we start, it won't be on this list.
for (const QHostAddress &address: _localAddress) {
for (int i=0; i<_localAddresses.count(); i++) {
QHostAddress &address = _localAddresses[i];
if (address == add) {
// This is a local address of the same host
return true;
@ -267,8 +257,7 @@ void UDPLink::_disconnect(void) @@ -267,8 +257,7 @@ void UDPLink::_disconnect(void)
**/
bool UDPLink::_connect(void)
{
if(this->isRunning() || _running)
{
if (this->isRunning() || _running) {
_running = false;
quit();
wait();
@ -408,7 +397,8 @@ void UDPConfiguration::_copyFrom(LinkConfiguration *source) @@ -408,7 +397,8 @@ void UDPConfiguration::_copyFrom(LinkConfiguration *source)
if (usource) {
_localPort = usource->localPort();
_clearTargetHosts();
for(UDPCLient* target: usource->targetHosts()) {
for (int i=0; i<usource->targetHosts().count(); i++) {
UDPCLient* target = usource->targetHosts()[i];
if(!contains_target(_targetHosts, target->address, target->port)) {
UDPCLient* newTarget = new UDPCLient(target);
_targetHosts.append(newTarget);
@ -432,13 +422,10 @@ void UDPConfiguration::_clearTargetHosts() @@ -432,13 +422,10 @@ void UDPConfiguration::_clearTargetHosts()
void UDPConfiguration::addHost(const QString host)
{
// Handle x.x.x.x:p
if (host.contains(":"))
{
if (host.contains(":")) {
addHost(host.split(":").first(), host.split(":").last().toUInt());
}
} else {
// If no port, use default
else
{
addHost(host, _localPort);
}
}
@ -460,8 +447,7 @@ void UDPConfiguration::addHost(const QString& host, quint16 port) @@ -460,8 +447,7 @@ void UDPConfiguration::addHost(const QString& host, quint16 port)
void UDPConfiguration::removeHost(const QString host)
{
if (host.contains(":"))
{
if (host.contains(":")) {
QHostAddress address = QHostAddress(get_ip_address(host.split(":").first()));
quint16 port = host.split(":").last().toUInt();
for (int i=0; i<_targetHosts.size(); i++) {

4
src/comm/UDPLink.h

@ -209,7 +209,7 @@ private: @@ -209,7 +209,7 @@ private:
UDPConfiguration* _udpConfig;
bool _connectState;
QList<UDPCLient*> _sessionTargets;
QList<QHostAddress> _localAddress;
QMutex _sessionTargetsMutex;
QList<QHostAddress> _localAddresses;
};

Loading…
Cancel
Save