diff --git a/crates/common/src/error.rs b/crates/common/src/error.rs index 641766a..9a4a3d3 100644 --- a/crates/common/src/error.rs +++ b/crates/common/src/error.rs @@ -110,6 +110,12 @@ impl From for Error { } } +impl From for Error { + fn from(value: quinn::ReadExactError) -> Self { + Self::Network(value.into()) + } +} + impl From for Error { fn from(value: quinn::WriteError) -> Self { Self::Network(value.into()) diff --git a/crates/common/src/error/network.rs b/crates/common/src/error/network.rs index 53f1600..57e6178 100644 --- a/crates/common/src/error/network.rs +++ b/crates/common/src/error/network.rs @@ -10,10 +10,12 @@ pub enum Error { Connection(String), Read(String), ReadToEnd(String), + ReadExact(String), Write(String), ClosedStream(String), ConnectionError(String), ChannelClosed, + ReadBoundExceed(usize, usize), } impl std::error::Error for Error {} @@ -27,10 +29,16 @@ impl Display for Error { Error::Connection(error_value) => error_value.fmt(f), Error::Read(error_value) => error_value.fmt(f), Error::ReadToEnd(error_value) => error_value.fmt(f), + Error::ReadExact(error_value) => error_value.fmt(f), Error::Write(error_value) => error_value.fmt(f), Error::ClosedStream(error_value) => error_value.fmt(f), Error::ConnectionError(error_value) => error_value.fmt(f), Error::ChannelClosed => write!(f, "Channel Closed"), + Error::ReadBoundExceed(actual, expected) => write!( + f, + "Read Bound Exceed | Actual = {} | Expected = {}", + actual, expected + ), } } } @@ -67,7 +75,13 @@ impl From for Error { impl From for Error { fn from(value: quinn::ReadToEndError) -> Self { - Self::Read(value.to_string()) + Self::ReadToEnd(value.to_string()) + } +} + +impl From for Error { + fn from(value: quinn::ReadExactError) -> Self { + Self::ReadExact(value.to_string()) } } diff --git a/crates/common/src/network.rs b/crates/common/src/network.rs index 9c3e3eb..0ea91f5 100644 --- a/crates/common/src/network.rs +++ b/crates/common/src/network.rs @@ -1,6 +1,9 @@ use quinn::{RecvStream, SendStream}; -use crate::{constant::MAXIMUM_MESSAGE_LENGTH, error::Error, request::Request, response::Response}; +use crate::{ + constant::MAXIMUM_MESSAGE_LENGTH, error::Error, message::Message, request::Request, + response::Response, +}; pub const AUTHENTICATION_MAX_READ_LENGHT: usize = 64; pub const REQUEST_MAX_READ_LENGHT: usize = MAXIMUM_MESSAGE_LENGTH * 2; @@ -43,4 +46,36 @@ impl Network { Ok(bitcode::decode::(&response)?) } + + pub async fn send_message( + message: &Message, + send_stream: &mut SendStream, + ) -> Result<(), Error> { + let payload = bitcode::encode(message); + send_stream + .write_all(&(payload.len() as u32).to_le_bytes()) + .await?; + send_stream.write_all(&payload).await?; + + Ok(()) + } + + pub async fn receive_message(receive_stream: &mut RecvStream) -> Result { + let mut length_bytes = [0u8; 4]; + receive_stream.read_exact(&mut length_bytes).await?; + + let length = u32::from_le_bytes(length_bytes) as usize; + if length > RESPONCE_MAX_READ_LENGHT { + return Err(crate::error::network::Error::ReadBoundExceed( + length, + RESPONCE_MAX_READ_LENGHT, + ) + .into()); + } + + let mut payload = vec![0u8; length]; + receive_stream.read_exact(&mut payload).await?; + + Ok(bitcode::decode(&payload)?) + } }