From 576ba23509e1b3e2322a7c224f4d35f94a5c1bfe Mon Sep 17 00:00:00 2001 From: Raine Date: Sun, 10 Mar 2024 19:34:13 +0100 Subject: [PATCH] feat: io --- domo_lib/Cargo.lock | 4 ++-- domo_lib/src/io.rs | 21 +++++++++++++++++++++ domo_lib/src/lib.rs | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 domo_lib/src/io.rs diff --git a/domo_lib/Cargo.lock b/domo_lib/Cargo.lock index 8db798c..827ce4b 100644 --- a/domo_lib/Cargo.lock +++ b/domo_lib/Cargo.lock @@ -19,14 +19,14 @@ dependencies = [ [[package]] name = "domo_lib" -version = "0.3.0" +version = "0.3.1" dependencies = [ "domo_proto", ] [[package]] name = "domo_proto" -version = "1.0.2" +version = "1.0.3" dependencies = [ "crc32fast", "rand", diff --git a/domo_lib/src/io.rs b/domo_lib/src/io.rs new file mode 100644 index 0000000..d40fe6e --- /dev/null +++ b/domo_lib/src/io.rs @@ -0,0 +1,21 @@ +use std::io; +use std::io::Read; +use domo_proto::packet::{Packet, PACKET_HEADER_SIZE}; + +pub fn try_read_packet(read_handle: &mut dyn Read) -> io::Result { + let mut header = [0_u8; PACKET_HEADER_SIZE]; + read_handle.read(header.as_mut_slice())?; + if header[0] != 1 { + return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid version")); + } + let mut body = vec![]; + body.reserve(u16::from_be_bytes([header[0x12], header[0x13]]) as usize); + read_handle.read_exact(&mut body)?; + + Packet::try_from({ + let mut data = vec![]; + data.extend(header); + data.extend(body); + data + }) +} \ No newline at end of file diff --git a/domo_lib/src/lib.rs b/domo_lib/src/lib.rs index 57dc335..91e82a4 100644 --- a/domo_lib/src/lib.rs +++ b/domo_lib/src/lib.rs @@ -1,4 +1,6 @@ /// Node abstraction pub mod node; /// State -pub mod state; \ No newline at end of file +pub mod state; +/// IO +pub mod io; \ No newline at end of file