diff --git a/src/blackcore/afv/clients/afvclient.cpp b/src/blackcore/afv/clients/afvclient.cpp index 908aa21f4..78699759c 100644 --- a/src/blackcore/afv/clients/afvclient.cpp +++ b/src/blackcore/afv/clients/afvclient.cpp @@ -37,19 +37,21 @@ namespace BlackCore } CAfvClient::CAfvClient(const QString &apiServer, QObject *parent) : - QObject(parent), CIdentifiable(this) + QObject(parent), CIdentifiable(this), + m_connection(new CClientConnection(apiServer, this)), + m_input(new CInput(SampleRate, this)), + m_output(new Output(this)), + m_voiceServerPositionTimer(new QTimer(this)) { - m_connection = new CClientConnection(apiServer, this); + m_connection->setReceiveAudio(false); - m_input = new CInput(SampleRate, this); connect(m_input, &CInput::opusDataAvailable, this, &CAfvClient::opusDataAvailable); connect(m_input, &CInput::inputVolumeStream, this, &CAfvClient::inputVolumeStream); - m_output = new Output(this); connect(m_output, &Output::outputVolumeStream, this, &CAfvClient::outputVolumeStream); connect(m_connection, &CClientConnection::audioReceived, this, &CAfvClient::audioOutDataAvailable); - connect(&m_voiceServerPositionTimer, &QTimer::timeout, this, &CAfvClient::onPositionUpdateTimer); + connect(m_voiceServerPositionTimer, &QTimer::timeout, this, &CAfvClient::onPositionUpdateTimer); // transceivers this->initTransceivers(); @@ -157,7 +159,7 @@ namespace BlackCore m_startDateTimeUtc = QDateTime::currentDateTimeUtc(); m_connection->setReceiveAudio(true); - m_voiceServerPositionTimer.start(5000); + m_voiceServerPositionTimer->start(5000); this->onSettingsChanged(); // make sure all settings are applied m_isStarted = true; CLogMessage(this).info(u"Started [Input: %1] [Output: %2]") << inputDevice.deviceName() << outputDevice.deviceName(); diff --git a/src/blackcore/afv/clients/afvclient.h b/src/blackcore/afv/clients/afvclient.h index 5ade19ea5..f35c33aef 100644 --- a/src/blackcore/afv/clients/afvclient.h +++ b/src/blackcore/afv/clients/afvclient.h @@ -229,7 +229,7 @@ namespace BlackCore double m_outputVolume = 1.0; double m_maxDbReadingInPTTInterval = -100; - QTimer m_voiceServerPositionTimer; + QTimer *m_voiceServerPositionTimer = nullptr; QVector m_transceivers; QSet m_enabledTransceivers; QVector m_aliasedStations; diff --git a/src/blackcore/afv/connection/apiserverconnection.cpp b/src/blackcore/afv/connection/apiserverconnection.cpp index f35f4dd44..fe5243b6a 100644 --- a/src/blackcore/afv/connection/apiserverconnection.cpp +++ b/src/blackcore/afv/connection/apiserverconnection.cpp @@ -23,7 +23,8 @@ namespace BlackCore { ApiServerConnection::ApiServerConnection(const QString &address, QObject *parent) : QObject(parent), - m_address(address) + m_address(address), + m_watch(new QElapsedTimer) { qDebug() << "ApiServerConnection instantiated"; } @@ -34,7 +35,7 @@ namespace BlackCore m_password = password; m_networkVersion = networkVersion; m_isAuthenticated = false; - m_watch.start(); + m_watch->start(); QUrl url(m_address); url.setPath("/api/v1/auth"); @@ -54,7 +55,7 @@ namespace BlackCore QScopedPointer reply(nam->post(request, QJsonDocument(obj).toJson())); while (! reply->isFinished()) { loop.exec(); } - qDebug() << "POST api/v1/auth (" << m_watch.elapsed() << "ms)"; + qDebug() << "POST api/v1/auth (" << m_watch->elapsed() << "ms)"; if (reply->error() != QNetworkReply::NoError) { qWarning() << reply->errorString(); @@ -142,7 +143,7 @@ namespace BlackCore checkExpiry(); - m_watch.start(); + m_watch->start(); QUrl url(m_address); url.setPath(resource); QNetworkAccessManager *nam = sApp->getNetworkAccessManager(); @@ -154,7 +155,7 @@ namespace BlackCore request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); QScopedPointer reply(nam->post(request, json.toJson())); while (! reply->isFinished()) { loop.exec(); } - qDebug() << "POST" << resource << "(" << m_watch.elapsed() << "ms)"; + qDebug() << "POST" << resource << "(" << m_watch->elapsed() << "ms)"; if (reply->error() != QNetworkReply::NoError) { @@ -167,7 +168,7 @@ namespace BlackCore { if (! m_isAuthenticated) { return; } - m_watch.start(); + m_watch->start(); QUrl url(m_address); url.setPath(resource); @@ -179,7 +180,7 @@ namespace BlackCore request.setRawHeader("Authorization", "Bearer " + m_jwt); QScopedPointer reply(nam->deleteResource(request)); while (! reply->isFinished()) { loop.exec(); } - qDebug() << "DELETE" << resource << "(" << m_watch.elapsed() << "ms)"; + qDebug() << "DELETE" << resource << "(" << m_watch->elapsed() << "ms)"; if (reply->error() != QNetworkReply::NoError) { diff --git a/src/blackcore/afv/connection/apiserverconnection.h b/src/blackcore/afv/connection/apiserverconnection.h index f126f91c9..fe87d6b45 100644 --- a/src/blackcore/afv/connection/apiserverconnection.h +++ b/src/blackcore/afv/connection/apiserverconnection.h @@ -72,7 +72,7 @@ namespace BlackCore QNetworkAccessManager *nam = sApp->getNetworkAccessManager(); - m_watch.start(); + m_watch->start(); QUrl url(m_address); url.setPath(resource); QEventLoop loop; @@ -82,7 +82,7 @@ namespace BlackCore request.setRawHeader("Authorization", "Bearer " + m_jwt); QScopedPointer reply(nam->post(request, QByteArray())); while (! reply->isFinished()) { loop.exec(); } - qDebug() << "POST" << resource << "(" << m_watch.elapsed() << "ms)"; + qDebug() << "POST" << resource << "(" << m_watch->elapsed() << "ms)"; if (reply->error() != QNetworkReply::NoError) { @@ -108,7 +108,7 @@ namespace BlackCore QNetworkAccessManager *nam = sApp->getNetworkAccessManager(); - m_watch.start(); + m_watch->start(); QUrl url(m_address); url.setPath(resource); QEventLoop loop; @@ -118,7 +118,7 @@ namespace BlackCore request.setRawHeader("Authorization", "Bearer " + m_jwt); QScopedPointer reply(nam->get(request)); while (! reply->isFinished()) { loop.exec(); } - qDebug() << "GET" << resource << "(" << m_watch.elapsed() << "ms)"; + qDebug() << "GET" << resource << "(" << m_watch->elapsed() << "ms)"; if (reply->error() != QNetworkReply::NoError) { @@ -156,7 +156,7 @@ namespace BlackCore bool m_isAuthenticated = false; - QElapsedTimer m_watch; + QElapsedTimer *m_watch = nullptr; }; } // ns } // ns diff --git a/src/blackcore/afv/connection/clientconnection.cpp b/src/blackcore/afv/connection/clientconnection.cpp index 220140a38..308d22a90 100644 --- a/src/blackcore/afv/connection/clientconnection.cpp +++ b/src/blackcore/afv/connection/clientconnection.cpp @@ -19,7 +19,9 @@ namespace BlackCore { CClientConnection::CClientConnection(const QString &apiServer, QObject *parent) : QObject(parent), - m_apiServerConnection(apiServer, this) + m_udpSocket(new QUdpSocket(this)), + m_voiceServerTimer(new QTimer(this)), + m_apiServerConnection(new ApiServerConnection(apiServer, this)) { qDebug() << "ClientConnection instantiated"; @@ -27,10 +29,10 @@ namespace BlackCore // connect(&m_apiServerConnection, &ApiServerConnection::addCallsignFinished, this, &ClientConnection::addCallsignFinished); // connect(&m_apiServerConnection, &ApiServerConnection::removeCallsignFinished, this, &ClientConnection::removeCallsignFinished); - connect(&m_voiceServerTimer, &QTimer::timeout, this, &CClientConnection::voiceServerHeartbeat); + connect(m_voiceServerTimer, &QTimer::timeout, this, &CClientConnection::voiceServerHeartbeat); - connect(&m_udpSocket, &QUdpSocket::readyRead, this, &CClientConnection::readPendingDatagrams); - connect(&m_udpSocket, qOverload(&QUdpSocket::error), this, &CClientConnection::handleSocketError); + connect(m_udpSocket, &QUdpSocket::readyRead, this, &CClientConnection::readPendingDatagrams); + connect(m_udpSocket, qOverload(&QUdpSocket::error), this, &CClientConnection::handleSocketError); } void CClientConnection::connectTo(const QString &userName, const QString &password, const QString &callsign) @@ -43,9 +45,9 @@ namespace BlackCore m_connection.m_userName = userName; m_connection.m_callsign = callsign; - bool result = m_apiServerConnection.connectTo(userName, password, m_networkVersion); + bool result = m_apiServerConnection->connectTo(userName, password, m_networkVersion); if (!result) { return; } - m_connection.m_tokens = m_apiServerConnection.addCallsign(m_connection.m_callsign); + m_connection.m_tokens = m_apiServerConnection->addCallsign(m_connection.m_callsign); m_connection.m_authenticatedDateTimeUtc = QDateTime::currentDateTimeUtc(); m_connection.createCryptoChannels(); @@ -71,12 +73,12 @@ namespace BlackCore if (! m_connection.m_callsign.isEmpty()) { - m_apiServerConnection.removeCallsign(m_connection.m_callsign); + m_apiServerConnection->removeCallsign(m_connection.m_callsign); } // TODO connectionCheckCancelTokenSource.Cancel(); //Stops connection check loop disconnectFromVoiceServer(); - m_apiServerConnection.forceDisconnect(); + m_apiServerConnection->forceDisconnect(); m_connection.m_tokens = {}; qDebug() << "Disconnection complete"; @@ -94,35 +96,35 @@ namespace BlackCore void CClientConnection::updateTransceivers(const QString &callsign, const QVector &transceivers) { - m_apiServerConnection.updateTransceivers(callsign, transceivers); + m_apiServerConnection->updateTransceivers(callsign, transceivers); } QVector CClientConnection::getAllAliasedStations() { - return m_apiServerConnection.getAllAliasedStations(); + return m_apiServerConnection->getAllAliasedStations(); } void CClientConnection::connectToVoiceServer() { QHostAddress localAddress(QHostAddress::AnyIPv4); - m_udpSocket.bind(localAddress); - m_voiceServerTimer.start(3000); + m_udpSocket->bind(localAddress); + m_voiceServerTimer->start(3000); qDebug() << "Connected to voice server (" + m_connection.m_tokens.VoiceServer.addressIpV4 << ")"; } void CClientConnection::disconnectFromVoiceServer() { - m_voiceServerTimer.stop(); - m_udpSocket.disconnectFromHost(); + m_voiceServerTimer->stop(); + m_udpSocket->disconnectFromHost(); qDebug() << "All TaskVoiceServer tasks stopped"; } void CClientConnection::readPendingDatagrams() { - while (m_udpSocket.hasPendingDatagrams()) + while (m_udpSocket->hasPendingDatagrams()) { - QNetworkDatagram datagram = m_udpSocket.receiveDatagram(); + QNetworkDatagram datagram = m_udpSocket->receiveDatagram(); processMessage(datagram.data()); } } @@ -154,7 +156,7 @@ namespace BlackCore void CClientConnection::handleSocketError(QAbstractSocket::SocketError error) { Q_UNUSED(error) - qDebug() << "UDP socket error" << m_udpSocket.errorString(); + qDebug() << "UDP socket error" << m_udpSocket->errorString(); } void CClientConnection::voiceServerHeartbeat() @@ -164,7 +166,7 @@ namespace BlackCore HeartbeatDto keepAlive; keepAlive.callsign = m_connection.m_callsign.toStdString(); const QByteArray dataBytes = CryptoDtoSerializer::serialize(*m_connection.voiceCryptoChannel, CryptoDtoMode::AEAD_ChaCha20Poly1305, keepAlive); - m_udpSocket.writeDatagram(dataBytes, QHostAddress(voiceServerUrl.host()), static_cast(voiceServerUrl.port())); + m_udpSocket->writeDatagram(dataBytes, QHostAddress(voiceServerUrl.host()), static_cast(voiceServerUrl.port())); } } // ns } // ns diff --git a/src/blackcore/afv/connection/clientconnection.h b/src/blackcore/afv/connection/clientconnection.h index 6192bc463..e462e004c 100644 --- a/src/blackcore/afv/connection/clientconnection.h +++ b/src/blackcore/afv/connection/clientconnection.h @@ -56,7 +56,7 @@ namespace BlackCore { QUrl voiceServerUrl("udp://" + m_connection.m_tokens.VoiceServer.addressIpV4); QByteArray dataBytes = Crypto::CryptoDtoSerializer::serialize(*m_connection.voiceCryptoChannel, CryptoDtoMode::AEAD_ChaCha20Poly1305, dto); - m_udpSocket.writeDatagram(dataBytes, QHostAddress(voiceServerUrl.host()), static_cast(voiceServerUrl.port())); + m_udpSocket->writeDatagram(dataBytes, QHostAddress(voiceServerUrl.host()), static_cast(voiceServerUrl.port())); } bool receiveAudioDto() const; @@ -85,11 +85,11 @@ namespace BlackCore CClientConnectionData m_connection; // Voice server - QUdpSocket m_udpSocket; - QTimer m_voiceServerTimer; + QUdpSocket *m_udpSocket = nullptr; + QTimer *m_voiceServerTimer = nullptr; // API server - ApiServerConnection m_apiServerConnection; + ApiServerConnection *m_apiServerConnection = nullptr; // Properties bool m_receiveAudioDto = true;