forge
Explore
Log in

Don't you dare go Hollow

Add framed message push over uni streams

Browse files at this commit

7af5e5256fbf0c6282a5d5a3d464a0c77eaeee4fAhmet Kaan GümüşParent: 7b4b40e
  • feat
    • add framed message push over uni streams
Show full message
✨ feat(common): add framed message push over uni streams
crates/common/src/error.rsmodified+6 -0
@@ -110,6 +110,12 @@ impl From<quinn::ReadToEndError> for Error {
110 }110 }
111 }111 }
112 112
113+impl From<quinn::ReadExactError> for Error {
114+ fn from(value: quinn::ReadExactError) -> Self {
115+ Self::Network(value.into())
116+ }
117+}
118+
113 impl From<quinn::WriteError> for Error {119 impl From<quinn::WriteError> for Error {
114 fn from(value: quinn::WriteError) -> Self {120 fn from(value: quinn::WriteError) -> Self {
115 Self::Network(value.into())121 Self::Network(value.into())
crates/common/src/error/network.rsmodified+15 -1
@@ -10,10 +10,12 @@ pub enum Error {
10 Connection(String),10 Connection(String),
11 Read(String),11 Read(String),
12 ReadToEnd(String),12 ReadToEnd(String),
13+ ReadExact(String),
13 Write(String),14 Write(String),
14 ClosedStream(String),15 ClosedStream(String),
15 ConnectionError(String),16 ConnectionError(String),
16 ChannelClosed,17 ChannelClosed,
18+ ReadBoundExceed(usize, usize),
17 }19 }
18 20
19 impl std::error::Error for Error {}21 impl std::error::Error for Error {}
@@ -27,10 +29,16 @@ impl Display for Error {
27 Error::Connection(error_value) => error_value.fmt(f),29 Error::Connection(error_value) => error_value.fmt(f),
28 Error::Read(error_value) => error_value.fmt(f),30 Error::Read(error_value) => error_value.fmt(f),
29 Error::ReadToEnd(error_value) => error_value.fmt(f),31 Error::ReadToEnd(error_value) => error_value.fmt(f),
32+ Error::ReadExact(error_value) => error_value.fmt(f),
30 Error::Write(error_value) => error_value.fmt(f),33 Error::Write(error_value) => error_value.fmt(f),
31 Error::ClosedStream(error_value) => error_value.fmt(f),34 Error::ClosedStream(error_value) => error_value.fmt(f),
32 Error::ConnectionError(error_value) => error_value.fmt(f),35 Error::ConnectionError(error_value) => error_value.fmt(f),
33 Error::ChannelClosed => write!(f, "Channel Closed"),36 Error::ChannelClosed => write!(f, "Channel Closed"),
37+ Error::ReadBoundExceed(actual, expected) => write!(
38+ f,
39+ "Read Bound Exceed | Actual = {} | Expected = {}",
40+ actual, expected
41+ ),
34 }42 }
35 }43 }
36 }44 }
@@ -67,7 +75,13 @@ impl From<quinn::ReadError> for Error {
67 75
68 impl From<quinn::ReadToEndError> for Error {76 impl From<quinn::ReadToEndError> for Error {
69 fn from(value: quinn::ReadToEndError) -> Self {77 fn from(value: quinn::ReadToEndError) -> Self {
70- Self::Read(value.to_string())78+ Self::ReadToEnd(value.to_string())
79+ }
80+}
81+
82+impl From<quinn::ReadExactError> for Error {
83+ fn from(value: quinn::ReadExactError) -> Self {
84+ Self::ReadExact(value.to_string())
71 }85 }
72 }86 }
73 87
crates/common/src/network.rsmodified+36 -1
@@ -1,6 +1,9 @@
1 use quinn::{RecvStream, SendStream};1 use quinn::{RecvStream, SendStream};
2 2
3-use crate::{constant::MAXIMUM_MESSAGE_LENGTH, error::Error, request::Request, response::Response};3+use crate::{
4+ constant::MAXIMUM_MESSAGE_LENGTH, error::Error, message::Message, request::Request,
5+ response::Response,
6+};
4 7
5 pub const AUTHENTICATION_MAX_READ_LENGHT: usize = 64;8 pub const AUTHENTICATION_MAX_READ_LENGHT: usize = 64;
6 pub const REQUEST_MAX_READ_LENGHT: usize = MAXIMUM_MESSAGE_LENGTH * 2;9 pub const REQUEST_MAX_READ_LENGHT: usize = MAXIMUM_MESSAGE_LENGTH * 2;
@@ -43,4 +46,36 @@ impl Network {
43 46
44 Ok(bitcode::decode::<Response>(&response)?)47 Ok(bitcode::decode::<Response>(&response)?)
45 }48 }
49+
50+ pub async fn send_message(
51+ message: &Message,
52+ send_stream: &mut SendStream,
53+ ) -> Result<(), Error> {
54+ let payload = bitcode::encode(message);
55+ send_stream
56+ .write_all(&(payload.len() as u32).to_le_bytes())
57+ .await?;
58+ send_stream.write_all(&payload).await?;
59+
60+ Ok(())
61+ }
62+
63+ pub async fn receive_message(receive_stream: &mut RecvStream) -> Result<Message, Error> {
64+ let mut length_bytes = [0u8; 4];
65+ receive_stream.read_exact(&mut length_bytes).await?;
66+
67+ let length = u32::from_le_bytes(length_bytes) as usize;
68+ if length > RESPONCE_MAX_READ_LENGHT {
69+ return Err(crate::error::network::Error::ReadBoundExceed(
70+ length,
71+ RESPONCE_MAX_READ_LENGHT,
72+ )
73+ .into());
74+ }
75+
76+ let mut payload = vec![0u8; length];
77+ receive_stream.read_exact(&mut payload).await?;
78+
79+ Ok(bitcode::decode(&payload)?)
80+ }
46 }81 }