kinda works + fixes a crash

This commit is contained in:
Luuk van Oijen committed 2023-11-20 17:02:50 +01:00
1 parent 5a42fe87fd
commit 6c2b131d72
2 files changed
+9 -4

No files matched your search

+6 -3
View File
@@ -52,6 +52,7 @@ async fn main() {
.expect("Failed to start server!"); .expect("Failed to start server!");
let mut status = server.get_server_status(); let mut status = server.get_server_status();
hb_tx.send(status.clone()).await;
loop { loop {
// TODO: Error handling // TODO: Error handling
if server.clients.len() > 0 { if server.clients.len() > 0 {
@@ -63,16 +64,18 @@ async fn main() {
}, },
Err(e) => error!("Error: {e}"), Err(e) => error!("Error: {e}"),
} }
}, }
ret = server::read_udp(&mut server.udp_socket) => { ret = server::read_udp(&mut server.udp_socket) => {
if let Some((addr, packet)) = ret { if let Some((addr, packet)) = ret {
server.process_udp(addr, packet).await; server.process_udp(addr, packet).await;
} }
}, }
_ = tokio::time::sleep(tokio::time::Duration::from_millis(50)) => {}
} }
} else { } else {
trace!("eepy mode");
// TODO: Scuffed? // TODO: Scuffed?
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await; tokio::time::sleep(tokio::time::Duration::from_millis(150)).await;
} }
if let Err(e) = server.process().await { if let Err(e) = server.process().await {
+3 -1
View File
@@ -86,7 +86,6 @@ pub async fn read_tcp(clients: &mut Vec<Client>) -> anyhow::Result<Option<(usize
Ok(match result { Ok(match result {
Ok(packet_opt) => { Ok(packet_opt) => {
if let Some(raw_packet) = packet_opt { if let Some(raw_packet) = packet_opt {
// self.parse_packet(index, raw_packet).await?;
Some((index, raw_packet)) Some((index, raw_packet))
} else { } else {
None None
@@ -720,6 +719,9 @@ impl Server {
return Err(ServerError::BrokenPacket.into()); return Err(ServerError::BrokenPacket.into());
} else { } else {
// Sent as text so removing 48 brings it from [48-57] to [0-9] // Sent as text so removing 48 brings it from [48-57] to [0-9]
if packet.data[3] < 48 || packet.data[5] < 48 {
return Err(ServerError::BrokenPacket.into());
}
let client_id = packet.data[3] - 48; let client_id = packet.data[3] - 48;
let car_id = packet.data[5] - 48; let car_id = packet.data[5] - 48;