serialize points

This commit is contained in:
Brandon Thomas
2021-01-13 10:35:21 -05:00
parent 12ec365461
commit 8b9217b5a0
2 changed files with 34 additions and 4 deletions
+20 -4
View File
@@ -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));
},
}
}
}
+14
View File
@@ -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<u8> {
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()
}
}