diff --git a/src/bin/client.rs b/src/bin/client.rs index 577e58c..3d8b11a 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -21,6 +21,12 @@ pub async fn main() -> anyhow::Result<()> { Clear(ClearType::All), )?; + 'get_instruction: loop { + match TODO { + todo!() + } + } + for i in 1..11 { let message = Message::new(lib::message::MessageKind::Move, format!("Hello toto x {}!!!", i)); client.send_message(message.unwrap().clone()).await?; diff --git a/src/message.rs b/src/message.rs index 2708665..368dba5 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,16 +1,20 @@ -use crate::constant::{MESSAGE_KIND_CREATE_LOBBY, MESSAGE_KIND_CREATE_PLAYER, MESSAGE_KIND_MOVE, METADATA_SIZE}; +use anyhow::Ok; +use serde::{Deserialize, Serialize}; + +// use crate::constant::{MESSAGE_KIND_CREATE_LOBBY, MESSAGE_KIND_CREATE_PLAYER, MESSAGE_KIND_MOVE, METADATA_SIZE}; // everything here needs to be adjusted for the deserialized json // -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Message { + pub from: String, pub message_kind: MessageKind, pub length: u16, pub content: String, } // idk i just want to layout something, really didn't think much -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub enum MessageKind { Move, CreatePlayer, @@ -18,51 +22,61 @@ pub enum MessageKind { } impl Message { - pub fn new(message_kind: MessageKind, content: impl Into) -> anyhow::Result { + pub fn new(from: &str, message_kind: MessageKind, content: impl Into) -> anyhow::Result { + let from = from.into(); let content = content.into(); let length = content.len() as u16; Ok(Self { + from, message_kind, length, content, }) } - pub fn encode(&self) -> Vec { - let mut buffer = Vec::new(); - buffer.extend(&self.length.to_be_bytes()); - buffer.extend(self.content.as_bytes()); + pub fn encode(&self) -> anyhow::Result { + // let mut buffer = Vec::new(); + // buffer.extend(self.from.as_bytes()); + // buffer.extend() + // buffer.extend(&self.length.to_be_bytes()); + // buffer.extend(self.content.as_bytes()); - buffer + let message_json: String = serde_json::to_string(self)?; + + Ok(message_json) } pub fn decode(buffer: &[u8]) -> anyhow::Result { - if buffer.len() < METADATA_SIZE { - return Err(anyhow::anyhow!("Invalid message length")); - } + // if buffer.len() < METADATA_SIZE { + // return Err(anyhow::anyhow!("Invalid message length")); + // } - let message_kind = MessageKind::try_from(buffer[0]).expect("Error parsing the MessageKind"); - let length = u16::from_be_bytes([buffer[1], buffer[2]]); - let content = String::from_utf8(buffer[3..3 + length as usize].to_vec())?; + // let message_kind = MessageKind::try_from(buffer[0]).expect("Error parsing the MessageKind"); + // let length = u16::from_be_bytes([buffer[1], buffer[2]]); + // let content = String::from_utf8(buffer[3..3 + length as usize].to_vec())?; - Ok(Self { - message_kind, - length, - content, - }) + // Ok(Self { + // message_kind, + // length, + // content, + // }) + + let message: Message = serde_json::from_slice(buffer)?; + + Ok(message) } } -impl TryFrom for MessageKind { - type Error = &'static str; +// impl TryFrom for MessageKind { +// type Error = &'static str; - fn try_from(value: u8) -> Result { - match value { - MESSAGE_KIND_MOVE => Ok(MessageKind::Move), - MESSAGE_KIND_CREATE_PLAYER => Ok(MessageKind::CreatePlayer), - MESSAGE_KIND_CREATE_LOBBY => Ok(MessageKind::CreateLobby), - _ => Err("invalid MessageKind byte") - } - } -} +// fn try_from(value: u8) -> Result { +// match value { +// MESSAGE_KIND_MOVE => Ok(MessageKind::Move), +// MESSAGE_KIND_CREATE_PLAYER => Ok(MessageKind::CreatePlayer), +// MESSAGE_KIND_CREATE_LOBBY => Ok(MessageKind::CreateLobby), +// _ => Err("invalid MessageKind byte") +// } +// } +// } diff --git a/src/server.rs b/src/server.rs index 0299a9a..fd9891c 100644 --- a/src/server.rs +++ b/src/server.rs @@ -30,9 +30,12 @@ impl Server { tokio::task::spawn(async move { let mut message_reader = MessageReader::new(); + + let (mut read, mut write) = socket.split(); + 'handler: loop { let mut buffer = vec![0; 256]; - let bytes_read = socket.read(&mut buffer).await?; + let bytes_read = read.read(&mut buffer).await?; let messages = message_reader.read(&buffer[..bytes_read])?;