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 <QtNetwork/QUdpSocket>
4
6
7QGC_LOGGING_CATEGORY(UdpForwarderLog, "GPS.UdpForwarder")
8
9UdpForwarder::UdpForwarder(QObject* parent) : QObject(parent) {}
10
15
16bool UdpForwarder::configure(const QString& address, quint16 port)
17{
18 stop();
19
20 const QHostAddress parsed(address);
21 if (address.isEmpty() || parsed.isNull() || port == 0) {
22 qCWarning(UdpForwarderLog) << "Invalid UDP forward config:" << address << port;
23 return false;
24 }
25
26 _address = parsed;
27 _port = port;
28 _socket = new QUdpSocket(this);
29 _enabled = true;
30
31 qCDebug(UdpForwarderLog) << "UDP forwarding configured:" << address << ":" << port;
32 return true;
33}
34
35void UdpForwarder::forward(const QByteArray& data)
36{
37 if (!_enabled || !_socket || _port == 0) {
38 return;
39 }
40
41 // No rate limiting: RTCM runs 5-50 KB/s; writeDatagram()'s return value
42 // surfaces saturation if it ever becomes a real problem.
43 const qint64 sent = _socket->writeDatagram(data, _address, _port);
44 if (sent < 0) {
45 qCWarning(UdpForwarderLog) << "UDP forward failed:" << _socket->errorString();
46 }
47}
48
50{
51 if (_socket) {
52 _socket->close();
53 _socket->deleteLater();
54 _socket = nullptr;
55 }
56 _enabled = false;
57 _port = 0;
58 _address.clear();
59}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
~UdpForwarder() override
QString address() const
bool configure(const QString &address, quint16 port)
quint16 port() const
void forward(const QByteArray &data)