diff --git a/src/blackcore/afv/clients/afvclient.cpp b/src/blackcore/afv/clients/afvclient.cpp index 01b2d769e..8612da25a 100644 --- a/src/blackcore/afv/clients/afvclient.cpp +++ b/src/blackcore/afv/clients/afvclient.cpp @@ -112,13 +112,30 @@ namespace BlackCore void CAfvClient::connectWithContexts() { if (m_connectedWithContext) { return; } - if (!hasContext()) { return; } + if (!hasContexts()) { return; } this->disconnect(sApp->getIContextOwnAircraft()); sApp->getIContextOwnAircraft()->disconnect(this); connect(sApp->getIContextOwnAircraft(), &IContextOwnAircraft::changedAircraftCockpit, this, &CAfvClient::onUpdateTransceiversFromContext, Qt::QueuedConnection); m_connectedWithContext = true; } + void CAfvClient::fetchSimulatorSettings() + { + // call that in correct thread + if (!hasContexts()) { return; } + + if (QThread::currentThread() != sApp->getIContextSimulator()->thread()) + { + // Method needs to be executed in the context thread + QPointer myself(this); + QMetaObject::invokeMethod(sApp->getIContextSimulator(), [ = ]() { if (myself) { this->fetchSimulatorSettings(); }}); + return; + } + + const bool integrated = sApp->getIContextSimulator()->getSimulatorSettings().isComIntegrated(); + m_integratedComUnit = integrated; + } + void CAfvClient::connectTo(const QString &cid, const QString &password, const QString &callsign, const QString &client) { if (QThread::currentThread() != thread()) @@ -406,7 +423,7 @@ namespace BlackCore { // also update if NOT connected, values will be preset - if (hasContext()) + if (hasContexts()) { const CSimulatedAircraft ownAircraft = sApp->getIContextOwnAircraft()->getOwnAircraft(); this->updatePosition(ownAircraft.latitude().value(CAngleUnit::deg()), @@ -790,7 +807,7 @@ namespace BlackCore void CAfvClient::onTimerUpdate() { - if (hasContext()) + if (hasContexts()) { // for pilot client const CSimulatedAircraft aircraft = sApp->getIContextOwnAircraft()->getOwnAircraft(); @@ -798,6 +815,9 @@ namespace BlackCore // disconnect if NOT connected this->autoLogoffWithoutFsdNetwork(); + + // get the settings in correct thread + this->fetchSimulatorSettings(); } else { @@ -816,7 +836,7 @@ namespace BlackCore void CAfvClient::autoLogoffWithoutFsdNetwork() { - if (!hasContext()) { return; } + if (!hasContexts()) { return; } if (!this->isConnected()) { m_connectMismatches = 0; return; } // AFV is connected @@ -860,13 +880,6 @@ namespace BlackCore bool tx2 = false; bool rx2 = true; - // make sure to use defaults with COM integration disabled - if (hasContext()) - { - const bool integrated = sApp->getIContextSimulator()->getSimulatorSettings().isComIntegrated(); - m_integratedComUnit = integrated; - } - if (m_integratedComUnit) { tx1 = com1.isTransmitEnabled(); @@ -1028,7 +1041,7 @@ namespace BlackCore CLogMessage(this).info(u"UserClient instantiated (deferred init)"); } - bool CAfvClient::hasContext() + bool CAfvClient::hasContexts() { return sApp && !sApp->isShuttingDown() && sApp->getIContextOwnAircraft() && sApp->getIContextNetwork() && sApp->getIContextSimulator(); } diff --git a/src/blackcore/afv/clients/afvclient.h b/src/blackcore/afv/clients/afvclient.h index c527d47b1..b11431f83 100644 --- a/src/blackcore/afv/clients/afvclient.h +++ b/src/blackcore/afv/clients/afvclient.h @@ -329,7 +329,7 @@ namespace BlackCore std::atomic_bool m_isStarted { false }; std::atomic_bool m_loopbackOn { false }; std::atomic_bool m_winCoInitialized { false }; //!< Windows only CoInitializeEx - std::atomic_bool m_integratedComUnit {false}; //!< is COM unit sychr + std::atomic_bool m_integratedComUnit {false}; //!< is COM unit sychronized, integrated QDateTime m_startDateTimeUtc; @@ -347,7 +347,8 @@ namespace BlackCore void deferredInit(); void initTransceivers(); void connectWithContexts(); - static bool hasContext(); + void fetchSimulatorSettings(); + static bool hasContexts(); std::atomic_bool m_connectedWithContext { false };