diff --git a/src/server/client.rs b/src/server/client.rs index 762495a..e91b28d 100644 --- a/src/server/client.rs +++ b/src/server/client.rs @@ -57,13 +57,6 @@ pub struct Client { pub state: ClientState, pub info: Option, pub cars: Vec<(u8, Car)>, - - pub ready: bool, - pub grid_spot: usize, - pub finished: bool, - - pub incidents: usize, - pub last_physics_update: Instant, } impl Drop for Client { @@ -108,13 +101,6 @@ impl Client { state: ClientState::Connecting, info: None, cars: Vec::new(), - - ready: false, - grid_spot: 0, - finished: false, - - incidents: 0, - last_physics_update: Instant::now(), } } @@ -155,19 +141,19 @@ impl Client { let id = self.read_raw(1).await?[0] as usize; debug!("HandleDownload connection for client id: {}", id); - // TODO: How does this work??? + let mut mod_name = { + let mut lock = CLIENT_MOD_PROGRESS.lock().await; + if lock.get(&id).is_none() { lock.insert(id, 0); } + let next_id = lock.get_mut(&id).unwrap(); - let mut lock = CLIENT_MOD_PROGRESS.lock().await; - if lock.get(&id).is_none() { lock.insert(id, 0); } - let next_id = lock.get_mut(&id).unwrap(); + let bmod = &config.mods[*next_id]; // TODO: This is a bit uhh yeah + debug!("Mod name: {}", bmod.0); - let bmod = &config.mods[*next_id]; // TODO: This is a bit uhh yeah + *next_id += 1; - *next_id += 1; + bmod.0.clone() + }; - debug!("Mod name: {}", bmod.0); - - let mut mod_name = bmod.0.clone(); if mod_name.starts_with("/") == false { mod_name = format!("/{mod_name}"); } @@ -175,9 +161,7 @@ impl Client { let mod_path = format!("Resources/Client{mod_name}"); let file_data = std::fs::read(mod_path)?; - let packet = RawPacket::from_data(file_data); - // let packet1 = RawPacket::from_data(file_data[..(file_data.len()/2)].to_vec()); - // let packet2 = RawPacket::from_data(file_data[(file_data.len()/2)..].to_vec()); + let packet = RawPacket::from_data(file_data[(file_data.len()/2)..].to_vec()); { let mut lock = self.write_half.lock().await; @@ -186,12 +170,6 @@ impl Client { if let Err(e) = tcp_write_raw(lock.deref_mut(), Packet::Raw(packet)).await { error!("{:?}", e); } - // if let Err(e) = tcp_write_raw(lock.deref_mut(), Packet::Raw(packet1)).await { - // error!("{:?}", e); - // } - // if let Err(e) = tcp_write_raw(lock.deref_mut(), Packet::Raw(packet2)).await { - // error!("{:?}", e); - // } trace!("Packets sent!"); drop(lock); } @@ -200,6 +178,7 @@ impl Client { return Ok(false); } _ => { + error!("Unknown code: {}", code); return Err(ClientError::AuthenticateError.into()); } } @@ -286,11 +265,32 @@ impl Client { } 'f' => { // Handle file download - let mut file_name = packet.data_as_string().clone(); - file_name.remove(0); // Remove f - debug!("Client requested file {}", file_name); + let mut mod_name = packet.data_as_string().clone(); + mod_name.remove(0); // Remove f + debug!("Client requested file {}", mod_name); self.write_packet(Packet::Raw(RawPacket::from_str("AG"))).await?; + + // Send the first half of the file + if mod_name.starts_with("/") == false { + mod_name = format!("/{mod_name}"); + } + + let mod_path = format!("Resources/Client{mod_name}"); + let file_data = std::fs::read(mod_path)?; + + let packet = RawPacket::from_data(file_data[..(file_data.len()/2)].to_vec()); + + { + let mut lock = self.write_half.lock().await; + lock.writable().await?; + trace!("Sending packets!"); + if let Err(e) = tcp_write_raw(lock.deref_mut(), Packet::Raw(packet)).await { + error!("{:?}", e); + } + trace!("Packets sent!"); + drop(lock); + } } _ => error!("Unknown packet! {:?}", packet), } diff --git a/src/server/mod.rs b/src/server/mod.rs index ec818a0..434757a 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -86,7 +86,7 @@ impl Server { let mut client = Client::new(socket); match client.authenticate(&cfg_ref).await { - Ok(b) if b => { + Ok(isClient) if isClient => { let mut lock = ci_ref .lock() .map_err(|e| error!("{:?}", e)) @@ -94,13 +94,14 @@ impl Server { lock.push(client); drop(lock); }, - Ok(b) => { + Ok(isClient) => { debug!("Downloader?"); }, Err(e) => { error!("Authentication error occured, kicking player..."); error!("{:?}", e); client.kick("Failed to authenticate player!").await; + // client.disconnect(); } } }); @@ -247,7 +248,7 @@ impl Server { Ok(()) } - // NOTE: Skips all clients that are currently connecting! + // NOTE: Skips all clients that are currently connecting or syncing resources! async fn broadcast(&self, packet: Packet, owner: Option) { for client in &self.clients { if let Some(id) = owner { @@ -255,7 +256,7 @@ impl Server { continue; } } - if client.state == ClientState::Connecting { + if client.state == ClientState::Connecting || client.state == ClientState::SyncingResources { continue; } client.queue_packet(packet.clone()).await; @@ -505,22 +506,10 @@ impl Server { 'O' => self.parse_vehicle_packet(client_idx, packet).await?, 'C' => { // TODO: Chat filtering? - let packet_data = packet.data_as_string(); - let message = packet_data.split(":").collect::>().get(2).map(|s| s.to_string()).unwrap_or(String::new()); - let message = message.trim(); - if message.starts_with("!") { - if message == "!ready" { - self.clients[client_idx].ready = true; - self.clients[client_idx].queue_packet(Packet::Raw(RawPacket::from_str("C:Server:You are now ready!"))).await; - } else if message == "!pos" { - let car = &self.clients[client_idx].cars.get(0).ok_or(ServerError::CarDoesntExist)?.1; - trace!("car transform (pos/rot/vel/rvel): {:?}", (car.pos, car.rot, car.vel, car.rvel)); - } else { - self.clients[client_idx].queue_packet(Packet::Raw(RawPacket::from_str("C:Server:Unknown command!"))).await; - } - } else { - self.broadcast(Packet::Raw(packet), None).await; - } + // let packet_data = packet.data_as_string(); + // let message = packet_data.split(":").collect::>().get(2).map(|s| s.to_string()).unwrap_or(String::new()); + // let message = message.trim(); + self.broadcast(Packet::Raw(packet), None).await; } _ => { let string_data = String::from_utf8_lossy(&packet.data[..]);