From a63c40c8d72ffba69d4dbb3f9a1fb5d9997d9680 Mon Sep 17 00:00:00 2001 From: Sebastian M <86157512+DocMoebiuz@users.noreply.github.com> Date: Sat, 20 Aug 2022 13:44:16 +0200 Subject: [PATCH] Add MF.Config.MAX_VARS_PER_FRAME to limit number of variables read in each frame (#13) * reduce ReadSimVars() count to improve performance 1. use std::list instead of std::vector, for circular iteration 2. list max 5 SimVars reading in very frame 3. VimVars clean() needs update list iterator * too slow for feedback data use 12 for a while * Changed back to std::vector and added method to send command for changing it. * removed explicit DEFAULT value Co-authored-by: Wei Shuai --- .../mobiflight-event-module.xml | 2 +- src/Sources/Code/Module.cpp | 33 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/PackageDefinitions/mobiflight-event-module.xml b/src/PackageDefinitions/mobiflight-event-module.xml index f6b9e7b..91e6512 100644 --- a/src/PackageDefinitions/mobiflight-event-module.xml +++ b/src/PackageDefinitions/mobiflight-event-module.xml @@ -1,5 +1,5 @@ - + MISC Event Module diff --git a/src/Sources/Code/Module.cpp b/src/Sources/Code/Module.cpp index 3d3ec59..d700b37 100644 --- a/src/Sources/Code/Module.cpp +++ b/src/Sources/Code/Module.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -10,7 +11,7 @@ #include "Module.h" HANDLE g_hSimConnect; -const char* version = "0.5.0"; +const char* version = "0.6.0"; const char* ClientName = "MobiFlightWasmModule"; const char* MobiFlightEventPrefix = "MobiFlight."; @@ -33,6 +34,10 @@ uint16_t SimVarOffset = 1000; // For each registered client can 10000 data definition ids are reserved uint16_t ClientDataDefinitionIdSimVarsRange = 10000; +// Maximum number of variables that are read from sim per frame, Default: 30 +// Can be set to different value via config command +uint16_t MOBIFLIGHT_MAX_VARS_PER_FRAME = 30; + // data struct for dynamically registered SimVars struct SimVar { int ID; @@ -58,6 +63,10 @@ struct Client { // This is an optimization to be able to re-use already defined data definition IDs & request IDs // after resetting registered SimVars uint16_t MaxClientDataDefinition = 0; + // Runtime Rolling CLient Data reading Index + //std::vector::iterator RollingClientDataReadIndex; + uint16_t RollingClientDataReadIndex; + }; // The list of currently registered clients @@ -272,6 +281,8 @@ void RegisterSimVar(const std::string code, Client* client) { void ClearSimVars(Client* client) { client->SimVars.clear(); fprintf(stderr, "MobiFlight: Cleared SimVar tracking."); + //client->RollingClientDataReadIndex = client->SimVars.begin(); + client->RollingClientDataReadIndex = 0; } // Read a single SimVar and send the current value to SimConnect Clients @@ -293,8 +304,14 @@ void ReadSimVar(SimVar &simVar, Client* client) { void ReadSimVars() { for (auto& client : RegisteredClients) { std::vector* SimVars = &(client->SimVars); - for (auto& value : *SimVars) { - ReadSimVar(value, client); + + int maxVarsPerFrame = SimVars->size() < MOBIFLIGHT_MAX_VARS_PER_FRAME ? SimVars->size() : MOBIFLIGHT_MAX_VARS_PER_FRAME; + + for (int i=0; i < maxVarsPerFrame; ++i) { + ReadSimVar(SimVars->at(client->RollingClientDataReadIndex), client); + client->RollingClientDataReadIndex++; + if (client->RollingClientDataReadIndex >= SimVars->size()) + client->RollingClientDataReadIndex = 0; } } } @@ -379,6 +396,8 @@ Client* RegisterNewClient(const std::string clientName) { newClient->DataDefinitionIDStringResponse = 2 * newClient->ID; // 500 Clients possible until offset 1000 is reached newClient->DataDefinitionIDStringCommand = newClient->DataDefinitionIDStringResponse + 1; newClient->SimVars = std::vector(); + //newClient->RollingClientDataReadIndex = newClient->SimVars.begin(); + newClient->RollingClientDataReadIndex = 0; newClient->DataDefinitionIdSimVarsStart = SimVarOffset + (newClient->ID * ClientDataDefinitionIdSimVarsRange); RegisteredClients.push_back(newClient); @@ -525,6 +544,14 @@ void CALLBACK MyDispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void* pContex SendNewClientResponse(client, newClient); fprintf(stderr, "MobiFlight: Received Client to register: %s.\n", str.c_str()); } + + if (m_str.get()->find("MF.Config.MAX_VARS_PER_FRAME.Set.") != std::string::npos) { + std::string prefix = "MF.Config.MAX_VARS_PER_FRAME.Set."; + str = m_str.get()->substr(prefix.length()); + uint16_t value = static_cast(std::stoi(str)); + MOBIFLIGHT_MAX_VARS_PER_FRAME = value; + fprintf(stderr, "MobiFlight: Set MF.Config.MAX_VARS_PER_FRAME to %u.\n", value); + } break; }