diff --git a/minecraft-server/src/entities/monsters/zombies.rs b/minecraft-server/src/entities/monsters/zombies.rs index 45306920..9b5f52b6 100644 --- a/minecraft-server/src/entities/monsters/zombies.rs +++ b/minecraft-server/src/entities/monsters/zombies.rs @@ -29,7 +29,7 @@ pub async fn zombie_ai_task(h: Handler, center_chunk: ChunkPosition, + packets_sent: usize, } impl Player { @@ -38,7 +39,7 @@ impl Player { server_msg_rcvr: BroadcastReceiver, change_receiver: MpscReceiver ) -> Eid { - let (packet_sender, packet_receiver) = mpsc_channel(100); + let (packet_sender, packet_receiver) = mpsc_channel(1000); let uuid = player_info.uuid; let mut player = Player { @@ -66,6 +67,7 @@ impl Player { loaded_chunks: HashSet::new(), info: player_info, + packets_sent: 0, }; // TODO: player should load existing entities @@ -172,6 +174,13 @@ impl Handler { async fn send_packet<'a>(&self, packet: PlayClientbound<'a>) { let packet = packet.serialize_minecraft_packet().unwrap(); + let packets_sent = self.mutate(|player| { + player.packets_sent += 1; + (player.packets_sent, EntityChanges::other()) + }).await.unwrap_or(0); + if packets_sent > 500 { + warn!("Many packets sent ({packets_sent})"); + } let Some(packet_sender) = self.observe(|player| player.packet_sender.clone()).await else {return}; packet_sender.send(packet).await.unwrap(); } @@ -179,7 +188,14 @@ impl Handler { async fn on_server_message(self, message: ServerMessage) { use ServerMessage::*; match message { - Tick => { + Tick(tick_id) => { + if tick_id % (20*10) == 0 { + self.send_packet(PlayClientbound::KeepAlive { keep_alive_id: tick_id as u64 }).await; + } + self.mutate(|player| { + player.packets_sent = 0; + (player.packets_sent, EntityChanges::other()) + }).await; self.send_packet(PlayClientbound::BundleDelimiter).await; } } @@ -327,9 +343,22 @@ impl Handler { position.y += 20.0; zombie.get_entity_mut().position = position; self.world.spawn_entity::(AnyEntity::Zombie(zombie)).await; - debug!("zombie spawned"); + } else if message == "stress" { + tokio::spawn(async move { + for i in 0..1000 { + let mut zombie = Zombie::default(); + let Some(mut position) = self.observe(|player| player.get_entity().position.clone()).await else {return}; + position.y += 20.0; + zombie.get_entity_mut().position = position; + self.world.spawn_entity::(AnyEntity::Zombie(zombie)).await; + tokio::time::sleep(Duration::from_millis(50)).await; + } + }); } } + RequestPing { payload } => { + self.send_packet(PlayClientbound::Ping { id: payload as i32 }).await; + } packet => warn!("Unsupported packet received: {packet:?}"), } } diff --git a/minecraft-server/src/entities/tasks/newton.rs b/minecraft-server/src/entities/tasks/newton.rs index 21d981e8..e83c673f 100644 --- a/minecraft-server/src/entities/tasks/newton.rs +++ b/minecraft-server/src/entities/tasks/newton.rs @@ -6,7 +6,7 @@ pub async fn newton_task(h: Handler, mut server_msg_rcvr loop { let msg = server_msg_rcvr.recv().await.unwrap(); - if !matches!(&msg, &ServerMessage::Tick) { + if !matches!(&msg, &ServerMessage::Tick(_)) { continue; } diff --git a/minecraft-server/src/server_behavior.rs b/minecraft-server/src/server_behavior.rs index a0475533..c7672e51 100644 --- a/minecraft-server/src/server_behavior.rs +++ b/minecraft-server/src/server_behavior.rs @@ -6,7 +6,7 @@ use crate::prelude::*; #[derive(Clone, Debug)] pub enum ServerMessage { /// Message indicating a new tick has started - Tick, + Tick(usize), } pub struct ServerBehavior { @@ -22,10 +22,12 @@ impl ServerBehavior { // Send ticks to player handlers tokio::spawn(async move { + let mut tick_id = 0; let mut tick = tokio::time::interval(Duration::from_millis(50)); loop { tick.tick().await; - let _ = sender.send(ServerMessage::Tick); + let _ = sender.send(ServerMessage::Tick(tick_id)); + tick_id += 1; } }); diff --git a/minecraft-server/src/world/mod.rs b/minecraft-server/src/world/mod.rs index 175e983c..3a2ed2dd 100644 --- a/minecraft-server/src/world/mod.rs +++ b/minecraft-server/src/world/mod.rs @@ -154,7 +154,7 @@ impl World { let Some(loaders) = loading_manager.get_loaders(position) else {return}; for loader in loaders { if let Some(sender) = senders.get_mut(loader) { - let _ = sender.send(change.clone()).await; + let _ = sender.try_send(change.clone()); } } }