Enroute Flight Navigation
A navigation app for VFR pilots
TrafficDataSource_Udp.h
1/***************************************************************************
2 * Copyright (C) 2021-2026 by Stefan Kebekus *
3 * stefan.kebekus@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#pragma once
22
23#include <QElapsedTimer>
24#include <QPointer>
25#include <QUdpSocket>
26
27#include "traffic/TrafficDataSource_AbstractSocket.h"
28
29#include <optional>
30
31using namespace Qt::Literals::StringLiterals;
32
33
34namespace Traffic {
35
43 Q_OBJECT
44
45public:
54 TrafficDataSource_Udp(bool isCanonical, quint16 port, QObject *parent = nullptr);
55
56 // Standard destructor
57 ~TrafficDataSource_Udp() override;
58
59
60
61 //
62 // Properties
63 //
64
67 Q_PROPERTY(quint16 port READ port CONSTANT)
68
69
70
71 //
72 // Getter Methods
73 //
74
75
79 [[nodiscard]] QString dataFormat() const override { return u"GDL90, XGPS"_s; }
80
88 [[nodiscard]] QString icon() const override { return u"/icons/material/ic_wifi.svg"_s; }
89
97 [[nodiscard]] auto sourceName() const -> QString override
98 {
99 return tr("UDP connection to port %1").arg(m_port);
100 }
101
106 [[nodiscard]] quint16 port() const
107 {
108 return m_port;
109 }
110
111
112public slots:
119
126
127private slots:
128 // Read messages from the socket datagrams and passes them on for
129 // processing: XGPS/XTRA datagrams are routed to processXGPSString, all
130 // other messages are passed on to processGDLMessage.
131 void onReadyRead();
132
133private:
134 Q_DISABLE_COPY_MOVE(TrafficDataSource_Udp)
135
136 QPointer<QUdpSocket> m_socket;
137 quint16 m_port;
138
139 // The same datagram is often delivered on several network interfaces. To
140 // sort out these duplicates, we store the hashes of recently received
141 // datagrams together with their arrival time in a circular array. A
142 // datagram counts as a duplicate only if an identical one arrived within
143 // the last duplicateWindowMs milliseconds. Without the time limit,
144 // legitimately recurring datagrams (e.g. XGPS reports from a stationary
145 // source, which carry no timestamp) would be suppressed forever.
146 // The cache is cleared on every (re)connect. The nextHashIndex points to
147 // the next array entry that will be re-written.
148 struct DatagramRecord
149 {
150 size_t hash = 0;
151 qint64 arrivalMs = 0;
152 };
153 static constexpr qint64 duplicateWindowMs = 500;
154 QVector<std::optional<DatagramRecord>> receivedDatagrams = QVector<std::optional<DatagramRecord>>(512, std::nullopt);
155 qsizetype nextHashIndex = 0;
156 QElapsedTimer m_arrivalClock;
157
158};
159
160} // namespace Traffic
TrafficDataSource_AbstractSocket(bool isCanonical, QObject *parent)
Default constructor.
void disconnectFromTrafficReceiver() override
Disconnect from traffic receiver.
auto sourceName() const -> QString override
Getter function for the property with the same name.
TrafficDataSource_Udp(bool isCanonical, quint16 port, QObject *parent=nullptr)
Default constructor.
QString dataFormat() const override
Getter function for the property with the same name.
void connectToTrafficReceiver() override
Start attempt to connect to traffic receiver.
quint16 port() const
Getter function for the property with the same name.
QString icon() const override
Getter function for the property with the same name.