Skip to content

Commit

Permalink
Comms: Speed up UDPLink Reception
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Dec 15, 2024
1 parent 1e8fc75 commit feaf7c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/Comms/UDPLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
QGC_LOGGING_CATEGORY(UDPLinkLog, "qgc.comms.udplink")

namespace {
constexpr int BUFFER_TRIGGER_SIZE = 10 * 1024;
constexpr int RECEIVE_TIME_LIMIT_MS = 50;

bool containsTarget(const QList<std::shared_ptr<UDPClient>> &list, const QHostAddress &address, quint16 port)
{
for (const std::shared_ptr<UDPClient> &target : list) {
Expand Down Expand Up @@ -273,6 +276,9 @@ bool UDPWorker::isConnected() const

void UDPWorker::setupSocket()
{
const QList<QHostAddress> localAddresses = QNetworkInterface::allAddresses();
_localAddresses = QSet(localAddresses.constBegin(), localAddresses.constEnd());

_socket->setProxy(QNetworkProxy::NoProxy);

(void) connect(_socket, &QUdpSocket::connected, this, &UDPWorker::_onSocketConnected);
Expand Down Expand Up @@ -352,11 +358,6 @@ void UDPWorker::disconnectLink()

void UDPWorker::writeData(const QByteArray &data)
{
if (!_socket->isValid()) {
emit errorOccurred(tr("Could Not Send Data - Socket is Invalid!"));
return;
}

if (!isConnected()) {
emit errorOccurred(tr("Could Not Send Data - Link is Disconnected!"));
return;
Expand Down Expand Up @@ -401,11 +402,6 @@ void UDPWorker::_onSocketDisconnected()

void UDPWorker::_onSocketReadyRead()
{
if (!_socket->isValid()) {
emit errorOccurred(tr("Socket is Invalid!"));
return;
}

if (!isConnected()) {
emit errorOccurred(tr("Could Not Read Data - Link is Disconnected!"));
return;
Expand All @@ -418,6 +414,7 @@ void UDPWorker::_onSocketReadyRead()
}

QByteArray buffer;
buffer.reserve(BUFFER_TRIGGER_SIZE);
QElapsedTimer timer;
timer.start();
while (_socket->hasPendingDatagrams()) {
Expand All @@ -428,20 +425,19 @@ void UDPWorker::_onSocketReadyRead()

(void) buffer.append(datagramIn.data());

if (buffer.size() > (10 * 1024) || (timer.elapsed() > 50)) {
if ((buffer.size() > BUFFER_TRIGGER_SIZE) || (timer.elapsed() > RECEIVE_TIME_LIMIT_MS)) {
emit dataReceived(buffer);
buffer.clear();
(void) timer.restart();
}

const QList<QHostAddress> localAddresses = QNetworkInterface::allAddresses();
const bool ipLocal = datagramIn.senderAddress().isLoopback() || localAddresses.contains(datagramIn.senderAddress());
const bool ipLocal = datagramIn.senderAddress().isLoopback() || _localAddresses.contains(datagramIn.senderAddress());
const QHostAddress senderAddress = ipLocal ? QHostAddress(QHostAddress::SpecialAddress::LocalHost) : datagramIn.senderAddress();

QMutexLocker locker(&_sessionTargetsMutex);
if (!containsTarget(_sessionTargets, senderAddress, datagramIn.senderPort())) {
qCDebug(UDPLinkLog) << "UDP Adding target:" << senderAddress << datagramIn.senderPort();
(void) _sessionTargets.append(std::make_shared<UDPClient>(senderAddress, datagramIn.senderPort()));
_sessionTargets.append(std::make_shared<UDPClient>(senderAddress, datagramIn.senderPort()));
}
locker.unlock();
}
Expand Down
1 change: 1 addition & 0 deletions src/Comms/UDPLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private slots:
QMutex _sessionTargetsMutex;
QList<std::shared_ptr<UDPClient>> _sessionTargets;
bool _isConnected = false;
QSet<QHostAddress> _localAddresses;

static const QHostAddress _multicastGroup;

Expand Down

0 comments on commit feaf7c5

Please sign in to comment.