Add SimVar subscription API and refactor struct handling#10
Merged
AussieScorcher merged 13 commits intostopbars:mainfrom Sep 23, 2025
Merged
Add SimVar subscription API and refactor struct handling#10AussieScorcher merged 13 commits intostopbars:mainfrom
AussieScorcher merged 13 commits intostopbars:mainfrom
Conversation
Replaces SimVarStructBinder with a new reflection-based SimVarFieldReaderFactory and related internal readers for efficient struct marshalling. Introduces ISimVarRequest and ISimVarSubscription interfaces, refactors SimVarManager to use these for hot-path request/response handling, and adds a strongly-typed subscription API for recurring SimVar updates. Updates SimConnectAttribute to support more flexible constructor overloads and nullable properties.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This pull request introduces a comprehensive SimVar subscription API and refactors struct handling infrastructure to support recurring data updates from Microsoft Flight Simulator. The changes focus on enabling real-time data streaming through subscription patterns while improving internal architecture for better performance and maintainability.
Key changes include:
- Addition of subscription API for both typed structs and scalar SimVars with recurring update periods
- Replacement of reflection-heavy struct binding with a new field reader factory system for efficient marshalling
- Introduction of internal interfaces (
ISimVarRequest,ISimVarSubscription) to support hot-path request/response handling
Reviewed Changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/SimConnect.NET.Tests.Net8/Tests/SimVarTests.cs | Adds comprehensive subscription testing with Position struct validation |
| src/SimConnect.NET/SimVar/SimVarManager.cs | Major refactor adding subscription methods and replacing reflection-based parsing with interface-driven approach |
| src/SimConnect.NET/SimVar/SimVarRequest.cs | Removed old generic request class |
| src/SimConnect.NET/SimVar/Internal/SimVarRequest.cs | New internal request implementation with subscription support and thread-pool callback dispatch |
| src/SimConnect.NET/SimVar/Internal/SimVarFieldReaderFactory.cs | New reflection-based factory for generating efficient struct field readers |
| src/SimConnect.NET/SimVar/Internal/SimVarFieldReader.cs | Concrete field reader implementation for typed struct marshalling |
| src/SimConnect.NET/SimVar/Internal/ISimVarRequest.cs | Internal interface for hot-path request handling without reflection |
| src/SimConnect.NET/SimVar/Internal/IFieldReader.cs | Interface for reading values from native memory into managed structs |
| src/SimConnect.NET/SimVar/ISimVarSubscription.cs | Public interface for subscription lifecycle management |
| src/SimConnect.NET/SimVar/SimVarSubscription.cs | Default subscription implementation with disposal and cancellation support |
| src/SimConnect.NET/SimConnectAttribute.cs | Enhanced with nullable parameters and additional constructor overloads |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Deleted an unnecessary blank line for improved code formatting and readability.
…e struct documentation to prevent test crash
…ANGELOG with new features, fixes, and notes
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Refactor input event handling to improve payload extraction and update struct documentation to prevent test crash
llamavert
approved these changes
Sep 23, 2025
Contributor
|
Thanks for your work, good job! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This pull request introduces several enhancements and refactors to the SimVar infrastructure, focusing on extensibility, error handling, and internal API clarity. The most significant changes are the addition of new internal interfaces and classes to support SimVar subscriptions and field reading, as well as improvements to the
SimConnectAttributeconstructor overloads and property types for greater flexibility.Changes Made
SimVar infrastructure and extensibility
ISimVarSubscriptioninterface to represent an active SimVar subscription, supporting disposal and completion tracking.ISimVarRequestinterface for handling SimVar requests in a non-generic, hot-path manner, with support for result setting, cancellation, and completion.IFieldReader<T>interface (renamed fromIFieldAccessor) to clarify its responsibility for reading values from native memory into managed structs.SimVarFieldReader<T, TDest>class to read and convert SimVar field values from native memory, supporting multiple SimConnect data types.SimConnectAttributeflexibility and error handlingSimConnectAttributeconstructors: removed exceptions when a SimVar is not found, allowing more flexible attribute creation. [1] [2]unit,dataType, andorderparameters optional and nullable in relevantSimConnectAttributeconstructors, and updated their property types to nullable for improved flexibility. Also introduced a new constructor overload for specifying only name and data type. [1] [2] [3]Replaces SimVarStructBinder with a new reflection-based SimVarFieldReaderFactory and related internal readers for efficient struct marshalling. Introduces ISimVarRequest and ISimVarSubscription interfaces, refactors SimVarManager to use these for hot-path request/response handling, and adds a strongly-typed subscription API for recurring SimVar updates. Updates SimConnectAttribute to support more flexible constructor overloads and nullable properties.Subscribe API Example
SimVar Subscription API
The
SimVarManagerexposes twoSubscribeoverloads to stream recurring simulator data. Both return anISimVarSubscriptionhandle you dispose (or cancel) to stop updates.Overload 1: Typed Struct Subscription
For aggregated / model-style data (
Tis a struct whose fields map to multiple SimVars). A single data definition is auto-built and cached perT.Example Struct
Overload 2: Scalar SimVar Subscription
For a single named SimVar (e.g.
"PLANE ALTITUDE"/"feet"). Definition is cached per(Name, Unit, DataType)tuple.Type Parameter (T)
where T : structSimVarFieldReaderFactory).int,long,float,double,bool,string,SimConnectDataLatLonAlt,SimConnectDataXyz)SimConnectDataTypeinference.Booleans transported as Int32 (0/1). Some implicit conversion fallback paths exist (e.g.
floatsatisfied bydouble).Parameters
simVarNameunitperiodOnceorNever).onValueobjectId0).cancellationTokenReturn Value
ISimVarSubscription— disposable handle. Disposing issues an internal stop (SimConnectPeriod.Never) and removes tracking.Lifecycle & Cancellation
With cancellation:
Error Conditions
ObjectDisposedExceptionsimVarNameorunit(scalar)ArgumentExceptiononValueArgumentNullExceptionperiodisOnceorNeverArgumentExceptionArgumentExceptionSimConnectExceptionPerformance & Caching
(Name, Unit, DataType).Threading & Callback Dispatch
SimConnect.NET deliberately offloads user callbacks to the .NET ThreadPool.
Relevant implementation excerpt (
SimVarRequest.SetResultlines 85–121):Why this design?
UnsafeQueueUserWorkItemavoids extra allocation vsTask.Runand uses a static lambda (no captures) for minimal overhead.preferLocal: truebiases work to a local thread, improving cache locality under burst loads.Practical Implications
If You Need Synchronous Semantics
Use one‑shot
GetAsyncfor atomic reads, or build an internal queue in your callback and signal a consumer thread.Writing Thread-Safe Handlers
When updating shared state:
Or use
Volatile.Write/Volatile.Readfor primitive snapshots.Best Practice: Unsubscribe
Always dispose (or rely on a cancellation token) when done to stop native update traffic and free tracking entries.
Mixed Example
Summary
Use the struct overload for cohesive snapshots; use the scalar overload for single values. Both yield a disposable
ISimVarSubscriptionfor explicit lifetime control.Author Information
Discord Username: bstudtma
Checklist: