From 8b9217b5a02d83b419a85275910ea6cd52b72f84 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 13 Jan 2021 10:35:21 -0500 Subject: [PATCH] serialize points --- src/main.rs | 24 ++++++++++++++++++++---- src/point.rs | 14 ++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1e0fc7b..88a06bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,31 @@ mod point; use point::Point; +use std::thread; +use zmq::Error; +use std::time::Duration; fn main() { let ctx = zmq::Context::new(); let socket = ctx.socket(zmq::REQ).unwrap(); - let point = Point::white_at(0.0f32, 0.0f32, 0.0f32); - - socket.connect("tcp://127.0.0.1:1234").unwrap(); - socket.send("hello world!", 0).unwrap(); + //socket.send("hello world!", 0).unwrap(); + + loop { + let point = Point::white_at(0.0f32, 0.0f32, 0.0f32); + let bytes = point.to_bytes(); + + //let result = socket.send("hello world!", 0); + let result = socket.send(&bytes, 0); + + match result { + Ok(_) => continue, + Err(e) => { + eprintln!("Error: {:?}", e); + thread::sleep(Duration::from_millis(5_000)); + }, + } + } } diff --git a/src/point.rs b/src/point.rs index 3f5c76f..d036659 100644 --- a/src/point.rs +++ b/src/point.rs @@ -1,3 +1,4 @@ +use bytes::{BytesMut, BufMut}; pub struct Point { pub x: f32, @@ -24,4 +25,17 @@ impl Point { a: 1.0f32, } } + + pub fn to_bytes(&self) -> Vec { + let mut buf = BytesMut::with_capacity(32); + buf.put_f32(self.x); + buf.put_f32(self.y); + buf.put_f32(self.z); + buf.put_f32(0.0f32); // Unused + buf.put_f32(self.r); + buf.put_f32(self.g); + buf.put_f32(self.b); + buf.put_f32(self.a); + buf.to_vec() + } } \ No newline at end of file