change things

This commit is contained in:
griffi-gh 2023-02-12 02:56:50 +01:00
parent 00f90156bd
commit 7ecc4d5c24
2 changed files with 19 additions and 16 deletions

View file

@ -3,7 +3,8 @@ use std::{
net::{UdpSocket, SocketAddr}, net::{UdpSocket, SocketAddr},
time::{Instant, Duration}, time::{Instant, Duration},
marker::PhantomData, marker::PhantomData,
collections::{VecDeque, vec_deque::Drain as DrainDeque}, collections::{VecDeque, vec_deque::Drain as DrainDeque},
io::ErrorKind,
}; };
use bincode::{Encode, Decode}; use bincode::{Encode, Decode};
use crate::{ use crate::{
@ -151,13 +152,13 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
} }
//receive //receive
let mut buf = Vec::new(); let mut buf = Vec::new();
loop { match self.socket.recv(&mut buf) {
if self.socket.recv(&mut buf).is_ok() { Ok(_) => {
//TODO check the first byte of the raw data instead of decoding? //TODO check the first byte of the raw data instead of decoding?
let (packet, _): (IdServerPacket<R>, _) = bincode::decode_from_slice(&buf, BINCODE_CONFIG)?; let (packet, _): (IdServerPacket<R>, _) = bincode::decode_from_slice(&buf, BINCODE_CONFIG)?;
let IdServerPacket(user_id, packet) = packet; let IdServerPacket(user_id, packet) = packet;
if self.client_id.map(|x| Some(x) != user_id).unwrap_or_default() { if self.client_id.map(|x| Some(x) != user_id).unwrap_or_default() {
continue return Ok(())
} }
self.reset_timeout(); self.reset_timeout();
match packet { match packet {
@ -178,10 +179,11 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
self.event_queue.push_back(ClientEvent::MessageReceived(message)); self.event_queue.push_back(ClientEvent::MessageReceived(message));
} }
} }
} else { },
break Err(error) if error.kind() != ErrorKind::WouldBlock => {
} return Err(error.into());
buf.clear(); },
_ => (),
} }
Ok(()) Ok(())
} }

View file

@ -2,7 +2,8 @@ use std::{
net::{UdpSocket, SocketAddr}, net::{UdpSocket, SocketAddr},
time::Instant, time::Instant,
marker::PhantomData, marker::PhantomData,
collections::{VecDeque, vec_deque::Drain as DrainDeque} collections::{VecDeque, vec_deque::Drain as DrainDeque},
io::ErrorKind
}; };
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use bincode::{Encode, Decode}; use bincode::{Encode, Decode};
@ -113,7 +114,6 @@ impl<S, R> Server<S, R> where S: Encode + Decode, R: Encode + Decode {
assert!(config.max_clients <= MAX_CLIENTS); assert!(config.max_clients <= MAX_CLIENTS);
let socket = UdpSocket::bind(addr)?; let socket = UdpSocket::bind(addr)?;
socket.set_nonblocking(true)?; socket.set_nonblocking(true)?;
//socket.set_broadcast(true)?;
Ok(Self { Ok(Self {
config, config,
socket, socket,
@ -125,8 +125,8 @@ impl<S, R> Server<S, R> where S: Encode + Decode, R: Encode + Decode {
pub fn update(&mut self) -> Result<()> { pub fn update(&mut self) -> Result<()> {
//TODO client timeout //TODO client timeout
let mut buf = Vec::new(); let mut buf = Vec::new();
loop { match self.socket.recv_from(&mut buf) {
if let Ok((_, addr)) = self.socket.recv_from(&mut buf) { Ok((_, addr)) => {
if let Ok(packet) = bincode::decode_from_slice(&buf, BINCODE_CONFIG) { if let Ok(packet) = bincode::decode_from_slice(&buf, BINCODE_CONFIG) {
let (packet, _): (IdClientPacket<R>, _) = packet; let (packet, _): (IdClientPacket<R>, _) = packet;
let IdClientPacket(id, packet) = packet; let IdClientPacket(id, packet) = packet;
@ -177,10 +177,11 @@ impl<S, R> Server<S, R> where S: Encode + Decode, R: Encode + Decode {
} else { } else {
bail!("Corrupted packet received"); bail!("Corrupted packet received");
} }
} else { },
break Err(error) if error.kind() != ErrorKind::WouldBlock => {
} return Err(error.into());
buf.clear() },
_ => (),
} }
Ok(()) Ok(())
} }