From fc402438f898af8efb659aae344b2d8cb85dccab Mon Sep 17 00:00:00 2001 From: michael-bailey Date: Thu, 18 Apr 2024 12:47:36 +0100 Subject: [PATCH] moved protocol crate to foundation, created helper functions for encoding and decoding --- foundation/Cargo.toml | 1 + foundation/src/lib.rs | 1 + foundation/src/networking/mod.rs | 75 ++++++++++++++++++++++++++++++++ protocol/src/proto/network.proto | 5 ++- 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 foundation/src/networking/mod.rs diff --git a/foundation/Cargo.toml b/foundation/Cargo.toml index e2ca9ce..cee5c78 100644 --- a/foundation/Cargo.toml +++ b/foundation/Cargo.toml @@ -25,4 +25,5 @@ uuid = {version = "1.1.2", features = ["serde", "v4"]} tokio = { version = "1.9.0", features = ["full"] } serde = { version = "1.0", features = ["derive"] } +prost.workspace = true protocol = { path = '../protocol' } \ No newline at end of file diff --git a/foundation/src/lib.rs b/foundation/src/lib.rs index 715a878..01bc78c 100644 --- a/foundation/src/lib.rs +++ b/foundation/src/lib.rs @@ -1,5 +1,6 @@ pub mod messages; pub mod models; +pub mod networking; pub mod prelude; use serde::{Deserialize, Serialize}; diff --git a/foundation/src/networking/mod.rs b/foundation/src/networking/mod.rs new file mode 100644 index 0000000..5a06a67 --- /dev/null +++ b/foundation/src/networking/mod.rs @@ -0,0 +1,75 @@ +use std::io::{self, ErrorKind}; + +use prost::{ + bytes::{BufMut, Bytes, BytesMut}, + Message, +}; +use tokio::{ + io::{AsyncReadExt, AsyncWriteExt}, + net::TcpStream, +}; + +pub async fn write_message( + stream: &mut TcpStream, + message: T, +) -> io::Result<()> +where + T: Message + Default, +{ + let message = encode_message::(&message)?; + stream.write_all(&message).await?; + Ok(()) +} + +pub fn encode_message(msg: &T) -> io::Result +where + T: Message, +{ + let length = msg.encoded_len(); + let mut buffer = BytesMut::with_capacity(4 + length); + buffer.put_u32(length as u32); + let encode_result = msg.encode(&mut buffer); + if let Err(err) = encode_result { + return Err(io::Error::new( + ErrorKind::InvalidInput, + format!("message encoding failed: {:?}", err), + )); + } + + Ok(buffer.into()) +} + +pub async fn read_message(stream: &mut TcpStream) -> io::Result +where + T: Message + Default, +{ + let size = stream.read_u32().await?; + println!("need to read: {}", size); + let mut buffer = BytesMut::with_capacity(size as usize); + unsafe { buffer.set_len(size as usize) }; + + println!("buffer size: {}", buffer.len()); + + stream.read_exact(&mut buffer).await?; + println!("buffer size after read: {}", buffer.len()); + println!("buffer content: {:?}", buffer); + + let message = decode_message::(buffer.into())?; + println!("decoded message: {:?}", message); + + Ok(message) +} + +pub fn decode_message(buffer: Bytes) -> io::Result +where + T: Message + Default, +{ + let msg_result = T::decode(buffer); + match msg_result { + Ok(msg) => Ok(msg), + Err(err) => Err(io::Error::new( + ErrorKind::InvalidInput, + format!("message decoding failed: {:?}", err), + )), + } +} diff --git a/protocol/src/proto/network.proto b/protocol/src/proto/network.proto index 4cb38bf..387a596 100644 --- a/protocol/src/proto/network.proto +++ b/protocol/src/proto/network.proto @@ -21,10 +21,13 @@ message Connect { message NetworkServerMessage { oneof message { Request request = 1; + Info got_info = 2; } } -message Request {} +message Request { + bool a = 1; +} message Info { string server_name = 1;