swift
clientconnection.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_CORE_AFV_CONNECTION_CLIENTCONNECTION_H
7 #define SWIFT_CORE_AFV_CONNECTION_CLIENTCONNECTION_H
8 
9 #include <QObject>
10 #include <QString>
11 #include <QTimer>
12 #include <QUdpSocket>
13 #include <QUuid>
14 
18 #include "core/afv/dto.h"
19 #include "misc/verify.h"
20 
21 namespace swift::core::afv::connection
22 {
24  class CClientConnection : public QObject
25  {
26  Q_OBJECT
27 
28  public:
31  {
34  };
35  Q_ENUM(ConnectionStatus)
36 
37 
38  CClientConnection(const QString &apiServer, QObject *parent = nullptr);
39 
42  void connectTo(const QString &userName, const QString &password, const QString &callsign, const QString &client,
43  ConnectionCallback callback);
44 
46  void disconnectFrom(const QString &reason = {});
47 
49  bool isConnected() const { return m_connection.isConnected(); }
50 
52  bool isVoiceServerAlive() const { return m_connection.isVoiceServerAlive(); }
53 
56  void setReceiveAudio(bool value) { m_connection.setReceiveAudio(value); }
57  bool receiveAudio() const { return m_connection.isReceivingAudio(); }
58  bool receiveAudioDto() const { return m_receiveAudioDto; }
59  void setReceiveAudioDto(bool receiveAudioDto) { m_receiveAudioDto = receiveAudioDto; }
61 
63  template <typename T>
64  void sendToVoiceServer(const T &dto)
65  {
66  if (!m_connection.m_voiceCryptoChannel || !m_udpSocket)
67  {
68  SWIFT_VERIFY_X(false, Q_FUNC_INFO, "sendVoice used without crypto channel or socket");
69  return;
70  }
71  const QUrl voiceServerUrl("udp://" + m_connection.getTokens().VoiceServer.addressIpV4);
72  const QByteArray dataBytes = crypto::CryptoDtoSerializer::serialize(
73  *m_connection.m_voiceCryptoChannel, crypto::CryptoDtoMode::AEAD_ChaCha20Poly1305, dto);
74  m_udpSocket->writeDatagram(dataBytes, QHostAddress(voiceServerUrl.host()),
75  static_cast<quint16>(voiceServerUrl.port()));
76  }
77 
79  void updateTransceivers(const QString &callsign, const QVector<TransceiverDto> &transceivers);
80 
82  QVector<StationDto> getAllAliasedStations();
83 
85  bool updateVoiceServerUrl(const QString &url);
86 
88  const QString &getVoiceServerUrl() const;
89 
91  qint64 secondsSinceAuthentication() const { return m_connection.secondsSinceAuthentication(); }
92 
95  const QString &getUserName() const { return m_connection.getUserName(); }
96  const QString &getCallsign() const { return m_connection.getCallsign(); }
97  const QString &getPassword() const
98  {
99  static const QString e;
100  return m_apiServerConnection ? m_apiServerConnection->getPassword() : e;
101  }
102  const QString &getClient() const
103  {
104  static const QString e;
105  return m_apiServerConnection ? m_apiServerConnection->getClient() : e;
106  }
107  const QUuid &getNetworkVersion() const { return m_networkVersion; }
109 
110  signals:
113 
114  private:
115  void connectToVoiceServer();
116  void disconnectFromVoiceServer();
117 
118  void readPendingDatagrams();
119  void processMessage(const QByteArray &messageDdata, bool loopback = false);
120  void handleSocketError(QAbstractSocket::SocketError error);
121 
122  void voiceServerHeartbeat();
123 
124  const QUuid m_networkVersion = QUuid("3a5ddc6d-cf5d-4319-bd0e-d184f772db80");
125 
126  // Data
127  CClientConnectionData m_connection;
128 
129  // Voice server
130  QUdpSocket *m_udpSocket = nullptr;
131  QTimer *m_voiceServerTimer = nullptr;
132 
133  // API server
134  CApiServerConnection *m_apiServerConnection = nullptr;
135 
136  // Properties
137  bool m_receiveAudioDto = true;
138  };
139 } // namespace swift::core::afv::connection
140 
141 #endif // SWIFT_CORE_AFV_CONNECTION_CLIENTCONNECTION_H
const PostCallsignResponseDto & getTokens() const
Tokens.
qint64 secondsSinceAuthentication() const
Time since authentication.
QScopedPointer< crypto::CCryptoDtoChannel > m_voiceCryptoChannel
used crypto channel
void setReceiveAudio(bool receive)
Receiving audio?
const QString & getUserName() const
User data.
void sendToVoiceServer(const T &dto)
Send voice DTO to server.
void updateTransceivers(const QString &callsign, const QVector< TransceiverDto > &transceivers)
Update transceivers.
CClientConnection(const QString &apiServer, QObject *parent=nullptr)
Ctor.
bool updateVoiceServerUrl(const QString &url)
Update the voice server URL.
void disconnectFrom(const QString &reason={})
Disconnect.
const QString & getPassword() const
User data.
void audioReceived(const AudioRxOnTransceiversDto &dto)
Audio has been received.
QVector< StationDto > getAllAliasedStations()
All aliased stations.
void setReceiveAudio(bool value)
Receiving audio?
const QUuid & getNetworkVersion() const
User data.
const QString & getCallsign() const
User data.
const QString & getVoiceServerUrl() const
Get the voice server URL.
qint64 secondsSinceAuthentication() const
Authenticated since when.
void setReceiveAudioDto(bool receiveAudioDto)
Receiving audio?
const QString & getClient() const
User data.
void connectTo(const QString &userName, const QString &password, const QString &callsign, const QString &client, ConnectionCallback callback)
Connect.
static QByteArray serialize(const QString &channelTag, CryptoDtoMode mode, const QByteArray &transmitKey, uint sequenceToBeSent, T dto)
Serialize a DTO.
Callable wrapper for a member function with function signature F.
Definition: slot.h:62
AudioRxOnTransceiversDto.
Definition: dto.h:256
VoiceServerConnectionDataDto VoiceServer
Properties.
Definition: dto.h:91
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition: verify.h:26