6#include <QNetworkDatagram>
11using namespace swift::config;
13using namespace swift::core::afv::crypto;
15namespace swift::core::afv::connection
30 &CClientConnection::voiceServerHeartbeat);
38 if (m_connection.isConnected())
44 m_connection.setUserName(userName);
45 m_connection.setCallsign(callsign);
48 m_apiServerConnection->connectTo(
49 userName, password, client, m_networkVersion,
51 this, [=,
this](
bool authenticated) {
60 const QString cs = m_connection.getCallsign();
61 m_connection.setTokens(m_apiServerConnection->addCallsign(cs));
62 m_connection.setTsAuthenticatedToNow();
63 m_connection.createCryptoChannels();
64 m_connection.setTsHeartbeatToNow();
65 this->connectToVoiceServer();
68 CLogMessage(
this).
info(u
"Connected: '%1' to voice server, socket open: %2")
77 m_connection.setConnected(authenticated);
82 callback(authenticated);
89 if (!m_connection.isConnected())
95 m_connection.setConnected(
false);
99 if (!m_connection.getCallsign().isEmpty())
101 m_apiServerConnection->removeCallsign(m_connection.getCallsign());
105 disconnectFromVoiceServer();
106 m_apiServerConnection->forceDisconnect();
107 m_connection.reset();
114 m_apiServerConnection->updateTransceivers(callsign, transceivers);
119 return m_apiServerConnection->getAllAliasedStations();
124 if (!m_apiServerConnection)
128 return m_apiServerConnection->setUrl(url);
134 if (!m_apiServerConnection)
138 return m_apiServerConnection->getUrl();
141 void CClientConnection::connectToVoiceServer()
144 m_udpSocket->
bind(localAddress);
145 m_voiceServerTimer->
start(3000);
150 void CClientConnection::disconnectFromVoiceServer()
152 m_voiceServerTimer->
stop();
157 void CClientConnection::readPendingDatagrams()
162 this->processMessage(datagram.
data());
166 void CClientConnection::processMessage(
const QByteArray &messageDdata,
bool loopback)
170 SWIFT_VERIFY_X(
false, Q_FUNC_INFO,
"processMessage used without crypto channel");
174 CryptoDtoSerializer::Deserializer deserializer =
180 const auto audioOnTransceiverDto = deserializer.
getDto<AudioRxOnTransceiversDto>();
181 if (m_connection.isReceivingAudio() && m_connection.isConnected())
188 m_connection.setTsHeartbeatToNow();
191 CLogMessage(
this).debug(u
"Received voice server heartbeat");
196 CLogMessage(
this).warning(u
"Received unknown data: %1 %2")
204 CLogMessage(
this).debug(u
"UDP socket error: '%1'") << m_udpSocket->errorString();
207 void CClientConnection::voiceServerHeartbeat()
209 if (!m_connection.m_voiceCryptoChannel || !m_udpSocket)
211 SWIFT_VERIFY_X(
false, Q_FUNC_INFO,
"voiceServerHeartbeat used without crypto channel or socket");
215 const QUrl voiceServerUrl(
"udp://" + m_connection.getTokens().VoiceServer.addressIpV4);
218 CLogMessage(
this).debug(u
"Sending voice server heartbeat to '%1'") << voiceServerUrl.host();
220 HeartbeatDto keepAlive;
221 keepAlive.
callsign = m_connection.getCallsign().toStdString();
223 CryptoDtoMode::AEAD_ChaCha20Poly1305, keepAlive);
224 m_udpSocket->writeDatagram(dataBytes, QHostAddress(voiceServerUrl.host()),
225 static_cast<quint16
>(voiceServerUrl.port()));
swift::misc::CSlot< void(bool)> ConnectionCallback
Callback for.
static bool isLocalDeveloperDebugBuild()
Local build for developers.
const PostCallsignResponseDto & getTokens() const
Tokens.
QScopedPointer< crypto::CCryptoDtoChannel > m_voiceCryptoChannel
used crypto channel
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.
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.
static QByteArray serialize(const QString &channelTag, CryptoDtoMode mode, const QByteArray &transmitKey, uint sequenceToBeSent, T dto)
Serialize a DTO.
static Deserializer deserialize(CCryptoDtoChannel &channel, const QByteArray &bytes, bool loopback)
Deserialize.
Class for emitting a log message.
Derived & debug()
Set the severity to debug.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
Free functions in swift::misc.
SWIFT_MISC_EXPORT const QString & boolToYesNo(bool v)
Bool to yes/no.
bool bind(QHostAddress::SpecialAddress addr, quint16 port, QAbstractSocket::BindMode mode)
virtual void disconnectFromHost()
void errorOccurred(QAbstractSocket::SocketError socketError)
QByteArray data() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool hasPendingDatagrams() const const
QNetworkDatagram receiveDatagram(qint64 maxSize)
static QByteArray getShortDtoName()
Names.
static QByteArray getShortDtoName()
Name.
std::string callsign
callsign
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.