From 924e094a385c253639cf19ab33ca30cbdf4974fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Garapich?= Date: Mon, 9 Mar 2015 19:18:24 +0100 Subject: [PATCH] Listen for simulators on another thread --- src/blackcore/context_simulator_impl.cpp | 38 +++++++++++++------ src/blackcore/context_simulator_impl.h | 2 + src/blackcore/simulator.h | 3 +- src/plugins/simulator/fs9/simulator_fs9.h | 1 + src/plugins/simulator/fsx/simulator_fsx.h | 1 + .../simulator/xplane/simulator_xplane.h | 1 + 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/blackcore/context_simulator_impl.cpp b/src/blackcore/context_simulator_impl.cpp index 7b272541f..be54e358b 100644 --- a/src/blackcore/context_simulator_impl.cpp +++ b/src/blackcore/context_simulator_impl.cpp @@ -404,13 +404,19 @@ namespace BlackCore driver.listener = factory->createListener(); connect(driver.listener, &ISimulatorListener::simulatorStarted, this, &CContextSimulator::ps_simulatorStarted); + driver.listener->moveToThread(&m_listenersThread); } ISimulatorListener *listener = m_simulatorDrivers[simulatorInfo].listener; Q_ASSERT(listener); - listener->start(); + + if (!m_listenersThread.isRunning()) + { + m_listenersThread.start(QThread::LowPriority); + } + + QMetaObject::invokeMethod(listener, "start"); CLogMessage(this).debug() << "Listening for simulator:" << simulatorInfo.toQString(true); - } void CContextSimulator::listenForAllSimulators() @@ -428,12 +434,15 @@ namespace BlackCore if (plugin.isUnspecified()) listenForAllSimulators(); else + { listenForSimulator(plugin); + } } void CContextSimulator::unloadSimulatorPlugin() { - if (m_simulator) { + if (m_simulator) + { // depending on shutdown order, network might already have been deleted IContextNetwork *networkContext = this->getIContextNetwork(); Q_ASSERT(networkContext); @@ -541,11 +550,15 @@ namespace BlackCore CSimulatorInfo plugin = getIContextSettings()->getSimulatorSettings().getSelectedPlugin(); // no simulator loaded yet, listen - if (!m_simulator) { + if (!m_simulator) + { stopSimulatorListeners(); - if (plugin.isSameSimulator(CSimulatorInfo::UnspecifiedSim())) { + if (plugin.isSameSimulator(CSimulatorInfo::UnspecifiedSim())) + { listenForAllSimulators(); - } else { + } + else + { listenForSimulator(plugin); } } @@ -599,16 +612,18 @@ namespace BlackCore { const QString path = qApp->applicationDirPath().append("/plugins/simulator"); m_pluginsDir = QDir(path); - if (!m_pluginsDir.exists()) { + if (!m_pluginsDir.exists()) + { CLogMessage(this).error("No plugin directory: %1") << m_pluginsDir.currentPath(); return; } QStringList fileNames = m_pluginsDir.entryList(QDir::Files); fileNames.sort(Qt::CaseInsensitive); // give a certain order, rather than random file order - for (const auto& fileName: fileNames) { - if (!QLibrary::isLibrary(fileName)) { - + for (const auto& fileName: fileNames) + { + if (!QLibrary::isLibrary(fileName)) + { continue; } @@ -624,7 +639,6 @@ namespace BlackCore } else { - CLogMessage(this).warning("Simulator driver in %1 is invalid") << pluginPath; } } @@ -634,7 +648,7 @@ namespace BlackCore { std::for_each(m_simulatorDrivers.begin(), m_simulatorDrivers.end(), [](DriverInfo& driver) { if (driver.listener) - driver.listener->stop(); + QMetaObject::invokeMethod(driver.listener, "stop"); }); } diff --git a/src/blackcore/context_simulator_impl.h b/src/blackcore/context_simulator_impl.h index 0f58c8ad5..522b6cf71 100644 --- a/src/blackcore/context_simulator_impl.h +++ b/src/blackcore/context_simulator_impl.h @@ -14,6 +14,7 @@ #include "blackcore/context_simulator.h" #include "blackcore/simulator.h" +#include "blackmisc/worker.h" #include "blacksim/simulatorinfo.h" #include "blacksim/simulatorinfolist.h" #include "blackmisc/nwtextmessagelist.h" @@ -216,6 +217,7 @@ namespace BlackCore QDir m_pluginsDir; QMap m_simulatorDrivers; QFuture m_canConnectResult; + BlackMisc::CRegularThread m_listenersThread; }; } // namespace diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index 5b36a6482..d5ed830e9 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -218,7 +218,8 @@ namespace BlackCore //! Destructor virtual ~ISimulatorListener() = default; - + + public slots: //! Start listening for the simulator to start. virtual void start() = 0; diff --git a/src/plugins/simulator/fs9/simulator_fs9.h b/src/plugins/simulator/fs9/simulator_fs9.h index f6de3cc97..64c294c52 100644 --- a/src/plugins/simulator/fs9/simulator_fs9.h +++ b/src/plugins/simulator/fs9/simulator_fs9.h @@ -121,6 +121,7 @@ namespace BlackSimPlugin //! Constructor CSimulatorFs9Listener(const QSharedPointer &fs9Host, const QSharedPointer &lobbyClient, QObject* parent); + public slots: //! \copydoc BlackCore::ISimulatorListener::start virtual void start() override; diff --git a/src/plugins/simulator/fsx/simulator_fsx.h b/src/plugins/simulator/fsx/simulator_fsx.h index d5d3cfd9d..8ffcdfa0a 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.h +++ b/src/plugins/simulator/fsx/simulator_fsx.h @@ -209,6 +209,7 @@ namespace BlackSimPlugin //! Constructor CSimulatorFsxListener(QObject* parent); + public slots: //! \copydoc BlackCore::ISimulatorListener::start virtual void start() override; diff --git a/src/plugins/simulator/xplane/simulator_xplane.h b/src/plugins/simulator/xplane/simulator_xplane.h index 71c6054f8..20c39aeea 100644 --- a/src/plugins/simulator/xplane/simulator_xplane.h +++ b/src/plugins/simulator/xplane/simulator_xplane.h @@ -181,6 +181,7 @@ namespace BlackSimPlugin //! Constructor CSimulatorXPlaneListener(QObject* parent); + public slots: //! \copydoc BlackCore::ISimulatorListener::start virtual void start() override;