aboutsummaryrefslogtreecommitdiff
path: root/utils/src/protocol.rs
diff options
context:
space:
mode:
Diffstat (limited to 'utils/src/protocol.rs')
-rw-r--r--utils/src/protocol.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/utils/src/protocol.rs b/utils/src/protocol.rs
new file mode 100644
index 0000000..4820f79
--- /dev/null
+++ b/utils/src/protocol.rs
@@ -0,0 +1,44 @@
+use std::convert::TryInto;
+use std::io::{self, BufReader, ErrorKind, Read};
+use std::net::TcpStream;
+
+pub enum MessageBody {
+ Version(u32),
+ NewCore(String, Vec<u8>),
+ Job(u64, Vec<u8>),
+}
+
+pub enum Reply {
+ Version(bool),
+ NewCore,
+ Job(i32, Vec<u8>),
+}
+
+pub struct RawMessage {
+ pub typ: u8,
+ pub id: u64,
+ pub payload: Vec<u8>,
+}
+
+impl RawMessage {
+ pub fn receive(reader: &mut BufReader<TcpStream>) -> io::Result<Option<Self>> {
+ let mut header = [0u8; 17];
+ if let Err(e) = reader.read(&mut header) {
+ if e.kind() == ErrorKind::UnexpectedEof { return Ok(None); }
+ else { return Err(e); }
+ }
+
+ let typ = header[0];
+ let id = u64::from_le_bytes(header[1..9].try_into().unwrap());
+ let length = usize::from_le_bytes(header[9..17].try_into().unwrap());
+
+ let mut payload = Vec::new();
+ payload.resize(length, 0u8);
+ if let Err(e) = reader.read(&mut payload) {
+ if e.kind() == ErrorKind::UnexpectedEof { return Ok(None); }
+ else { return Err(e); }
+ }
+
+ Ok(Some(Self { typ, id, payload }))
+ }
+}