Skip to content

Commit

Permalink
Add MF.Config.MAX_VARS_PER_FRAME to limit number of variables read in…
Browse files Browse the repository at this point in the history
… 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 <[email protected]>
  • Loading branch information
DocMoebiuz and cpuwolf authored Aug 20, 2022
1 parent 57355bd commit a63c40c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/PackageDefinitions/mobiflight-event-module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<AssetPackage Version="0.5.0">
<AssetPackage Version="0.6.0">
<ItemSettings>
<ContentType>MISC</ContentType>
<Title>Event Module</Title>
Expand Down
33 changes: 30 additions & 3 deletions src/Sources/Code/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
#include <SimConnect.h>
#include <MSFS\Legacy\gauges.h>
#include <vector>
#include <list>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#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.";
Expand All @@ -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;
Expand All @@ -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<SimVar>::iterator RollingClientDataReadIndex;
uint16_t RollingClientDataReadIndex;

};

// The list of currently registered clients
Expand Down Expand Up @@ -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
Expand All @@ -293,8 +304,14 @@ void ReadSimVar(SimVar &simVar, Client* client) {
void ReadSimVars() {
for (auto& client : RegisteredClients) {
std::vector<SimVar>* 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;
}
}
}
Expand Down Expand Up @@ -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<SimVar>();
//newClient->RollingClientDataReadIndex = newClient->SimVars.begin();
newClient->RollingClientDataReadIndex = 0;
newClient->DataDefinitionIdSimVarsStart = SimVarOffset + (newClient->ID * ClientDataDefinitionIdSimVarsRange);

RegisteredClients.push_back(newClient);
Expand Down Expand Up @@ -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<uint16_t>(std::stoi(str));
MOBIFLIGHT_MAX_VARS_PER_FRAME = value;
fprintf(stderr, "MobiFlight: Set MF.Config.MAX_VARS_PER_FRAME to %u.\n", value);
}
break;
}

Expand Down

0 comments on commit a63c40c

Please sign in to comment.