QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
UdpForwarder.cc
Go to the documentation of this file.
1#include "UdpForwarder.h"
2
3#include <algorithm>
4
6
7QGC_LOGGING_CATEGORY(UdpForwarderLog, "Utilities.UdpForwarder")
8
9UdpForwarder::UdpForwarder(QObject* parent) : QObject(parent) {}
10
11bool UdpForwarder::configure(const QString& address, quint16 port)
12{
13 stop();
14
15 const QHostAddress parsed(address);
16 if (address.isEmpty() || parsed.isNull() || port == 0) {
17 qCWarning(UdpForwarderLog) << "Invalid UDP forward config:" << address << port;
18 return false;
19 }
20
21 _address = parsed;
22 _port = port;
23
24 qCDebug(UdpForwarderLog) << "UDP forwarding configured:" << address << ":" << port;
25 return true;
26}
27
28qint64 UdpForwarder::forward(const QByteArray& data)
29{
30 if (!isEnabled()) {
31 return 0;
32 }
33
34 // No rate limiting: RTCM runs 5-50 KB/s; writeDatagram()'s return value
35 // surfaces saturation if it ever becomes a real problem.
36 const qint64 sent = _socket.writeDatagram(data, _address, _port);
37 if (sent < 0) {
38 qCWarning(UdpForwarderLog) << "UDP forward failed:" << _socket.errorString();
39 }
40 return (std::max) (qint64{0}, sent);
41}
42
44{
45 _socket.close();
46 _port = 0;
47 _address.clear();
48}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
bool isEnabled() const
qint64 forward(const QByteArray &data)
QString address() const
bool configure(const QString &address, quint16 port)
quint16 port() const