changeset 19:ba09079686a0

Add support for different endianness between client and server.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 26 Aug 2021 23:43:36 +0200
parents 3f7b7a3ad8fe
children f81d56c5da76
files Cargo.toml src/bin/client.rs src/bin/server.rs src/protocol.rs
diffstat 4 files changed, 10 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- 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 }
--- a/src/bin/client.rs
+++ b/src/bin/client.rs
@@ -66,10 +66,9 @@ fn build_ui(app: &gtk::Application) {
     println!("opened {:?}", socket);
 
     let send = move |event: &Event| {
-        // TODO: Yolo-alignment.
-        let buf: &[u8; std::mem::size_of::<Event>()] = 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::<Event>());
     };
     let send2 = send.clone();
--- 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::<Event>() {
             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);
--- 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 <https://www.gnu.org/licenses/>.
 
+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),