Skip to content

Commit

Permalink
feat: write events first
Browse files Browse the repository at this point in the history
  • Loading branch information
Sequal32 committed May 25, 2023
1 parent 3e0ab77 commit 72da96f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
38 changes: 25 additions & 13 deletions src/yourcontrols/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,25 +1479,19 @@ impl Definitions {
self.process_events(conn)
}

fn filter_all_sync(&self, data: &mut AllNeedSync, sync_permission: &SyncPermission) {
data.filter(|name| self.can_sync(name, sync_permission));
}

fn split_unreliable(&self, data: &mut AllNeedSync) -> AllNeedSync {
data.filter_keep(|name| {
self.interpolate_vars.contains(name) || self.unreliable_vars.contains(name)
})
}

pub fn get_need_sync(
&mut self,
fn filter_all_sync(
&self,
mut data: AllNeedSync,
sync_permission: &SyncPermission,
) -> (Option<AllNeedSync>, Option<AllNeedSync>) {
let mut data = AllNeedSync::new();
// Swap queued vars into local var
swap(&mut data, &mut self.current_sync);
// Filter out based on what the client's current permissions are
self.filter_all_sync(&mut data, sync_permission);
data.filter(|name| self.can_sync(name, sync_permission));
// Split into interpolated vs non interpolated values - used for reliable/unreliable transmissions
let regular = self.split_unreliable(&mut data);
// Convert into options
Expand All @@ -1511,6 +1505,25 @@ impl Definitions {
(unreliable, regular)
}

pub fn get_var_sync(
&mut self,
sync_permission: &SyncPermission,
) -> (Option<AllNeedSync>, Option<AllNeedSync>) {
let mut data = AllNeedSync::new();
data.avars = data.avars.drain().collect();
data.lvars = data.lvars.drain().collect();
self.filter_all_sync(data, sync_permission)
}

pub fn get_event_sync(
&mut self,
sync_permission: &SyncPermission,
) -> (Option<AllNeedSync>, Option<AllNeedSync>) {
let mut data = AllNeedSync::new();
data.events = data.events.drain(..).collect();
self.filter_all_sync(data, sync_permission)
}

fn can_sync(&self, var_name: &str, sync_permission: &SyncPermission) -> bool {
// Check categories
match self.categories.get(var_name) {
Expand Down Expand Up @@ -1642,12 +1655,11 @@ impl Definitions {
pub fn on_receive_data(
&mut self,
conn: &SimConnector,
data: AllNeedSync,
mut data: AllNeedSync,
time: f64,
sync_permission: &SyncPermission,
) -> Result<(), Error> {
let mut data = data;
self.filter_all_sync(&mut data, sync_permission);
data.filter(|name| self.can_sync(name, sync_permission));

// In this specific order
// Aircraft var data should overwrite any event data
Expand Down
54 changes: 28 additions & 26 deletions src/yourcontrols/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use std::{
};
use update::Updater;
use yourcontrols_net::{Client, Event, Payloads, ReceiveMessage, Server, TransferClient};
use yourcontrols_types::AllNeedSync;

use crate::util::get_hostname_ip;

Expand Down Expand Up @@ -114,12 +115,11 @@ fn start_client(
}

fn write_update_data(
definitions: &mut Definitions,
data: (Option<AllNeedSync>, Option<AllNeedSync>),
client: &mut Box<dyn TransferClient>,
permission: &SyncPermission,
log_sent: bool,
) {
let (unreliable, reliable) = definitions.get_need_sync(permission);
let (unreliable, reliable) = data;

if let Some(data) = unreliable {
client.update(data, true);
Expand Down Expand Up @@ -564,33 +564,35 @@ fn main() {
}
}

// Handle initial connection delay, allows lvars to be processed
if let Some(time) = connection_time {
if time.elapsed().as_secs() >= 3 {
// Update
let can_update = update_rate_instant.elapsed().as_secs_f64() > update_rate;

// Do not let server send initial data - wait for data to get cleared on the previous loop
if !observing && can_update && ready_to_process_data {
let permission = SyncPermission {
is_server: client.is_host(),
is_master: control.has_control(),
is_init: false,
};

write_update_data(&mut definitions, client, &permission, true);
// Handle initial 3 second connection delay, allows lvars to be processed
if let Some(true) = connection_time.map(|t| t.elapsed().as_secs() >= 3) {
// Update
let can_update_vars = update_rate_instant.elapsed().as_secs_f64() > update_rate;

// Do not let server send initial data - wait for data to get cleared on the previous loop
if !observing && ready_to_process_data {
let permission = SyncPermission {
is_server: client.is_host(),
is_master: control.has_control(),
is_init: false,
};

update_rate_instant = Instant::now();
if can_update_vars {
write_update_data(definitions.get_var_sync(&permission), client, true);
} else {
write_update_data(definitions.get_event_sync(&permission), client, true);
}

// Tell server we're ready to receive data after 3 seconds
if !ready_to_process_data {
ready_to_process_data = true;
definitions.reset_sync();
update_rate_instant = Instant::now();
}

if !client.is_host() {
client.send_ready();
}
// Tell server we're ready to receive data after 3 seconds
if !ready_to_process_data {
ready_to_process_data = true;
definitions.reset_sync();

if !client.is_host() {
client.send_ready();
}
}
}
Expand Down

0 comments on commit 72da96f

Please sign in to comment.