diff --git a/Cargo.lock b/Cargo.lock index 87bcb55..86e3aae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,12 +87,6 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "block" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" - [[package]] name = "block-buffer" version = "0.9.0" @@ -192,18 +186,6 @@ dependencies = [ "cipher", ] -[[package]] -name = "current_locale" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0be8ddcccda8be68d8e31a421ceea7c79857404daa052434ae30ce2f402cd10" -dependencies = [ - "libc", - "objc", - "objc-foundation", - "winapi", -] - [[package]] name = "digest" version = "0.9.0" @@ -572,15 +554,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "malloc_buf" -version = "0.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" -dependencies = [ - "libc", -] - [[package]] name = "memchr" version = "2.5.0" @@ -658,35 +631,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b2b2cbbfd8defa51ff24450a61d73b3ff3e158484ddd274a883e886e6fbaa78" -[[package]] -name = "objc" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" -dependencies = [ - "malloc_buf", -] - -[[package]] -name = "objc-foundation" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9" -dependencies = [ - "block", - "objc", - "objc_id", -] - -[[package]] -name = "objc_id" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b" -dependencies = [ - "objc", -] - [[package]] name = "once_cell" version = "1.14.0" @@ -1239,12 +1183,11 @@ dependencies = [ [[package]] name = "usdpl-back" -version = "0.9.0" +version = "0.9.1" dependencies = [ "async-recursion", "async-trait", "bytes", - "current_locale", "gettext-ng", "hex", "log", @@ -1275,7 +1218,7 @@ dependencies = [ [[package]] name = "usdpl-front" -version = "0.9.0" +version = "0.9.1" dependencies = [ "console_error_panic_hook", "hex", diff --git a/Cargo.toml b/Cargo.toml index 9830661..85b6d98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "usdpl-rs" +name = "usdpl" version = "0.9.0" authors = ["NGnius (Graham) "] edition = "2021" diff --git a/README.md b/README.md index ca9f744..bec629b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ A faster, safer way to write plugin back-ends - [x] External API documentation - [ ] Internal protocol documentation - [x] Async support -- [ ] Encryption +- [x] Encryption - [x] PluginLoader/Decky support - [x] Plugin template - [ ] Crankshaft support diff --git a/usdpl-back/Cargo.toml b/usdpl-back/Cargo.toml index dafdd0a..59e4417 100644 --- a/usdpl-back/Cargo.toml +++ b/usdpl-back/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "usdpl-back" -version = "0.9.0" +version = "0.9.1" edition = "2021" license = "GPL-3.0-only" repository = "https://github.com/NGnius/usdpl-rs" diff --git a/usdpl-back/src/instance.rs b/usdpl-back/src/instance.rs index a9284eb..76f64f3 100644 --- a/usdpl-back/src/instance.rs +++ b/usdpl-back/src/instance.rs @@ -9,7 +9,7 @@ use usdpl_core::{socket, RemoteCallResponse}; use super::{Callable, MutCallable, AsyncCallable, WrappedCallable}; static LAST_ID: AtomicU64 = AtomicU64::new(0); -const MAX_ID_DIFFERENCE: u64 = 5; +const MAX_ID_DIFFERENCE: u64 = 32; //type WrappedCallable = Arc>>; // thread-safe, cloneable Callable @@ -94,11 +94,16 @@ impl Instance { socket::Packet::Call(call) => { log::debug!("Got USDPL call {} (`{}`, params: {})", call.id, call.function, call.parameters.len()); let last_id = LAST_ID.load(Ordering::SeqCst); - if call.id == 0 { - log::info!("Call ID is 0, assuming new connection (resetting last id)"); - LAST_ID.store(0, Ordering::SeqCst); + if last_id == 0 { + log::info!("Last id is 0, assuming resumed connection (overriding last id)"); + LAST_ID.store(call.id, Ordering::SeqCst); + } else if call.id < MAX_ID_DIFFERENCE { + log::info!("Call ID is low, assuming new connection (resetting last id)"); + LAST_ID.store(call.id, Ordering::SeqCst); } else if call.id > last_id && call.id - last_id < MAX_ID_DIFFERENCE { LAST_ID.store(call.id, Ordering::SeqCst); + } else if call.id < last_id && last_id - call.id < MAX_ID_DIFFERENCE { + // Allowed, but don't store new (lower) LAST_ID } else { #[cfg(not(debug_assertions))] { diff --git a/usdpl-front/Cargo.toml b/usdpl-front/Cargo.toml index bd0611a..f897fe8 100644 --- a/usdpl-front/Cargo.toml +++ b/usdpl-front/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "usdpl-front" -version = "0.9.0" +version = "0.9.1" authors = ["NGnius (Graham) "] edition = "2021" license = "GPL-3.0-only" diff --git a/usdpl-front/src/lib.rs b/usdpl-front/src/lib.rs index 4aa80b3..056d96e 100644 --- a/usdpl-front/src/lib.rs +++ b/usdpl-front/src/lib.rs @@ -9,6 +9,8 @@ mod connection; mod convert; mod imports; +use std::sync::atomic::{AtomicU64, Ordering}; + use js_sys::Array; use wasm_bindgen::prelude::*; @@ -18,7 +20,7 @@ use usdpl_core::{socket::Packet, RemoteCall}; static mut CTX: UsdplContext = UsdplContext { port: 31337, - id: 1, + id: AtomicU64::new(0), #[cfg(feature = "encrypt")] key: Vec::new(), }; @@ -37,7 +39,7 @@ fn encryption_key() -> Vec { #[derive(Debug)] struct UsdplContext { port: u16, - id: u64, + id: AtomicU64, #[cfg(feature = "encrypt")] key: Vec, } @@ -52,11 +54,8 @@ fn get_key() -> Vec { } fn increment_id() -> u64 { - let current_id = unsafe { CTX.id }; - unsafe { - CTX.id += 1; - } - current_id + let atomic = unsafe { &CTX.id }; + atomic.fetch_add(1, Ordering::SeqCst) } /// Initialize the front-end library @@ -68,7 +67,7 @@ pub fn init_usdpl(port: u16) { unsafe { CTX = UsdplContext { port: port, - id: 1, + id: AtomicU64::new(0), #[cfg(feature = "encrypt")] key: encryption_key(), };