This commit is contained in:
griffi-gh 2023-02-12 03:22:42 +01:00
parent 907e3164d7
commit 70aaba01b0
2 changed files with 7 additions and 7 deletions

View file

@ -151,11 +151,11 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
self.last_heartbeat = Instant::now(); self.last_heartbeat = Instant::now();
} }
//receive //receive
let mut buf = Vec::new(); let mut buf = [0; u16::MAX as usize];
match self.socket.recv(&mut buf) { match self.socket.recv(&mut buf) {
Ok(_) => { Ok(length) => {
//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[..length], 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() {
return Ok(()) return Ok(())

View file

@ -70,7 +70,7 @@ impl<S, R> Server<S, R> where S: Encode + Decode, R: Encode + Decode {
fn add_client(&mut self, addr: SocketAddr) -> Result<ClientId> { fn add_client(&mut self, addr: SocketAddr) -> Result<ClientId> {
let Some(id) = (1..=self.config.max_clients) let Some(id) = (1..=self.config.max_clients)
.map(|x| ClientId::new(x as _).unwrap()) .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"); bail!("Server full");
}; };
if self.clients.iter().any(|x| x.1.addr == addr) { if self.clients.iter().any(|x| x.1.addr == addr) {
@ -124,10 +124,10 @@ 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 = [0; u16::MAX as usize];
match self.socket.recv_from(&mut buf) { match self.socket.recv_from(&mut buf) {
Ok((_, addr)) => { Ok((len, addr)) => {
if let Ok(packet) = bincode::decode_from_slice(&buf, BINCODE_CONFIG) { if let Ok(packet) = bincode::decode_from_slice(&buf[..len], BINCODE_CONFIG) {
let (packet, _): (IdClientPacket<R>, _) = packet; let (packet, _): (IdClientPacket<R>, _) = packet;
let IdClientPacket(id, packet) = packet; let IdClientPacket(id, packet) = packet;
match id { match id {