added contants and message kind

This commit is contained in:
clizia 2025-01-26 17:19:15 +01:00
parent a085ae78bb
commit d001bacae8
4 changed files with 25 additions and 13 deletions

5
src/constant.rs Normal file
View file

@ -0,0 +1,5 @@
pub const METADATA_SIZE: usize = 2;
pub const MESSAGE_KIND_MOVE: u8 = 0;
pub const MESSAGE_KIND_PLAYER_SETUP: u8 = 1;
pub const MESSAGE_KIND_LOBBY_SETUP: u8 = 2;

View file

@ -6,3 +6,4 @@ pub mod lobby;
pub mod player; pub mod player;
pub mod message; pub mod message;
pub mod message_read; pub mod message_read;
pub mod constant;

View file

@ -1,16 +1,28 @@
use crate::constant::METADATA_SIZE;
// everything here needs to be adjusted for the deserialized json // everything here needs to be adjusted for the deserialized json
// //
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Message { pub struct Message {
pub message_kind: MessageKind,
pub length: u16, pub length: u16,
pub content: String, pub content: String,
} }
// idk i just want to layout something, really didn't think much
#[derive(Debug, Clone)]
pub enum MessageKind {
Move(u8),
PlayerSetup(u8),
LobbySetup(u8),
}
impl Message { impl Message {
pub fn new(content: impl Into<String>) -> anyhow::Result<Self> { pub fn new(content: impl Into<String>) -> anyhow::Result<Self> {
let content = content.into(); let content = content.into();
let length = content.len() as u16; let length = content.len() as u16;
// add message kind to constructor
Ok(Self { Ok(Self {
length, length,
content, content,
@ -26,18 +38,14 @@ impl Message {
} }
pub fn decode(buffer: &[u8]) -> anyhow::Result<Self> { pub fn decode(buffer: &[u8]) -> anyhow::Result<Self> {
// this `if` compares the buffer length to the minimum if buffer.len() < METADATA_SIZE {
// message size (which is 2 because it takes to bytes
// to write 0), it is now hardcoded but it MUST be moved
// into a constant in a special constant definition file
// byeeeeeeeeeeeeeeeeee luv ya
if buffer.len() < 2 {
return Err(anyhow::anyhow!("Invalid message length")); return Err(anyhow::anyhow!("Invalid message length"));
} }
let length = u16::from_be_bytes([buffer[0], buffer[1]]); let length = u16::from_be_bytes([buffer[0], buffer[1]]);
let content = String::from_utf8(buffer[2..2 + length as usize].to_vec())?; let content = String::from_utf8(buffer[2..2 + length as usize].to_vec())?;
// add message kind to constructor
Ok(Self { Ok(Self {
length, length,
content, content,

View file

@ -1,7 +1,5 @@
// every hardcoded 2 will one day be a constant in a special // add message kind everywhere is needed
// constant file but rn I AM LAZY AND ITS LATE use crate::{constant::METADATA_SIZE, message::Message};
//
use crate::message::Message;
pub struct MessageReader { pub struct MessageReader {
pub buffer: Vec<u8>, pub buffer: Vec<u8>,
@ -13,17 +11,17 @@ impl MessageReader {
} }
fn can_parse(&self) -> bool { fn can_parse(&self) -> bool {
if self.buffer.len() < 2 { if self.buffer.len() < METADATA_SIZE {
return false; return false;
} }
let length = u16::from_be_bytes([self.buffer[0], self.buffer[1]]); let length = u16::from_be_bytes([self.buffer[0], self.buffer[1]]);
self.buffer.len() >= 2 + length as usize self.buffer.len() >= METADATA_SIZE + length as usize
} }
fn parse_first(&mut self) -> anyhow::Result<Message> { fn parse_first(&mut self) -> anyhow::Result<Message> {
let length = u16::from_be_bytes([self.buffer[0], self.buffer[1]]); let length = u16::from_be_bytes([self.buffer[0], self.buffer[1]]);
let message_length = 2 + length as usize; let message_length = METADATA_SIZE + length as usize;
let message = self.buffer[..message_length].to_vec(); let message = self.buffer[..message_length].to_vec();
self.buffer = self.buffer[message_length..].to_vec(); self.buffer = self.buffer[message_length..].to_vec();