Add asynchronous connection in ISimulator/CSimulatorFSX

refs #231
This commit is contained in:
Roland Winklmeier
2014-05-06 13:22:36 +02:00
committed by Roland Winklmeier
parent 5498b449ac
commit de75c5c35a
4 changed files with 45 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
include (../../../../config.pri)
include (../../../../build.pri)
QT += core dbus gui network
QT += core dbus gui network concurrent
TARGET = simulator_fsx
TEMPLATE = lib

View File

@@ -9,6 +9,7 @@
#include "blacksim/fsx/fsxsimulatorsetup.h"
#include "blacksim/simulatorinfo.h"
#include <QTimer>
#include <QtConcurrent>
using namespace BlackMisc::Aviation;
using namespace BlackMisc::PhysicalQuantities;
@@ -73,6 +74,21 @@ namespace BlackSimPlugin
return true;
}
void CSimulatorFsx::asyncConnectTo()
{
connect(&m_watcherConnect, SIGNAL(finished()), this, SLOT(connectToFinished()));
auto asyncConnectFunc = [&]() -> bool
{
if (FAILED(SimConnect_Open(&m_hSimConnect, "BlackBox", nullptr, 0, 0, 0))) return false;
return true;
};
QFuture<bool> result = QtConcurrent::run( asyncConnectFunc );
m_watcherConnect.setFuture(result);
}
bool CSimulatorFsx::disconnectFrom()
{
if (!m_isConnected)
@@ -327,6 +343,22 @@ namespace BlackSimPlugin
SimConnect_CallDispatch(m_hSimConnect, SimConnectProc, this);
}
void CSimulatorFsx::connectToFinished()
{
if( m_watcherConnect.result() )
{
initSystemEvents();
initDataDefinitions();
m_simconnectTimerId = startTimer(50);
m_isConnected = true;
emit statusChanged(Connected);
}
else
emit statusChanged(ConnectionFailed);
}
HRESULT CSimulatorFsx::initSystemEvents()
{
HRESULT hr = S_OK;

View File

@@ -15,6 +15,7 @@
#include <QObject>
#include <QtPlugin>
#include <QHash>
#include <QFutureWatcher>
#ifndef NOMINMAX
#define NOMINMAX
@@ -82,6 +83,9 @@ namespace BlackSimPlugin
//! \copydoc ISimulator::connectTo()
virtual bool connectTo() override;
//! \copydoc ISimulator::asyncConnectTo()
virtual void asyncConnectTo() override;
//! \copydoc ISimulator::disconnectFrom()
virtual bool disconnectFrom() override;
@@ -134,6 +138,9 @@ namespace BlackSimPlugin
//! \brief Dispatch SimConnect messages
void dispatch();
//! \brief Called when asynchronous connection to Simconnect has finished
void connectToFinished();
private:
struct SimConnectObject
@@ -167,6 +174,8 @@ namespace BlackSimPlugin
QHash<BlackMisc::Aviation::CCallsign, SimConnectObject> m_simConnectObjects;
int m_simconnectTimerId;
QFutureWatcher<bool> m_watcherConnect;
};
}