From d8180bb4ed771f7a7b00c12f2eea3dd96603d857 Mon Sep 17 00:00:00 2001 From: Aleksander Date: Tue, 14 Jan 2025 22:42:18 +0100 Subject: [PATCH] WayVR: Update IPC, send runtime info, update deps --- Cargo.lock | 73 ++----------------------------- Cargo.toml | 4 +- src/backend/wayvr/server_ipc.rs | 77 ++++++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 93 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d9e70f..dbe5e0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -432,15 +432,6 @@ dependencies = [ "syn 2.0.89", ] -[[package]] -name = "atomic-polyfill" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" -dependencies = [ - "critical-section", -] - [[package]] name = "atomic-waker" version = "1.1.2" @@ -912,12 +903,6 @@ dependencies = [ "cc", ] -[[package]] -name = "cobs" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15" - [[package]] name = "codespan-reporting" version = "0.11.1" @@ -1114,12 +1099,6 @@ dependencies = [ "libc", ] -[[package]] -name = "critical-section" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" - [[package]] name = "crossbeam-deque" version = "0.8.5" @@ -1832,15 +1811,6 @@ dependencies = [ "crunchy", ] -[[package]] -name = "hash32" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" -dependencies = [ - "byteorder", -] - [[package]] name = "hashbrown" version = "0.12.3" @@ -1872,20 +1842,6 @@ dependencies = [ "hashbrown 0.14.5", ] -[[package]] -name = "heapless" -version = "0.7.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" -dependencies = [ - "atomic-polyfill", - "hash32", - "rustc_version", - "serde", - "spin", - "stable_deref_trait", -] - [[package]] name = "heck" version = "0.4.1" @@ -3284,17 +3240,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "postcard" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "170a2601f67cc9dba8edd8c4870b15f71a6a2dc196daec8c83f72b59dff628a8" -dependencies = [ - "cobs", - "heapless", - "serde", -] - [[package]] name = "ppv-lite86" version = "0.2.20" @@ -3688,9 +3633,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.133" +version = "1.0.135" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" +checksum = "2b0d7ba2887406110130a978386c4e1befb98c674b4fba677954e4db976630d9" dependencies = [ "itoa", "memchr", @@ -3888,15 +3833,6 @@ dependencies = [ "serde", ] -[[package]] -name = "spin" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" -dependencies = [ - "lock_api", -] - [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -4604,13 +4540,13 @@ dependencies = [ [[package]] name = "wayvr_ipc" version = "0.1.0" -source = "git+https://github.com/olekolek1000/wayvr-ipc.git?rev=c2a6438ffdcc78ff9c0637d914df1bc673723824#c2a6438ffdcc78ff9c0637d914df1bc673723824" +source = "git+https://github.com/olekolek1000/wayvr-ipc.git?rev=b484d1f0607b3f67e2c9e49797819738344bcb5c#b484d1f0607b3f67e2c9e49797819738344bcb5c" dependencies = [ "anyhow", "bytes", "log", - "postcard", "serde", + "serde_json", "smallvec", ] @@ -5094,7 +5030,6 @@ dependencies = [ "once_cell", "openxr", "ovr_overlay", - "postcard", "regex", "rodio", "rosc", diff --git a/Cargo.toml b/Cargo.toml index 05be73f..a5c5c4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,9 +84,8 @@ uuid = { version = "1.10.0", features = ["v4", "fast-rng"], optional = true } wayland-client = { version = "0.31.6", optional = true } wayland-egl = { version = "0.32.4", optional = true } interprocess = { version = "2.2.2", optional = true } -postcard = { version = "1.1.1", optional = true } bytes = { version = "1.9.0", optional = true } -wayvr_ipc = { git = "https://github.com/olekolek1000/wayvr-ipc.git", rev = "c2a6438ffdcc78ff9c0637d914df1bc673723824", default-features = false, optional = true } +wayvr_ipc = { git = "https://github.com/olekolek1000/wayvr-ipc.git", rev = "b484d1f0607b3f67e2c9e49797819738344bcb5c", default-features = false, optional = true } ################################ [build-dependencies] @@ -109,7 +108,6 @@ wayvr = [ "dep:wayland-client", "dep:wayland-egl", "dep:interprocess", - "dep:postcard", "dep:bytes", "dep:wayvr_ipc", ] diff --git a/src/backend/wayvr/server_ipc.rs b/src/backend/wayvr/server_ipc.rs index d412bfd..7ac1e0c 100644 --- a/src/backend/wayvr/server_ipc.rs +++ b/src/backend/wayvr/server_ipc.rs @@ -4,16 +4,21 @@ use interprocess::local_socket::{self, traits::Listener, ToNsName}; use smallvec::SmallVec; use std::io::{Read, Write}; use wayvr_ipc::{ - ipc::{self, binary_decode, binary_encode}, + ipc::{self}, packet_client::{self, PacketClient}, packet_server::{self, PacketServer}, }; +pub struct AuthInfo { + pub client_name: String, + pub protocol_version: u32, // client protocol version +} + pub struct Connection { alive: bool, conn: local_socket::Stream, next_packet: Option, - handshaking: bool, + auth: Option, } pub fn send_packet(conn: &mut local_socket::Stream, data: &[u8]) -> anyhow::Result<()> { @@ -71,7 +76,7 @@ pub fn gen_args_vec(input: &str) -> Vec<&str> { input.split_whitespace().collect() } -pub fn gen_env_vec(input: &Vec) -> Vec<(&str, &str)> { +pub fn gen_env_vec(input: &[String]) -> Vec<(&str, &str)> { let res = input .iter() .filter_map(|e| e.as_str().split_once('=')) @@ -84,19 +89,25 @@ impl Connection { Self { conn, alive: true, - handshaking: true, + auth: None, next_packet: None, } } - fn kill(&mut self) { + fn kill(&mut self, reason: &str) { + let _dont_care = send_packet( + &mut self.conn, + &ipc::data_encode(&PacketServer::Disconnect(packet_server::Disconnect { + reason: String::from(reason), + })), + ); self.alive = false; } - fn process_handshake(&mut self, payload: Payload) -> anyhow::Result<()> { - let Ok(handshake) = binary_decode::(&payload) else { - anyhow::bail!("Invalid handshake"); - }; + fn process_handshake(&mut self, handshake: &packet_client::Handshake) -> anyhow::Result<()> { + if self.auth.is_some() { + anyhow::bail!("You were already authenticated"); + } if handshake.protocol_version != ipc::PROTOCOL_VERSION { anyhow::bail!( @@ -109,8 +120,29 @@ impl Connection { anyhow::bail!("Invalid magic"); } - log::info!("Accepted new connection"); - self.handshaking = false; + match handshake.client_name.len() { + 0 => anyhow::bail!("Client name is empty"), + 1..32 => {} + _ => anyhow::bail!("Client name is too long"), + } + + log::info!("IPC: Client \"{}\" connected.", handshake.client_name); + + self.auth = Some(AuthInfo { + client_name: handshake.client_name.clone(), + protocol_version: handshake.protocol_version, + }); + + // Send auth response + send_packet( + &mut self.conn, + &ipc::data_encode(&PacketServer::HandshakeSuccess( + packet_server::HandshakeSuccess { + runtime: String::from("wlx-overlay-s"), + }, + )), + )?; + Ok(()) } @@ -136,7 +168,7 @@ impl Connection { send_packet( &mut self.conn, - &binary_encode(&PacketServer::WvrDisplayListResponse( + &ipc::data_encode(&PacketServer::WvrDisplayListResponse( serial, packet_server::WvrDisplayList { list }, )), @@ -165,7 +197,7 @@ impl Connection { send_packet( &mut self.conn, - &binary_encode(&PacketServer::WvrDisplayCreateResponse( + &ipc::data_encode(&PacketServer::WvrDisplayCreateResponse( serial, display_handle.as_packet(), )), @@ -193,7 +225,7 @@ impl Connection { send_packet( &mut self.conn, - &binary_encode(&PacketServer::WvrProcessLaunchResponse(serial, res)), + &ipc::data_encode(&PacketServer::WvrProcessLaunchResponse(serial, res)), )?; Ok(()) @@ -214,7 +246,7 @@ impl Connection { send_packet( &mut self.conn, - &binary_encode(&PacketServer::WvrDisplayGetResponse(serial, disp)), + &ipc::data_encode(&PacketServer::WvrDisplayGetResponse(serial, disp)), )?; Ok(()) @@ -242,7 +274,7 @@ impl Connection { send_packet( &mut self.conn, - &binary_encode(&PacketServer::WvrProcessListResponse( + &ipc::data_encode(&PacketServer::WvrProcessListResponse( serial, packet_server::WvrProcessList { list }, )), @@ -270,13 +302,15 @@ impl Connection { } fn process_payload(&mut self, params: &mut TickParams, payload: Payload) -> anyhow::Result<()> { - if self.handshaking { - self.process_handshake(payload)?; + let packet: PacketClient = ipc::data_decode(&payload)?; + + if let PacketClient::Handshake(handshake) = &packet { + self.process_handshake(handshake)?; return Ok(()); } - let packet: PacketClient = binary_decode(&payload)?; match packet { + PacketClient::Handshake(_) => unreachable!(), // handled previously PacketClient::WvrDisplayList(serial) => { self.handle_wvr_display_list(params, serial)?; } @@ -305,7 +339,8 @@ impl Connection { if let Err(e) = self.process_payload(params, payload) { log::error!("Invalid payload from the client, closing connection: {}", e); - self.kill(); + // send also error message directly to the client before disconnecting + self.kill(format!("{}", e).as_str()); false } else { true @@ -341,7 +376,7 @@ impl Connection { "Client sent a packet header with the size over {} bytes, closing connection.", size_limit ); - self.kill(); + self.kill("Too big packet received (over 128 KiB)"); return false; }