Ref T27, preparation for refactoring, turned private slots into normal member functions

* Motivation: slots no longer required, and in the driver the (small) slot overhead might matter
* renamed some members to better reflect future use
* timer intervals as constexpr
* fixed some typos and comments
This commit is contained in:
Klaus Basan
2017-06-22 01:24:40 +02:00
parent 5aaf5cdeb1
commit 6d26c96569
8 changed files with 134 additions and 58 deletions

View File

@@ -18,7 +18,7 @@ namespace BlackSimPlugin
{
namespace Fsx
{
//! FSX Simulator Implementation
//! FSX simulator implementation
class CSimulatorFsx : public BlackSimPlugin::FsxCommon::CSimulatorFsxCommon
{
Q_OBJECT

View File

@@ -129,7 +129,7 @@ namespace BlackSimPlugin
return simObject.hasValidRequestAndObjectId() && objectId == simObject.getObjectId();
}
bool CSimConnectObjects::containsPendingAdd() const
bool CSimConnectObjects::containsPendingAdded() const
{
for (const CSimConnectObject &simObject : this->values())
{
@@ -138,6 +138,55 @@ namespace BlackSimPlugin
return false;
}
bool CSimConnectObjects::containsPendingRemoved() const
{
for (const CSimConnectObject &simObject : this->values())
{
if (simObject.isPendingRemoved()) { return true; }
}
return false;
}
int CSimConnectObjects::countPendingAdded() const
{
int c = 0;
for (const CSimConnectObject &simObject : this->values())
{
if (simObject.isPendingAdded()) { c++; }
}
return c;
}
int CSimConnectObjects::countPendingRemoved() const
{
int c = 0;
for (const CSimConnectObject &simObject : this->values())
{
if (simObject.isPendingRemoved()) { c++; }
}
return c;
}
CCallsignSet CSimConnectObjects::getPendingAddedCallsigns() const
{
CCallsignSet callsigns;
for (const CSimConnectObject &simObject : this->values())
{
if (simObject.isPendingAdded()) { callsigns.push_back(simObject.getCallsign()); }
}
return callsigns;
}
CCallsignSet CSimConnectObjects::getPendingRemovedCallsigns() const
{
CCallsignSet callsigns;
for (const CSimConnectObject &simObject : this->values())
{
if (simObject.isPendingRemoved()) { callsigns.push_back(simObject.getCallsign()); }
}
return callsigns;
}
void CSimConnectObjects::toggleInterpolatorModes()
{
for (const CCallsign &cs : this->keys())

View File

@@ -36,7 +36,7 @@ namespace BlackSimPlugin
//! Destructor
~CSimConnectObject() {}
//! Get Callsign
//! Get callsign
const BlackMisc::Aviation::CCallsign &getCallsign() const { return m_aircraft.getCallsign(); }
//! Simulated aircraft (as added)
@@ -57,6 +57,9 @@ namespace BlackSimPlugin
//! Get current lights (requested from simulator)
const BlackMisc::Aviation::CAircraftLights &getCurrentLightsInSimulator() const { return m_currentLightsInSim; }
//! Received lights in simulator
bool hasCurrentLightsInSimulator() const { return !m_currentLightsInSim.isNull(); }
//! Set current lights when received from simulator
void setCurrentLightsInSimulator(const BlackMisc::Aviation::CAircraftLights &lights) { m_currentLightsInSim = lights; }
@@ -162,7 +165,22 @@ namespace BlackSimPlugin
bool isKnownSimObjectId(DWORD objectId) const;
//! Pending add condition
bool containsPendingAdd() const;
bool containsPendingAdded() const;
//! Pending removed condition
bool containsPendingRemoved() const;
//! Number of pending added
int countPendingAdded() const;
//! Number of pending removed
int countPendingRemoved() const;
//! Callsigns of pending added callsigns
BlackMisc::Aviation::CCallsignSet getPendingAddedCallsigns() const;
//! Callsigns of pending removed callsigns
BlackMisc::Aviation::CCallsignSet getPendingRemovedCallsigns() const;
//! Toggle interpolator modes
void toggleInterpolatorModes();

View File

@@ -51,8 +51,8 @@ namespace BlackSimPlugin
Q_ASSERT_X(ownAircraftProvider, Q_FUNC_INFO, "Missing provider");
Q_ASSERT_X(remoteAircraftProvider, Q_FUNC_INFO, "Missing provider");
Q_ASSERT_X(sApp, Q_FUNC_INFO, "Missing global object");
m_realityBubbleTimer.setInterval(20 * 1000);
connect(&m_realityBubbleTimer, &QTimer::timeout, this, &CSimulatorFsxCommon::ps_addAircraftCurrentlyOutOfBubble);
m_addPendingAircraftTimer.setInterval(AddPendingAircraftIntervalMs);
connect(&m_addPendingAircraftTimer, &QTimer::timeout, this, &CSimulatorFsxCommon::addPendingAircraft);
m_useFsuipc = false;
m_defaultModel =
@@ -94,8 +94,8 @@ namespace BlackSimPlugin
// set structures and move on
initEvents();
initDataDefinitionsWhenConnected();
m_simConnectTimerId = startTimer(10);
m_realityBubbleTimer.start();
m_simConnectTimerId = startTimer(DispatchIntervalMs);
m_addPendingAircraftTimer.start();
return true;
}
@@ -127,7 +127,7 @@ namespace BlackSimPlugin
if (callsign.isEmpty()) { return false; }
// check if we have to do something
m_outOfRealityBubble.removeByCallsign(callsign);
m_addPendingAircraft.removeByCallsign(callsign);
if (m_simConnectObjects.contains(callsign))
{
const CSimConnectObject simObj = m_simConnectObjects[callsign];
@@ -293,7 +293,7 @@ namespace BlackSimPlugin
{
CCallsignSet callsigns(this->m_simConnectObjects.keys());
callsigns.push_back(m_aircraftToAddAgainWhenRemoved.getCallsigns()); // not really rendered right now, but very soon
callsigns.push_back(m_outOfRealityBubble.getCallsigns()); // not really rendered, but for the logic it should look like it is
callsigns.push_back(m_addPendingAircraft.getCallsigns()); // not really rendered, but for the logic it should look like it is
return CCallsignSet(this->m_simConnectObjects.keys());
}
@@ -364,11 +364,12 @@ namespace BlackSimPlugin
return;
}
emitSimulatorCombinedStatus();
emitSimulatorCombinedStatus(); // force sending status
}
void CSimulatorFsxCommon::onSimStopped()
{
// stopping events in FSX: Load menu, weather and season
const int oldStatus = getSimulatorStatus();
m_simSimulating = false;
emitSimulatorCombinedStatus(oldStatus);
@@ -547,11 +548,11 @@ namespace BlackSimPlugin
if (!simObject.hasValidRequestAndObjectId() || callsign.isEmpty()) { return false; }
// we know the object has been created. But it can happen it is directly removed afterwards
QTimer::singleShot(500, this, [ = ] { this->ps_deferredSimulatorReportedObjectAdded(callsign); });
QTimer::singleShot(500, this, [ = ] { this->deferredSimulatorReportedObjectAdded(callsign); });
return true;
}
bool CSimulatorFsxCommon::ps_deferredSimulatorReportedObjectAdded(const CCallsign &callsign)
bool CSimulatorFsxCommon::deferredSimulatorReportedObjectAdded(const CCallsign &callsign)
{
if (callsign.isEmpty()) { return false; }
if (!m_simConnectObjects.contains(callsign)) { return false; } // removed in mean time
@@ -594,13 +595,13 @@ namespace BlackSimPlugin
return true;
}
void CSimulatorFsxCommon::ps_addAircraftCurrentlyOutOfBubble()
void CSimulatorFsxCommon::addPendingAircraft()
{
if (m_outOfRealityBubble.isEmpty()) { return; }
if (m_addPendingAircraft.isEmpty()) { return; }
const CCallsignSet aircraftCallsignsInRange(getAircraftInRangeCallsigns());
CSimulatedAircraftList toBeAddedAircraft;
CCallsignSet toBeRemovedCallsigns;
for (const CSimulatedAircraft &aircraft : as_const(m_outOfRealityBubble))
for (const CSimulatedAircraft &aircraft : as_const(m_addPendingAircraft))
{
Q_ASSERT_X(!aircraft.getCallsign().isEmpty(), Q_FUNC_INFO, "missing callsign");
if (aircraftCallsignsInRange.contains(aircraft.getCallsign()))
@@ -612,7 +613,7 @@ namespace BlackSimPlugin
toBeRemovedCallsigns.push_back(aircraft.getCallsign());
}
}
m_outOfRealityBubble.removeByCallsigns(toBeRemovedCallsigns);
m_addPendingAircraft.removeByCallsigns(toBeRemovedCallsigns);
// add aircraft, but non blocking
int t = 100;
@@ -633,7 +634,6 @@ namespace BlackSimPlugin
const CCallsign callsign(simObject.getCallsign());
Q_ASSERT_X(!callsign.isEmpty(), Q_FUNC_INFO, "missing callsign");
bool ok = false;
if (simObject.isPendingRemoved())
{
// good case, object has been removed
@@ -642,11 +642,15 @@ namespace BlackSimPlugin
else
{
// object was removed, but removal was not requested by us
// this means we are out of the reality bubble (or something else went wrong)
// this means we are out of the reality bubble or something else went wrong
// Possible reasons:
// 1) out of reality bubble
// 2) wrong position (in ground etc.)
// 3) Simulator not running (ie in stopped mode)
if (!simObject.getAircraftModelString().isEmpty())
{
this->m_outOfRealityBubble.push_back(simObject.getAircraft());
CLogMessage(this).info("Aircraft removed, '%1' '%2' object id '%3' out of reality bubble") << callsign.toQString() << simObject.getAircraftModelString() << objectID;
this->m_addPendingAircraft.push_back(simObject.getAircraft());
CLogMessage(this).info("Aircraft removed, '%1' '%2' object id '%3' out of reality bubble or other reason") << callsign.toQString() << simObject.getAircraftModelString() << objectID;
}
else
{
@@ -656,7 +660,7 @@ namespace BlackSimPlugin
// in all cases we remove
const int c = m_simConnectObjects.remove(callsign);
ok = c > 0;
const bool removed = (c > 0);
const bool updated = this->updateAircraftRendered(simObject.getCallsign(), false);
if (updated)
{
@@ -694,10 +698,10 @@ namespace BlackSimPlugin
void CSimulatorFsxCommon::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
ps_dispatch();
dispatch();
}
void CSimulatorFsxCommon::ps_dispatch()
void CSimulatorFsxCommon::dispatch()
{
HRESULT hr = SimConnect_CallDispatch(m_hSimConnect, SimConnectProc, this);
if (hr != S_OK)
@@ -736,7 +740,7 @@ namespace BlackSimPlugin
Q_ASSERT_X(CThreadUtils::isCurrentThreadObjectThread(this), Q_FUNC_INFO, "wrong thread");
if (callsign.isEmpty()) { return false; } // can happen if an object is not an aircraft
m_outOfRealityBubble.removeByCallsign(callsign);
m_addPendingAircraft.removeByCallsign(callsign);
if (!m_simConnectObjects.contains(callsign)) { return false; } // already fully removed or not yet added
CSimConnectObject &simObject = m_simConnectObjects[callsign];
@@ -767,7 +771,7 @@ namespace BlackSimPlugin
}
// cleanup function, actually this should not be needed
QTimer::singleShot(100, this, &CSimulatorFsxCommon::ps_physicallyRemoveAircraftNotInProvider);
QTimer::singleShot(100, this, &CSimulatorFsxCommon::physicallyRemoveAircraftNotInProvider);
// bye
return true;
@@ -870,7 +874,7 @@ namespace BlackSimPlugin
return hr;
}
// inti data definitions and SB data area
// init data definitions and SB data area
hr += initDataDefinitionsWhenConnected();
if (hr != S_OK)
{
@@ -1287,7 +1291,7 @@ namespace BlackSimPlugin
void CSimulatorFsxCommon::clearAllAircraft()
{
m_simConnectObjects.clear();
m_outOfRealityBubble.clear();
m_addPendingAircraft.clear();
CSimulatorFsCommon::clearAllAircraft();
}
@@ -1318,7 +1322,7 @@ namespace BlackSimPlugin
return simObjectCallsigns.difference(providerCallsigns);
}
CCallsignSet CSimulatorFsxCommon::ps_physicallyRemoveAircraftNotInProvider()
CCallsignSet CSimulatorFsxCommon::physicallyRemoveAircraftNotInProvider()
{
const CCallsignSet toBeRemoved(getCallsignsMissingInProvider());
if (toBeRemoved.isEmpty()) { return toBeRemoved; }

View File

@@ -138,30 +138,29 @@ namespace BlackSimPlugin
virtual void ps_remoteProviderAddAircraftParts(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::Aviation::CAircraftParts &parts) override;
//! @}
//! Timer event (our SimConnect event loop), runs ps_dispatch
//! Timer event (our SimConnect event loop), runs dispatch
//! \sa m_simconnectTimerId
virtual void timerEvent(QTimerEvent *event) override;
private slots:
private:
//! Dispatch SimConnect messages
void ps_dispatch();
void dispatch();
//! Remove aircraft not in provider anymore
//! Remove aircraft no longer in provider
//! \remark kind of cleanup function, in an ideal this should never need to cleanup something
BlackMisc::Aviation::CCallsignSet ps_physicallyRemoveAircraftNotInProvider();
BlackMisc::Aviation::CCallsignSet physicallyRemoveAircraftNotInProvider();
//! Handle that an object has been added in simulator
//! \remark checks if the object was really added after an add request and not directly removed again
bool ps_deferredSimulatorReportedObjectAdded(const BlackMisc::Aviation::CCallsign &callsign);
bool deferredSimulatorReportedObjectAdded(const BlackMisc::Aviation::CCallsign &callsign);
//! Try to add the aircraft currently out of bubble
void ps_addAircraftCurrentlyOutOfBubble();
//! Try to add the next aircraft (one by one)
void addPendingAircraft();
private:
//! Call this method to declare the simulator connected
void setSimConnected();
//! Called when sim has started
//! Called when simulator has started
void onSimRunning();
//! Slot called every visual frame
@@ -213,13 +212,13 @@ namespace BlackSimPlugin
void updateOwnAircraftFromSimulator(const DataDefinitionClientAreaSb &sbDataArea);
//! An AI aircraft was added in the simulator
bool simulatorReportedObjectAdded(DWORD objectID);
bool simulatorReportedObjectAdded(DWORD objectId);
//! Simulator reported that AI aircraft was removed
bool simulatorReportedObjectRemoved(DWORD objectID);
//! Set ID of a SimConnect object, so far we only have an request id in the object
bool setSimConnectObjectId(DWORD requestID, DWORD objectID);
bool setSimConnectObjectId(DWORD requestId, DWORD objectId);
//! Remember current lights
bool setCurrentLights(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::Aviation::CAircraftLights &lights);
@@ -252,19 +251,21 @@ namespace BlackSimPlugin
BlackMisc::Aviation::CCallsignSet getCallsignsMissingInProvider() const;
//! Request for sim data?
static bool isRequestForSimData(DWORD requestId) { return requestId >= (RequestSimDataStart + RequestSimDataOffset) && requestId < (RequestSimDataStart + RequestSimDataOffset + SimObjectNumber); }
static bool isRequestForSimData(DWORD requestId) { return requestId >= (RequestSimDataStart + RequestSimDataOffset) && requestId < (RequestSimDataStart + RequestSimDataOffset + MaxSimObjects); }
//! Request for sim data?
static bool isRequestForLights(DWORD requestId) { return requestId >= (RequestSimDataStart + RequestLightsOffset) && requestId < (RequestSimDataStart + RequestLightsOffset + SimObjectNumber); }
static bool isRequestForLights(DWORD requestId) { return requestId >= (RequestSimDataStart + RequestLightsOffset) && requestId < (RequestSimDataStart + RequestLightsOffset + MaxSimObjects); }
static constexpr int GuessRemoteAircraftPartsCycle = 20; //!< guess every n-th cycle
static constexpr int SkipUpdateCyclesForCockpit = 10; //!< skip x cycles before updating cockpit again
static constexpr int IgnoreReceiveExceptions = 10; //!< skip exceptions when displayed more than x times
static constexpr int SimObjectNumber = 10000; //!< max. SimObjects at the same time
static constexpr int MaxSimObjects = 10000; //!< max.number of SimObjects at the same time
static constexpr int RequestSimDataStart = static_cast<int>(CSimConnectDefinitions::RequestEndMarker);
static constexpr int RequestSimDataEnd = RequestSimDataStart + SimObjectNumber - 1;
static constexpr int RequestSimDataOffset = 0 * SimObjectNumber;
static constexpr int RequestLightsOffset = 1 * SimObjectNumber;
static constexpr int RequestSimDataEnd = RequestSimDataStart + MaxSimObjects - 1;
static constexpr int RequestSimDataOffset = 0 * MaxSimObjects;
static constexpr int RequestLightsOffset = 1 * MaxSimObjects;
static constexpr int AddPendingAircraftIntervalMs = 20 * 1000;
static constexpr int DispatchIntervalMs = 10;
QString m_simConnectVersion; //!< SimConnect version
bool m_simConnected = false; //!< Is simulator connected?
@@ -274,13 +275,13 @@ namespace BlackSimPlugin
int m_simConnectTimerId = -1; //!< Timer identifier
int m_skipCockpitUpdateCycles = 0; //!< skip some update cycles to allow changes in simulator cockpit to be set
int m_interpolationRequest = 0; //!< current interpolation request
int m_dispatchErrors = 0; //!< number of dispatched failed, \sa ps_dispatch
int m_dispatchErrors = 0; //!< number of dispatched failed, \sa dispatch
int m_receiveExceptionCount = 0; //!< exceptions
HANDLE m_hSimConnect = nullptr; //!< handle to SimConnect object
CSimConnectObjects m_simConnectObjects; //!< AI objects and their object / request ids
QTimer m_realityBubbleTimer { this }; //!< updating of aircraft out of reality bubble
QTimer m_addPendingAircraftTimer { this }; //!< updating of aircraft awaiting add
DWORD m_requestIdSimData = RequestSimDataStart; //!< request id, use obtainRequestId() to get id
BlackMisc::Simulation::CSimulatedAircraftList m_outOfRealityBubble; //!< aircraft removed by FSX because they are out of reality bubble
BlackMisc::Simulation::CSimulatedAircraftList m_addPendingAircraft; //!< aircraft awaiting to be added
};
//! Listener for FSX

View File

@@ -111,7 +111,7 @@ namespace BlackSimPlugin
case SIMCONNECT_RECV_ID_EVENT_OBJECT_ADDREMOVE:
{
const SIMCONNECT_RECV_EVENT_OBJECT_ADDREMOVE *event = static_cast<SIMCONNECT_RECV_EVENT_OBJECT_ADDREMOVE *>(pData);
const DWORD objectID = event->dwData;
const DWORD objectId = event->dwData;
const SIMCONNECT_SIMOBJECT_TYPE objectType = event->eObjType;
if (objectType != SIMCONNECT_SIMOBJECT_TYPE_AIRCRAFT && objectType != SIMCONNECT_SIMOBJECT_TYPE_HELICOPTER)
{
@@ -119,15 +119,15 @@ namespace BlackSimPlugin
}
// such an object is not necessarily one of ours
// for instance, I always see object 5 when I start the simulator
if (!simulatorFsx->getSimConnectObjects().isKnownSimObjectId(objectID)) break;
// for instance, I always see object "5" when I start the simulator
if (!simulatorFsx->getSimConnectObjects().isKnownSimObjectId(objectId)) break;
switch (event->uEventID)
{
case SystemEventObjectAdded:
// added in SIMCONNECT_RECV_ID_ASSIGNED_OBJECT_ID
break;
case SystemEventObjectRemoved:
simulatorFsx->simulatorReportedObjectRemoved(objectID);
simulatorFsx->simulatorReportedObjectRemoved(objectId);
break;
default:
break;