diff --git a/src/blackcore/fsd/fsdclient.cpp b/src/blackcore/fsd/fsdclient.cpp index c6b7f2d71..0b8918785 100644 --- a/src/blackcore/fsd/fsdclient.cpp +++ b/src/blackcore/fsd/fsdclient.cpp @@ -108,10 +108,7 @@ namespace BlackCore::Fsd m_tokenBucket(10, 5000, 1) { initializeMessageTypes(); - connect(&m_socket, &QTcpSocket::readyRead, this, &CFSDClient::readDataFromSocket, Qt::QueuedConnection); - connect(&m_socket, &QTcpSocket::connected, this, &CFSDClient::handleSocketConnected); - connect(&m_socket, qOverload(&QTcpSocket::error), this, &CFSDClient::printSocketError, Qt::QueuedConnection); - connect(&m_socket, qOverload(&QTcpSocket::error), this, &CFSDClient::handleSocketError, Qt::QueuedConnection); + connectSocketSignals(); m_positionUpdateTimer.setObjectName(this->objectName().append(":m_positionUpdateTimer")); connect(&m_positionUpdateTimer, &QTimer::timeout, this, &CFSDClient::sendPilotDataUpdate); @@ -137,6 +134,14 @@ namespace BlackCore::Fsd } } + void CFSDClient::connectSocketSignals() + { + connect(m_socket.get(), &QTcpSocket::readyRead, this, &CFSDClient::readDataFromSocket, Qt::QueuedConnection); + connect(m_socket.get(), &QTcpSocket::connected, this, &CFSDClient::handleSocketConnected); + connect(m_socket.get(), &QTcpSocket::errorOccurred, this, &CFSDClient::printSocketError, Qt::QueuedConnection); + connect(m_socket.get(), &QTcpSocket::errorOccurred, this, &CFSDClient::handleSocketError, Qt::QueuedConnection); + } + void CFSDClient::setClientIdAndKey(quint16 id, const QByteArray &key) { QWriteLocker l(&m_lockUserClientBuffered); @@ -241,7 +246,7 @@ namespace BlackCore::Fsd return; } - if (m_socket.isOpen()) { return; } + if (m_socket->isOpen()) { return; } Q_ASSERT(!m_clientName.isEmpty()); Q_ASSERT((m_versionMajor + m_versionMinor) > 0); Q_ASSERT(m_capabilities != Capabilities::None); @@ -266,7 +271,7 @@ namespace BlackCore::Fsd const CServer s = this->getServer(); const QString host = s.getAddress(); const quint16 port = static_cast(s.getPort()); - m_socket.connectToHost(host, port); + m_socket->connectToHost(host, port); this->startPositionTimers(); } @@ -286,12 +291,12 @@ namespace BlackCore::Fsd // allow also to close if broken CLoginMode mode = this->getLoginMode(); - if (m_socket.isOpen()) + if (m_socket->isOpen()) { if (mode.isPilot()) { this->sendDeletePilot(); } else if (mode.isObserver()) { this->sendDeleteAtc(); } } - m_socket.close(); + m_socket->close(); this->updateConnectionStatus(CConnectionStatus::Disconnected); this->clearState(); @@ -817,7 +822,7 @@ namespace BlackCore::Fsd if (message.isEmpty()) { return; } const QByteArray bufferEncoded = m_fsdTextCodec->fromUnicode(message); if (m_printToConsole) { qDebug() << "FSD Sent=>" << bufferEncoded; } - if (!m_unitTestMode) { m_socket.write(bufferEncoded); } + if (!m_unitTestMode) { m_socket->write(bufferEncoded); } // remove CR/LF and emit emitRawFsdMessage(message.trimmed(), true); @@ -972,7 +977,7 @@ namespace BlackCore::Fsd std::array sysuid = {}; vatsim_get_system_unique_id(sysuid.data()); - const QString userInfo = QStringLiteral("CID=") % cid % " " % m_clientName % " IP=" % m_socket.localAddress().toString() % + const QString userInfo = QStringLiteral("CID=") % cid % " " % m_clientName % " IP=" % m_socket->localAddress().toString() % " SYS_UID=" % sysuid.data() % " FSVER=" % m_hostApplication % " LT=" % QString::number(latitude) % " LO=" % QString::number(longitude) % " AL=" % QString::number(altitude) % " " % realName; @@ -2127,14 +2132,14 @@ namespace BlackCore::Fsd void CFSDClient::readDataFromSocketMaxLines(int maxLines) { - if (m_socket.bytesAvailable() < 1) { return; } + if (m_socket->bytesAvailable() < 1) { return; } int lines = 0; // reads at least one line if available - while (m_socket.canReadLine()) + while (m_socket->canReadLine()) { - const QByteArray dataEncoded = m_socket.readLine(); + const QByteArray dataEncoded = m_socket->readLine(); if (dataEncoded.isEmpty()) { continue; } const QString data = m_fsdTextCodec->toUnicode(dataEncoded); this->parseMessage(data); @@ -2164,9 +2169,9 @@ namespace BlackCore::Fsd QString CFSDClient::socketErrorString(QAbstractSocket::SocketError error) const { QString e = CFSDClient::socketErrorToQString(error); - if (!m_socket.errorString().isEmpty()) + if (!m_socket->errorString().isEmpty()) { - e += QStringLiteral(": ") % m_socket.errorString(); + e += QStringLiteral(": ") % m_socket->errorString(); } return e; } diff --git a/src/blackcore/fsd/fsdclient.h b/src/blackcore/fsd/fsdclient.h index ae3af85c3..3e2274bee 100644 --- a/src/blackcore/fsd/fsdclient.h +++ b/src/blackcore/fsd/fsdclient.h @@ -473,7 +473,8 @@ namespace BlackCore::Fsd // Parser QHash m_messageTypeMapping; - QTcpSocket m_socket { this }; //!< used TCP socket, parent needed as it runs in worker thread + std::unique_ptr m_socket = std::make_unique(this); //!< used TCP socket, parent needed as it runs in worker thread + void connectSocketSignals(); std::atomic_bool m_unitTestMode { false }; std::atomic_bool m_printToConsole { false };