From 68beaa6d9d47432f6c8f8f6533601d6b0df9cf8c Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 27 Aug 2018 14:19:32 +0200 Subject: [PATCH] Ref T312, added checkImpl/check in simulator listeners --- .../simulator/emulated/simulatoremulated.cpp | 9 ++- .../simulator/emulated/simulatoremulated.h | 5 +- src/plugins/simulator/fs9/simulatorfs9.cpp | 74 ++++++++++++------- src/plugins/simulator/fs9/simulatorfs9.h | 10 ++- .../fsxcommon/simulatorfsxcommon.cpp | 14 ++++ .../simulator/fsxcommon/simulatorfsxcommon.h | 5 +- .../simulator/xplane/simulatorxplane.cpp | 20 ++++- .../simulator/xplane/simulatorxplane.h | 5 +- 8 files changed, 106 insertions(+), 36 deletions(-) diff --git a/src/plugins/simulator/emulated/simulatoremulated.cpp b/src/plugins/simulator/emulated/simulatoremulated.cpp index 7a175f653..35a297328 100644 --- a/src/plugins/simulator/emulated/simulatoremulated.cpp +++ b/src/plugins/simulator/emulated/simulatoremulated.cpp @@ -342,7 +342,7 @@ namespace BlackSimPlugin void CSimulatorEmulated::onSettingsChanged() { - const CSwiftPluginSettings settings(m_settings.get()); + const CSwiftPluginSettings settings(m_pluginSettings.get()); m_log = settings.isLoggingFunctionCalls(); const CSimulatorInfo simulator = settings.getEmulatedSimulator(); @@ -446,7 +446,7 @@ namespace BlackSimPlugin const QPointer guard(this); QTimer::singleShot(2000, this, [ = ] { - if (guard.isNull()) { return; } + if (!guard) { return; } Q_ASSERT_X(this->getPluginInfo().isValid(), Q_FUNC_INFO, "Invalid plugin"); emit this->simulatorStarted(this->getPluginInfo()); }); @@ -454,5 +454,10 @@ namespace BlackSimPlugin void CSimulatorEmulatedListener::stopImpl() { } + + void CSimulatorEmulatedListener::checkImpl() + { + this->startImpl(); + } } // ns } // ns diff --git a/src/plugins/simulator/emulated/simulatoremulated.h b/src/plugins/simulator/emulated/simulatoremulated.h index a648c1bfc..83e542ef0 100644 --- a/src/plugins/simulator/emulated/simulatoremulated.h +++ b/src/plugins/simulator/emulated/simulatoremulated.h @@ -168,7 +168,7 @@ namespace BlackSimPlugin BlackMisc::Simulation::CSimulatedAircraftList m_renderedAircraft; //!< represents remote aircraft in simulator QScopedPointer m_monitorWidget; //!< parent will be main window, so we need to destroy widget when destroyed BlackMisc::CConnectionGuard m_connectionGuard; //!< connected with provider - BlackMisc::CSettingReadOnly m_settings { this, &CSimulatorEmulated::onSettingsChanged }; + BlackMisc::CSettingReadOnly m_pluginSettings { this, &CSimulatorEmulated::onSettingsChanged }; QMap m_interpolators; //!< interpolators per callsign }; @@ -187,6 +187,9 @@ namespace BlackSimPlugin //! \copydoc BlackCore::ISimulatorListener::stopImpl virtual void stopImpl() override; + + //! \copydoc BlackCore::ISimulatorListener::stopImpl + virtual void checkImpl() override; }; } // ns } // ns diff --git a/src/plugins/simulator/fs9/simulatorfs9.cpp b/src/plugins/simulator/fs9/simulatorfs9.cpp index 291d85b26..b8029efab 100644 --- a/src/plugins/simulator/fs9/simulatorfs9.cpp +++ b/src/plugins/simulator/fs9/simulatorfs9.cpp @@ -32,7 +32,7 @@ using namespace BlackMisc::Network; using namespace BlackMisc::Simulation; using namespace BlackMisc::Simulation::FsCommon; using namespace BlackMisc::Weather; -using namespace BlackSimPlugin::Fs9; +using namespace BlackCore; using namespace BlackSimPlugin::FsCommon; namespace BlackSimPlugin @@ -86,9 +86,9 @@ namespace BlackSimPlugin FS_PBH pbhstrct; pbhstrct.pbh = positionVelocity.pbh; - int pitch = std::floor(pbhstrct.pitch / CFs9Sdk::pitchMultiplier()); + int pitch = qRound(std::floor(pbhstrct.pitch / CFs9Sdk::pitchMultiplier())); if (pitch < -90 || pitch > 89) { CLogMessage().warning("FS9: Pitch value out of limits: %1") << pitch; } - int bank = std::floor(pbhstrct.bank / CFs9Sdk::bankMultiplier()); + int bank = qRound(std::floor(pbhstrct.bank / CFs9Sdk::bankMultiplier())); // MSFS has inverted pitch and bank angles pitch = ~pitch; @@ -260,21 +260,21 @@ namespace BlackSimPlugin return changed; } - void CSimulatorFs9::displayStatusMessage(const BlackMisc::CStatusMessage &message) const + void CSimulatorFs9::displayStatusMessage(const CStatusMessage &message) const { // Avoid errors from CDirectPlayPeer as it may end in infinite loop - if (message.getSeverity() == BlackMisc::CStatusMessage::SeverityError && message.isFromClass()) + if (message.getSeverity() == CStatusMessage::SeverityError && message.isFromClass()) { return; } - if (message.getSeverity() != BlackMisc::CStatusMessage::SeverityDebug) + if (message.getSeverity() != CStatusMessage::SeverityDebug) { QMetaObject::invokeMethod(m_fs9Host.data(), "sendTextMessage", Q_ARG(QString, message.toQString())); } } - void CSimulatorFs9::displayTextMessage(const BlackMisc::Network::CTextMessage &message) const + void CSimulatorFs9::displayTextMessage(const CTextMessage &message) const { this->displayStatusMessage(message.asStatusMessage(true, true)); } @@ -385,7 +385,7 @@ namespace BlackSimPlugin CSimulatorFs9Listener::CSimulatorFs9Listener(const CSimulatorPluginInfo &info, const QSharedPointer &fs9Host, const QSharedPointer &lobbyClient) : - BlackCore::ISimulatorListener(info), + ISimulatorListener(info), m_timer(new QTimer(this)), m_fs9Host(fs9Host), m_lobbyClient(lobbyClient) @@ -395,26 +395,12 @@ namespace BlackSimPlugin m_timer->setObjectName(this->objectName() + ":m_timer"); // Test whether we can lobby connect at all. - bool canLobbyConnect = m_lobbyClient->canLobbyConnect(); + const bool canLobbyConnect = m_lobbyClient->canLobbyConnect(); - connect(m_timer, &QTimer::timeout, [this, canLobbyConnect]() + // check connection + connect(m_timer, &QTimer::timeout, [ = ]() { - if (m_fs9Host->getHostAddress().isEmpty()) { return; } // host not yet set up - if (canLobbyConnect) - { - if (m_isConnecting || m_lobbyClient->connectFs9ToHost(m_fs9Host->getHostAddress()) == S_OK) - { - m_isConnecting = true; - CLogMessage(this).info("swift is joining FS9 to the multiplayer session..."); - } - } - - if (!m_isStarted && m_fs9Host->isConnected()) - { - emit simulatorStarted(getPluginInfo()); - m_isStarted = true; - m_isConnecting = false; - } + this->checkConnection(canLobbyConnect); }); } @@ -429,6 +415,42 @@ namespace BlackSimPlugin m_timer->stop(); } + void CSimulatorFs9Listener::checkImpl() + { + if (m_timer) { m_timer->start(); } + if (this->isShuttingDown()) { return; } + + QPointer myself(this); + QTimer::singleShot(0, this, [ = ] + { + if (!myself) { return; } + const bool canLobbyConnect = m_lobbyClient->canLobbyConnect(); + this->checkConnection(canLobbyConnect); + }); + } + + bool CSimulatorFs9Listener::checkConnection(bool canLobbyConnect) + { + if (m_fs9Host->getHostAddress().isEmpty()) { return false; } // host not yet set up + if (canLobbyConnect) + { + if (m_isConnecting || m_lobbyClient->connectFs9ToHost(m_fs9Host->getHostAddress()) == S_OK) + { + m_isConnecting = true; + CLogMessage(this).info("swift is joining FS9 to the multiplayer session..."); + } + } + + if (!m_isStarted && m_fs9Host->isConnected()) + { + m_isStarted = true; + m_isConnecting = false; + emit this->simulatorStarted(this->getPluginInfo()); + } + + return m_isConnecting; + } + static void cleanupFs9Host(CFs9Host *host) { host->quitAndWait(); diff --git a/src/plugins/simulator/fs9/simulatorfs9.h b/src/plugins/simulator/fs9/simulatorfs9.h index 92a3d3667..1f28b20da 100644 --- a/src/plugins/simulator/fs9/simulatorfs9.h +++ b/src/plugins/simulator/fs9/simulatorfs9.h @@ -49,7 +49,7 @@ namespace BlackSimPlugin QObject *parent = nullptr); //! Destructor - virtual ~CSimulatorFs9() = default; + virtual ~CSimulatorFs9() override = default; //! \name Interface implementations //! \@{ @@ -125,7 +125,13 @@ namespace BlackSimPlugin //! \copydoc BlackCore::ISimulatorListener::stopImpl virtual void stopImpl() override; + //! \copydoc BlackCore::ISimulatorListener::checkImpl + virtual void checkImpl() override; + private: + //! Check connection to FS9 + bool checkConnection(bool canLobbyConnect); + QTimer *m_timer = nullptr; bool m_isConnecting = false; bool m_isStarted = false; @@ -145,7 +151,7 @@ namespace BlackSimPlugin CSimulatorFs9Factory(QObject *parent = nullptr); //! Destructor - virtual ~CSimulatorFs9Factory(); + virtual ~CSimulatorFs9Factory() override; //! \copydoc BlackCore::ISimulatorFactory::create virtual BlackCore::ISimulator *create( diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp index 3014b6d5a..e06122b7f 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp @@ -2101,6 +2101,20 @@ namespace BlackSimPlugin m_timer.stop(); } + void CSimulatorFsxCommonListener::checkImpl() + { + if (!m_timer.isActive()) { return; } + if (this->isShuttingDown()) { return; } + + m_timer.start(); // restart because we will check just now + QPointer myself(this); + QTimer::singleShot(0, this, [ = ] + { + if (!myself) { return; } + this->checkConnection(); + }); + } + QString CSimulatorFsxCommonListener::backendInfo() const { if (m_simulatorName.isEmpty()) { return ISimulatorListener::backendInfo(); } diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h index c42c779fa..68c2d94ee 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h @@ -521,6 +521,9 @@ namespace BlackSimPlugin //! \copydoc BlackCore::ISimulatorListener::stopImpl virtual void stopImpl() override; + //! \copydoc BlackCore::ISimulatorListener::checkImpl + virtual void checkImpl() override; + //! Test if connection can be established void checkConnection(); @@ -531,7 +534,7 @@ namespace BlackSimPlugin bool checkSimConnectDll() const; private: - QTimer m_timer { this }; //!< timer, "this" is needed otherwise warnings when move to new thread + QTimer m_timer { this }; //!< timer, "this" is needed otherwise I get warnings when move to new thread QString m_simulatorVersion; QString m_simConnectVersion; QString m_simulatorName; diff --git a/src/plugins/simulator/xplane/simulatorxplane.cpp b/src/plugins/simulator/xplane/simulatorxplane.cpp index 0d42f2ae4..407612288 100644 --- a/src/plugins/simulator/xplane/simulatorxplane.cpp +++ b/src/plugins/simulator/xplane/simulatorxplane.cpp @@ -1096,11 +1096,11 @@ namespace BlackSimPlugin { QString dbusAddress = m_xswiftbusServerSetting.getThreadLocal(); - if (BlackMisc::CDBusServer::isSessionOrSystemAddress(dbusAddress)) + if (CDBusServer::isSessionOrSystemAddress(dbusAddress)) { checkConnectionViaBus(dbusAddress); } - else if (BlackMisc::CDBusServer::isQtDBusAddress(dbusAddress)) + else if (CDBusServer::isQtDBusAddress(dbusAddress)) { m_timer.start(); } @@ -1121,6 +1121,20 @@ namespace BlackSimPlugin m_timer.stop(); } + void CSimulatorXPlaneListener::checkImpl() + { + if (!m_timer.isActive()) { return; } + if (this->isShuttingDown()) { return; } + + m_timer.start(); // restart because we will check just now + QPointer myself(this); + QTimer::singleShot(0, this, [ = ] + { + if (!myself) { return; } + this->checkConnectionViaPeer(); + }); + } + void CSimulatorXPlaneListener::checkConnectionViaBus(const QString &address) { if (m_watcher) { return; } @@ -1148,7 +1162,7 @@ namespace BlackSimPlugin void CSimulatorXPlaneListener::checkConnectionViaPeer() { m_conn = QDBusConnection::connectToPeer(m_xswiftbusServerSetting.getThreadLocal(), "xswiftbus"); - if (! m_conn.isConnected()) + if (!m_conn.isConnected()) { // This is required to cleanup the connection in QtDBus m_conn.disconnectFromPeer(m_conn.name()); diff --git a/src/plugins/simulator/xplane/simulatorxplane.h b/src/plugins/simulator/xplane/simulatorxplane.h index cbeeb12aa..f545e5872 100644 --- a/src/plugins/simulator/xplane/simulatorxplane.h +++ b/src/plugins/simulator/xplane/simulatorxplane.h @@ -118,7 +118,7 @@ namespace BlackSimPlugin QObject *parent = nullptr); //! Dtor - virtual ~CSimulatorXPlane(); + virtual ~CSimulatorXPlane() override; //! \name ISimulator implementations //! @{ @@ -273,6 +273,9 @@ namespace BlackSimPlugin //! \copydoc BlackCore::ISimulatorListener::stopImpl virtual void stopImpl() override; + //! \copydoc BlackCore::ISimulatorListener::checkImpl + virtual void checkImpl() override; + private: //! Check if XSwiftBus service is already registered on the bus void checkConnectionViaBus(const QString &address);