adjust message for json

This commit is contained in:
clizia 2025-02-19 21:04:56 +01:00
parent 0d5c18ce09
commit 2e3c3f9fff
3 changed files with 55 additions and 32 deletions

View file

@ -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?;

View file

@ -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<String>) -> anyhow::Result<Self> {
pub fn new(from: &str, message_kind: MessageKind, content: impl Into<String>) -> anyhow::Result<Self> {
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<u8> {
let mut buffer = Vec::new();
buffer.extend(&self.length.to_be_bytes());
buffer.extend(self.content.as_bytes());
pub fn encode(&self) -> anyhow::Result<String> {
// 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<Self> {
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<u8> for MessageKind {
type Error = &'static str;
// impl TryFrom<u8> for MessageKind {
// type Error = &'static str;
fn try_from(value: u8) -> Result<Self, Self::Error> {
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<Self, Self::Error> {
// 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")
// }
// }
// }

View file

@ -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])?;