6 #include <QNetworkDatagram>
11 using namespace swift::config;
13 using namespace swift::core::afv::crypto;
15 namespace swift::core::afv::connection
17 CClientConnection::CClientConnection(
const QString &apiServer, QObject *parent)
18 : QObject(parent), m_udpSocket(new QUdpSocket(this)), m_voiceServerTimer(new QTimer(this)),
29 connect(m_voiceServerTimer, &QTimer::timeout,
this,
30 &CClientConnection::voiceServerHeartbeat);
31 connect(m_udpSocket, &QUdpSocket::readyRead,
this, &CClientConnection::readPendingDatagrams);
32 connect(m_udpSocket, &QAbstractSocket::errorOccurred,
this, &CClientConnection::handleSocketError);
47 QPointer<CClientConnection> myself(
this);
49 userName, password, client, m_networkVersion,
51 this, [=](
bool authenticated) {
53 if (!myself) {
return; }
62 this->connectToVoiceServer();
65 CLogMessage(
this).
info(u
"Connected: '%1' to voice server, socket open: %2")
66 << cs << boolToYesNo(m_udpSocket->isOpen());
68 else { m_connection.
reset(); }
74 if (callback) { callback(authenticated); }
96 disconnectFromVoiceServer();
115 if (!m_apiServerConnection) {
return false; }
116 return m_apiServerConnection->
setUrl(url);
121 static const QString e;
122 if (!m_apiServerConnection) {
return e; }
123 return m_apiServerConnection->
getUrl();
126 void CClientConnection::connectToVoiceServer()
128 const QHostAddress localAddress(QHostAddress::AnyIPv4);
129 m_udpSocket->bind(localAddress);
130 m_voiceServerTimer->start(3000);
135 void CClientConnection::disconnectFromVoiceServer()
137 m_voiceServerTimer->stop();
138 m_udpSocket->disconnectFromHost();
142 void CClientConnection::readPendingDatagrams()
144 while (m_udpSocket->hasPendingDatagrams())
146 const QNetworkDatagram datagram = m_udpSocket->receiveDatagram();
147 this->processMessage(datagram.data());
151 void CClientConnection::processMessage(
const QByteArray &messageDdata,
bool loopback)
155 SWIFT_VERIFY_X(
false, Q_FUNC_INFO,
"processMessage used without crypto channel");
165 const AudioRxOnTransceiversDto audioOnTransceiverDto = deserializer.
getDto<AudioRxOnTransceiversDto>();
174 if (CBuildConfig::isLocalDeveloperDebugBuild())
186 void CClientConnection::handleSocketError(QAbstractSocket::SocketError error)
189 CLogMessage(
this).
debug(u
"UDP socket error: '%1'") << m_udpSocket->errorString();
192 void CClientConnection::voiceServerHeartbeat()
196 SWIFT_VERIFY_X(
false, Q_FUNC_INFO,
"voiceServerHeartbeat used without crypto channel or socket");
201 if (CBuildConfig::isLocalDeveloperDebugBuild())
203 CLogMessage(
this).
debug(u
"Sending voice server heartbeat to '%1'") << voiceServerUrl.host();
205 HeartbeatDto keepAlive;
206 keepAlive.callsign = m_connection.
getCallsign().toStdString();
208 CryptoDtoMode::AEAD_ChaCha20Poly1305, keepAlive);
209 m_udpSocket->writeDatagram(dataBytes, QHostAddress(voiceServerUrl.host()),
210 static_cast<quint16
>(voiceServerUrl.port()));
void removeCallsign(const QString &callsign)
Remove callsign from network.
PostCallsignResponseDto addCallsign(const QString &callsign)
Add callsign to network.
void forceDisconnect()
Force disconnect from network.
QVector< StationDto > getAllAliasedStations()
All aliased stations.
void connectTo(const QString &username, const QString &password, const QString &client, const QUuid &networkVersion, ConnectionCallback callback)
Connect to network.
void updateTransceivers(const QString &callsign, const QVector< TransceiverDto > &transceivers)
Update transceivers.
const QString & getUrl() const
Get the URL.
bool setUrl(const QString &url)
Set the URL.
const PostCallsignResponseDto & getTokens() const
Tokens.
bool isReceivingAudio() const
Receiving audio?
void setTsAuthenticatedToNow()
Timestamps.
void createCryptoChannels()
Crypto channels for voice and data.
bool isConnected() const
Is connected?
void setCallsign(const QString &callsign)
Callsign.
void setTokens(const PostCallsignResponseDto &dto)
Tokens.
const QString & getCallsign() const
Callsign.
void setConnected(bool connected)
Is connected?
void setTsHeartbeatToNow()
Timestamps.
void setUserName(const QString &un)
Uername.
QScopedPointer< crypto::CCryptoDtoChannel > m_voiceCryptoChannel
used crypto channel
void reset()
Reset all login related data.
void updateTransceivers(const QString &callsign, const QVector< TransceiverDto > &transceivers)
Update transceivers.
bool updateVoiceServerUrl(const QString &url)
Update the voice server URL.
void disconnectFrom(const QString &reason={})
Disconnect.
void audioReceived(const AudioRxOnTransceiversDto &dto)
Audio has been received.
QVector< StationDto > getAllAliasedStations()
All aliased stations.
const QString & getVoiceServerUrl() const
Get the voice server URL.
void connectTo(const QString &userName, const QString &password, const QString &callsign, const QString &client, ConnectionCallback callback)
Connect.
Class for emitting a log message.
Derived & warning(const char16_t(&format)[N])
Set the severity to warning, providing a format string.
Derived & debug()
Set the severity to debug.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
Callable wrapper for a member function with function signature F.
Free functions in swift::misc.
static QByteArray getShortDtoName()
Names.
static QByteArray getShortDtoName()
Name.
VoiceServerConnectionDataDto VoiceServer
Properties.
QString addressIpV4
Properties.
quint16 m_dataLength
Data.
QByteArray m_dtoNameBuffer
Name data.
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.