mirror of
https://github.com/storytold/point-generator.git
synced 2026-10-09 00:09:43 +00:00
serialize points
This commit is contained in:
+20
-4
@@ -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));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user