diff --git a/Cargo.toml b/Cargo.toml index aef0d9f5..336133b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -128,6 +128,7 @@ iced_widget = { version = "0.13.0-dev", path = "widget" } iced_winit = { version = "0.13.0-dev", path = "winit" } async-std = "1.0" +bincode = "1.3" bitflags = "2.0" bytemuck = { version = "1.0", features = ["derive"] } cosmic-text = "0.10" @@ -151,7 +152,6 @@ raw-window-handle = "0.6" resvg = "0.36" rustc-hash = "1.0" serde = "1.0" -serde_json = "1.0" semver = "1.0" smol = "1.0" smol_str = "0.2" diff --git a/sentinel/Cargo.toml b/sentinel/Cargo.toml index c34c5048..0b22fbcd 100644 --- a/sentinel/Cargo.toml +++ b/sentinel/Cargo.toml @@ -16,7 +16,7 @@ iced_core.workspace = true iced_style.workspace = true iced_style.features = ["serde"] -serde_json.workspace = true +bincode.workspace = true futures.workspace = true log.workspace = true diff --git a/sentinel/src/client.rs b/sentinel/src/client.rs index 5792dc85..ec370088 100644 --- a/sentinel/src/client.rs +++ b/sentinel/src/client.rs @@ -69,16 +69,11 @@ async fn send( stream: &mut io::BufStream, input: Input, ) -> Result<(), io::Error> { - stream - .write_all( - format!( - "{}\n", - serde_json::to_string(&input).expect("Serialize input message") - ) - .as_bytes(), - ) - .await?; + let bytes = bincode::serialize(&input).expect("Encode input message"); + let size = bytes.len() as u64; + stream.write_all(&size.to_be_bytes()).await?; + stream.write_all(&bytes).await?; stream.flush().await?; Ok(()) diff --git a/sentinel/src/lib.rs b/sentinel/src/lib.rs index c645ea49..e1446951 100644 --- a/sentinel/src/lib.rs +++ b/sentinel/src/lib.rs @@ -11,7 +11,7 @@ use futures::future; use futures::stream::{self, Stream, StreamExt}; use semver::Version; use serde::{Deserialize, Serialize}; -use tokio::io::{self, AsyncBufReadExt, BufStream}; +use tokio::io::{self, AsyncReadExt, BufStream}; use tokio::net; pub const SOCKET_ADDRESS: &str = "127.0.0.1:9167"; @@ -74,33 +74,34 @@ async fn connect() -> Result { async fn receive( mut stream: BufStream, ) -> Result<(BufStream, Event), io::Error> { - let mut input = String::new(); + let mut bytes = Vec::new(); loop { - match stream.read_line(&mut input).await? { - 0 => return Ok((stream, Event::Disconnected)), - n => { - match serde_json::from_str(&input[..n]) { - Ok(input) => { - return Ok(( - stream, - match dbg!(input) { - Input::Connected(version) => { - Event::Connected(version) - } - Input::TimingMeasured(timing) => { - Event::TimingMeasured(timing) - } - Input::ThemeChanged(palette) => { - Event::ThemeChanged(palette) - } - }, - )) - } - Err(_) => { - // TODO: Log decoding error - } - } + let size = stream.read_u64().await? as usize; + + if bytes.len() < size { + bytes.resize(size, 0); + } + + let _n = stream.read_exact(&mut bytes[..size]).await?; + + match bincode::deserialize(&bytes) { + Ok(input) => { + return Ok(( + stream, + match dbg!(input) { + Input::Connected(version) => Event::Connected(version), + Input::TimingMeasured(timing) => { + Event::TimingMeasured(timing) + } + Input::ThemeChanged(palette) => { + Event::ThemeChanged(palette) + } + }, + )); + } + Err(_) => { + // TODO: Log decoding error } } }