diff --git a/kubi-udp/src/client.rs b/kubi-udp/src/client.rs index 75ddf2f..31f5d17 100644 --- a/kubi-udp/src/client.rs +++ b/kubi-udp/src/client.rs @@ -151,11 +151,11 @@ impl Client where S: Encode + Decode, R: Encode + Decode { self.last_heartbeat = Instant::now(); } //receive - let mut buf = Vec::new(); + let mut buf = [0; u16::MAX as usize]; match self.socket.recv(&mut buf) { - Ok(_) => { + Ok(length) => { //TODO check the first byte of the raw data instead of decoding? - let (packet, _): (IdServerPacket, _) = bincode::decode_from_slice(&buf, BINCODE_CONFIG)?; + let (packet, _): (IdServerPacket, _) = bincode::decode_from_slice(&buf[..length], BINCODE_CONFIG)?; let IdServerPacket(user_id, packet) = packet; if self.client_id.map(|x| Some(x) != user_id).unwrap_or_default() { return Ok(()) diff --git a/kubi-udp/src/server.rs b/kubi-udp/src/server.rs index 64bc97a..f358c6e 100644 --- a/kubi-udp/src/server.rs +++ b/kubi-udp/src/server.rs @@ -70,7 +70,7 @@ impl Server where S: Encode + Decode, R: Encode + Decode { fn add_client(&mut self, addr: SocketAddr) -> Result { let Some(id) = (1..=self.config.max_clients) .map(|x| ClientId::new(x as _).unwrap()) - .find(|i| self.clients.contains_key(i)) else { + .find(|i| !self.clients.contains_key(i)) else { bail!("Server full"); }; if self.clients.iter().any(|x| x.1.addr == addr) { @@ -124,10 +124,10 @@ impl Server where S: Encode + Decode, R: Encode + Decode { } pub fn update(&mut self) -> Result<()> { //TODO client timeout - let mut buf = Vec::new(); + let mut buf = [0; u16::MAX as usize]; match self.socket.recv_from(&mut buf) { - Ok((_, addr)) => { - if let Ok(packet) = bincode::decode_from_slice(&buf, BINCODE_CONFIG) { + Ok((len, addr)) => { + if let Ok(packet) = bincode::decode_from_slice(&buf[..len], BINCODE_CONFIG) { let (packet, _): (IdClientPacket, _) = packet; let IdClientPacket(id, packet) = packet; match id {