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 <QPointer>
24#include <QUdpSocket>
25
26#include "traffic/TrafficDataSource_AbstractSocket.h"
27
28#include <optional>
29
30using namespace Qt::Literals::StringLiterals;
31
32
33namespace Traffic {
34
42 Q_OBJECT
43
44public:
53 TrafficDataSource_Udp(bool isCanonical, quint16 port, QObject *parent = nullptr);
54
55 // Standard destructor
56 ~TrafficDataSource_Udp() override;
57
58
59
60 //
61 // Properties
62 //
63
66 Q_PROPERTY(quint16 port READ port CONSTANT)
67
68
69
70 //
71 // Getter Methods
72 //
73
74
78 [[nodiscard]] QString dataFormat() const override { return u"GDL90, XGPS"_s; }
79
87 [[nodiscard]] QString icon() const override { return u"/icons/material/ic_wifi.svg"_s; }
88
96 [[nodiscard]] auto sourceName() const -> QString override
97 {
98 return tr("UDP connection to port %1").arg(m_port);
99 }
100
105 [[nodiscard]] quint16 port() const
106 {
107 return m_port;
108 }
109
110
111public slots:
118
125
126private slots:
127 // Read messages from the socket datagrams and passes them on for
128 // processing: XGPS/XTRA datagrams are routed to processXGPSString, all
129 // other messages are passed on to processGDLMessage.
130 void onReadyRead();
131
132private:
133 Q_DISABLE_COPY_MOVE(TrafficDataSource_Udp)
134
135 QPointer<QUdpSocket> m_socket;
136 quint16 m_port;
137
138 // We use this vector to store the last 512 datagram hashes in a circular
139 // array. This is used to sort out doubly sent datagrams. The nextHashIndex
140 // points to the next vector entry that will be re-written. Empty slots hold
141 // std::nullopt, which can never equal a real hash — so a datagram that
142 // legitimately hashes to 0 is not mistaken for an as-yet-unwritten slot.
143 // Note: use parenthesis-initialization here — brace-init would select the
144 // std::initializer_list constructor and create a two-element vector.
145 QVector<std::optional<size_t>> receivedDatagramHashes = QVector<std::optional<size_t>>(512, std::nullopt);
146 qsizetype nextHashIndex = 0;
147
148};
149
150} // 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.