From 68b7455c9e6720dbbef36bf7697fb9240950f19e Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Wed, 28 Jun 2023 22:21:56 -0400 Subject: [PATCH] Make front WS service immutable to avoid wasm-rust exceptions, add debug logging --- Cargo.lock | 24 ++++++++++++++++------ usdpl-back/src/websockets.rs | 16 +++++++++++++-- usdpl-build/src/front/mod.rs | 15 ++++++++++++++ usdpl-build/src/front/service_generator.rs | 2 +- usdpl-front/Cargo.toml | 5 +++-- usdpl-front/src/client_handler.rs | 2 +- usdpl-front/src/lib.rs | 2 ++ 7 files changed, 54 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42ea19a..44f28bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -183,6 +183,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "console_log" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be8aed40e4edbf4d3b4431ab260b63fdc40f5780a4766824329ea0f1eefe3c0f" +dependencies = [ + "log", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -797,6 +808,8 @@ checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" [[package]] name = "nrpc" version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f41caeb65399490c6f68ab2527a833d6f2e9b0d7d5ffc4b062f1484b3fa61cd" dependencies = [ "async-trait", "bytes", @@ -805,9 +818,7 @@ dependencies = [ [[package]] name = "nrpc" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f41caeb65399490c6f68ab2527a833d6f2e9b0d7d5ffc4b062f1484b3fa61cd" +version = "0.7.0" dependencies = [ "async-trait", "bytes", @@ -818,7 +829,7 @@ dependencies = [ name = "nrpc-build" version = "0.7.0" dependencies = [ - "nrpc 0.6.0", + "nrpc 0.7.0", "prettyplease 0.2.4", "proc-macro2", "prost-build", @@ -1502,7 +1513,7 @@ dependencies = [ "gettext-ng", "hex", "log", - "nrpc 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "nrpc 0.6.0", "obfstr", "prost", "ratchet_rs", @@ -1539,12 +1550,13 @@ version = "0.11.0" dependencies = [ "async-channel", "console_error_panic_hook", + "console_log", "futures", "gloo-net", "hex", "js-sys", "log", - "nrpc 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "nrpc 0.7.0", "obfstr", "prost", "usdpl-core", diff --git a/usdpl-back/src/websockets.rs b/usdpl-back/src/websockets.rs index 5fbed68..9b30695 100644 --- a/usdpl-back/src/websockets.rs +++ b/usdpl-back/src/websockets.rs @@ -46,7 +46,7 @@ impl WebsocketServer { let tcp = TcpListener::bind(addr).await?; while let Ok((stream, _addr_do_not_use)) = tcp.accept().await { - tokio::spawn(Self::connection_handler(self.services.clone(), stream)); + tokio::spawn(error_logger("USDPL websocket server error", Self::connection_handler(self.services.clone(), stream))); } Ok(()) @@ -65,6 +65,7 @@ impl WebsocketServer { services: ServiceRegistry<'static>, stream: TcpStream, ) -> Result<(), RatchetError> { + log::debug!("connection_handler invoked!"); let upgraded = ratchet_rs::accept_with( stream, WebSocketConfig::default(), @@ -77,6 +78,8 @@ impl WebsocketServer { let request_path = upgraded.request.uri().path(); + log::debug!("accepted new connection on uri {}", request_path); + let mut websocket = upgraded.websocket; let descriptor = Self::parse_uri_path(request_path) @@ -92,6 +95,7 @@ impl WebsocketServer { )) } Message::Binary => { + log::debug!("got binary ws message on uri {}", request_path); let response = services .call_descriptor( descriptor.service, @@ -102,6 +106,7 @@ impl WebsocketServer { .map_err(|e| { RatchetError::with_cause(ratchet_rs::ErrorKind::Protocol, e.to_string()) })?; + log::debug!("service completed response on uri {}", request_path); websocket.write_binary(response).await?; } Message::Ping(x) => websocket.write_pong(x).await?, @@ -109,11 +114,12 @@ impl WebsocketServer { Message::Close(_) => break, } } + log::debug!("ws connection {} closed", request_path); Ok(()) } fn parse_uri_path<'a>(path: &'a str) -> Result, &'static str> { - let mut iter = path.split('/'); + let mut iter = path.trim_matches('/').split('/'); if let Some(service) = iter.next() { if let Some(method) = iter.next() { if iter.next().is_none() { @@ -129,3 +135,9 @@ impl WebsocketServer { } } } + +async fn error_logger(msg: &'static str, f: impl core::future::Future>) { + if let Err(e) = f.await { + log::error!("{}: {}", msg, e); + } +} diff --git a/usdpl-build/src/front/mod.rs b/usdpl-build/src/front/mod.rs index b9a6f29..d706453 100644 --- a/usdpl-build/src/front/mod.rs +++ b/usdpl-build/src/front/mod.rs @@ -28,3 +28,18 @@ pub fn build( .transpile() .unwrap() } + +pub fn build_min( + custom_protos: impl Iterator, + custom_dirs: impl Iterator, +) { + crate::dump_protos_out().unwrap(); + nrpc_build::Transpiler::new( + crate::all_proto_filenames(crate::proto_builtins_out_path(), custom_protos), + crate::proto_out_paths(custom_dirs), + ) + .unwrap() + .generate_client() + .transpile() + .unwrap() +} diff --git a/usdpl-build/src/front/service_generator.rs b/usdpl-build/src/front/service_generator.rs index 4297795..ce6eeda 100644 --- a/usdpl-build/src/front/service_generator.rs +++ b/usdpl-build/src/front/service_generator.rs @@ -80,7 +80,7 @@ fn generate_service_methods( gen_methods.push(quote::quote! { #[wasm_bindgen] - pub async fn #method_name(&mut self, #(#input_params)*) -> Option<#method_output> { + pub async fn #method_name(&self, #(#input_params)*) -> Option<#method_output> { #params_to_fields_transformer diff --git a/usdpl-front/Cargo.toml b/usdpl-front/Cargo.toml index 36ad139..93d7970 100644 --- a/usdpl-front/Cargo.toml +++ b/usdpl-front/Cargo.toml @@ -14,7 +14,7 @@ crate-type = ["cdylib", "rlib"] [features] default = [] decky = ["usdpl-core/decky"] -debug = ["console_error_panic_hook"] +debug = ["console_error_panic_hook", "console_log"] encrypt = ["usdpl-core/encrypt", "obfstr", "hex"] [dependencies] @@ -22,6 +22,7 @@ wasm-bindgen = "0.2" wasm-bindgen-futures = "0.4" gloo-net = { version = "0.2", features = ["websocket"] } futures = "0.3" +console_log = { version = "1.0", optional = true, features = ["color"] } # The `console_error_panic_hook` crate provides better debugging of panics by # logging them with `console.error`. This is great for development, but requires @@ -44,7 +45,7 @@ async-channel = "1.8" obfstr = { version = "0.3", optional = true } hex = { version = "0.4", optional = true } -nrpc = "0.6" +nrpc = { version = "0.7", path = "../../nRPC/nrpc" } usdpl-core = { version = "0.11", path = "../usdpl-core" } prost = "0.11" log = "0.4" diff --git a/usdpl-front/src/client_handler.rs b/usdpl-front/src/client_handler.rs index 7d8fe94..74e0089 100644 --- a/usdpl-front/src/client_handler.rs +++ b/usdpl-front/src/client_handler.rs @@ -54,7 +54,7 @@ impl WebSocketHandler { #[async_trait::async_trait] impl ClientHandler for WebSocketHandler { async fn call( - &mut self, + &self, package: &str, service: &str, method: &str, diff --git a/usdpl-front/src/lib.rs b/usdpl-front/src/lib.rs index af5f3f6..db31334 100644 --- a/usdpl-front/src/lib.rs +++ b/usdpl-front/src/lib.rs @@ -78,6 +78,8 @@ fn increment_id() -> u64 { pub fn init_usdpl(port: u16) { #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); + #[cfg(feature = "console_log")] + console_log::init_with_level(log::Level::Debug).expect("USDPL: error initializing console log"); //REMOTE_PORT.store(port, std::sync::atomic::Ordering::SeqCst); unsafe { CTX = UsdplContext {