# HG changeset patch # User Emmanuel Gil Peyrot # Date 1630014216 -7200 # Node ID ba09079686a0b470d2bf51f95b72e27483189833 # Parent 3f7b7a3ad8fe4c42e35802a81984177d3bf49f0a Add support for different endianness between client and server. diff --git a/Cargo.toml b/Cargo.toml --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,8 @@ gui = ["cairo-rs", "gdk", "gio", "glib", [dependencies] input-linux = "0.3.0" bitflags = "1.2.1" +serde = { version = "1.0", features = ["derive"] } +bincode = "1.3" cairo-rs = { version = "0.14", optional = true } gdk = { version = "0.14", optional = true } gio = { version = "0.14", optional = true } diff --git a/src/bin/client.rs b/src/bin/client.rs --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -66,10 +66,9 @@ fn build_ui(app: >k::Application) { println!("opened {:?}", socket); let send = move |event: &Event| { - // TODO: Yolo-alignment. - let buf: &[u8; std::mem::size_of::()] = unsafe { std::mem::transmute(event) }; + let buf = bincode::serialize(event).unwrap(); let socket = socket.lock().unwrap(); - let amount = socket.send_to(buf, address).unwrap(); + let amount = socket.send_to(&buf, address).unwrap(); assert_eq!(amount, std::mem::size_of::()); }; let send2 = send.clone(); diff --git a/src/bin/server.rs b/src/bin/server.rs --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -43,17 +43,16 @@ pub fn run_server(address: &str) -> io:: println!("Listening on {:?}", socket); println!("Here is an example client: https://hg.linkmauve.fr/remote-gamepad"); - let mut event: Event = Default::default(); let mut last = Some((0., 0.)); state.set_size(320, 240); loop { - // TODO: Yolo-alignment. - let buf: &mut [u8; 16] = unsafe { std::mem::transmute(&mut event) }; - let (amount, source) = socket.recv_from(buf)?; + let mut buf: [u8; 16] = Default::default(); + let (amount, source) = socket.recv_from(&mut buf)?; if amount != std::mem::size_of::() { eprintln!("Invalid data length: {}", amount); continue; } + let event: Event = bincode::deserialize(&buf).unwrap(); println!("{:?} from {:?}", event, source); if event.buttons.contains(Buttons::A) { state.select_tool(Key::ButtonToolPen); diff --git a/src/protocol.rs b/src/protocol.rs --- a/src/protocol.rs +++ b/src/protocol.rs @@ -14,11 +14,12 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +use serde::{Serialize, Deserialize}; use bitflags::bitflags; bitflags! { /// This is the memory layout of the buttons on the 3DS. - #[derive(Default)] + #[derive(Serialize, Deserialize, Default)] pub struct Buttons: u32 { const A = 0x00000001; const B = 0x00000002; @@ -55,7 +56,7 @@ bitflags! { } } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] pub struct Event { pub buttons: Buttons, pad: (i16, i16),