Add unsubscribe to objects

This commit is contained in:
Mihai Dinculescu
2022-10-18 21:46:01 +01:00
parent a2002e02cd
commit 0db9847030
11 changed files with 211 additions and 109 deletions

View File

@@ -26,12 +26,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("Simple Program");
match client {
Ok(mut client) => loop {
Ok(mut client) => {
let mut notifications_received = 0;
loop {
let notification = client.get_next_dispatch()?;
match notification {
Some(Notification::Open) => {
println!("Open");
println!("Connection opened.");
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
@@ -39,6 +42,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
println!("Subscription stopped.");
break;
}
}
}
_ => (),
@@ -46,7 +58,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
},
}
}
Err(e) => {
println!("Error: {e:?}")
}
@@ -56,4 +69,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
```
See [more examples](https://github.com/mihai-dinculescu/simconnect-sdk/tree/main/examples).
See [more examples][examples].
[examples]: https://github.com/mihai-dinculescu/simconnect-sdk/tree/main/examples

View File

@@ -18,12 +18,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("Simple Program");
match client {
Ok(mut client) => loop {
Ok(mut client) => {
let mut notifications_received = 0;
loop {
let notification = client.get_next_dispatch()?;
match notification {
Some(Notification::Open) => {
println!("Open");
println!("Connection opened.");
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
@@ -31,6 +34,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
println!("Subscription stopped.");
break;
}
}
}
_ => (),
@@ -38,7 +50,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
},
}
}
Err(e) => {
println!("Error: {e:?}")
}

View File

@@ -31,7 +31,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
match notification {
Some(Notification::Open) => {
println!("Open");
println!("Connection opened.");
// After the connection is successfully open, we register the structs
client.register_object::<GpsData>()?;

View File

@@ -22,7 +22,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("Simple Program");
match client {
Ok(mut client) => loop {
Ok(mut client) => {
let mut notifications_received = 0;
loop {
let notification = client.get_next_dispatch()?;
match notification {
@@ -35,6 +38,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
info!("{gps_data:?}");
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
println!("Subscription stopped.");
break;
}
}
}
_ => (),
@@ -42,7 +54,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
},
}
}
Err(e) => {
error!("{e:?}")
}

View File

@@ -37,12 +37,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("Simple Program");
match client {
Ok(mut client) => loop {
Ok(mut client) => {
let mut notifications_received = 0;
loop {
let notification = client.get_next_dispatch()?;
match notification {
Some(Notification::Open) => {
println!("Open");
println!("Connection opened.");
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
@@ -50,6 +53,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
println!("Subscription stopped.");
break;
}
}
}
_ => (),
@@ -57,7 +69,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
},
}
}
Err(e) => {
println!("Error: {e:?}")
}

View File

@@ -9,7 +9,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
match notification {
Some(Notification::Open) => {
println!("Open");
println!("Connection opened.");
// After the connection is successfully open

View File

@@ -23,6 +23,7 @@ fn main() {
.allowlist_function("SimConnect_GetNextDispatch")
.allowlist_function("SimConnect_AddToDataDefinition")
.allowlist_function("SimConnect_RequestDataOnSimObject")
.allowlist_function("SimConnect_ClearDataDefinition")
.allowlist_function("SimConnect_SubscribeToFacilities")
.allowlist_function("SimConnect_UnsubscribeToFacilities")
.allowlist_function("SimConnect_RequestFacilitiesList")

View File

@@ -27,12 +27,15 @@
//! let client = SimConnect::new("Simple Program");
//!
//! match client {
//! Ok(mut client) => loop {
//! Ok(mut client) => {
//! let mut notifications_received = 0;
//!
//! loop {
//! let notification = client.get_next_dispatch()?;
//!
//! match notification {
//! Some(Notification::Open) => {
//! println!("Open");
//! println!("Connection opened.");
//!
//! // After the connection is successfully open, we register the struct
//! client.register_object::<GpsData>()?;
@@ -40,6 +43,15 @@
//! Some(Notification::Object(data)) => {
//! if let Ok(gps_data) = GpsData::try_from(&data) {
//! println!("{gps_data:?}");
//!
//! notifications_received += 1;
//!
//! // After we have received 10 notifications, we unregister the struct
//! if notifications_received > 10 {
//! client.unregister_object::<GpsData>()?;
//! println!("Subscription stopped.");
//! break;
//! }
//! }
//! }
//! _ => (),
@@ -47,7 +59,8 @@
//!
//! // sleep for about a frame to reduce CPU usage
//! std::thread::sleep(std::time::Duration::from_millis(16));
//! },
//! }
//! }
//! Err(e) => {
//! println!("Error: {e:?}")
//! }

View File

@@ -28,12 +28,15 @@ use crate::{
/// let client = SimConnect::new("Simple Program");
///
/// match client {
/// Ok(mut client) => loop {
/// Ok(mut client) => {
/// let mut notifications_received = 0;
///
/// loop {
/// let notification = client.get_next_dispatch()?;
///
/// match notification {
/// Some(Notification::Open) => {
/// println!("Open");
/// println!("Connection opened.");
///
/// // After the connection is successfully open, we register the struct
/// client.register_object::<GpsData>()?;
@@ -41,6 +44,15 @@ use crate::{
/// Some(Notification::Object(data)) => {
/// if let Ok(gps_data) = GpsData::try_from(&data) {
/// println!("{gps_data:?}");
///
/// notifications_received += 1;
///
/// // After we have received 10 notifications, we unregister the struct
/// if notifications_received > 10 {
/// client.unregister_object::<GpsData>()?;
/// println!("Subscription stopped.");
/// break;
/// }
/// }
/// }
/// _ => (),
@@ -48,7 +60,8 @@ use crate::{
///
/// // sleep for about a frame to reduce CPU usage
/// std::thread::sleep(std::time::Duration::from_millis(16));
/// },
/// }
/// }
/// Err(e) => {
/// println!("Error: {e:?}")
/// }
@@ -322,12 +335,12 @@ impl SimConnect {
/// Unregister a Request ID in the internal state so that the user doesn't have to manually manage Request IDs.
#[tracing::instrument(name = "SimConnect::unregister_request_id_by_type_name")]
pub(super) fn unregister_request_id_by_type_name(&mut self, type_name: String) {
self.registered_objects.remove(&type_name);
pub(super) fn unregister_request_id_by_type_name(&mut self, type_name: &str) -> Option<u32> {
self.registered_objects.remove(type_name)
}
/// Get the Type Name of a Request ID.
#[tracing::instrument(name = "SimConnect::get_request_id_by_type_name")]
#[tracing::instrument(name = "SimConnect::get_type_name_by_request_id")]
pub(super) fn get_type_name_by_request_id(&self, request_id: u32) -> Option<String> {
self.registered_objects
.iter()

View File

@@ -79,7 +79,7 @@ impl SimConnect {
));
}
self.unregister_request_id_by_type_name(type_name);
self.unregister_request_id_by_type_name(&type_name);
Ok(())
}

View File

@@ -16,6 +16,27 @@ impl SimConnect {
Ok(id)
}
// Unregister an object with SimConnect.
#[tracing::instrument(name = "SimConnect::register_object")]
pub fn unregister_object<T: SimConnectObjectExt>(&mut self) -> Result<u32, SimConnectError> {
let type_name: String = std::any::type_name::<T>().into();
let request_id = self
.registered_objects
.get(&type_name)
.ok_or_else(|| SimConnectError::ObjectNotRegistered(type_name.clone()))?;
unsafe {
success!(bindings::SimConnect_ClearDataDefinition(
self.handle.as_ptr(),
*request_id,
));
}
self.unregister_request_id_by_type_name(&type_name)
.ok_or(SimConnectError::ObjectNotRegistered(type_name))
}
/// Add a Microsoft Flight Simulator simulation variable name to a client defined object definition.
///
/// # Remarks