Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process client packets before run schedulers #587

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 23 additions & 59 deletions crates/valence_server/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,74 +108,38 @@ fn run_event_loop(
EventWriter<PacketEvent>,
Commands,
)>,
mut check_again: Local<Vec<(Entity, usize)>>,
) {
debug_assert!(check_again.is_empty());

let (mut clients, mut event_writer, mut commands) = state.get_mut(world);

for (entity, mut client) in &mut clients {
match client.connection_mut().try_recv() {
Ok(Some(pkt)) => {
event_writer.send(PacketEvent {
client: entity,
timestamp: pkt.timestamp,
id: pkt.id,
data: pkt.body,
});

let remaining = client.connection().len();

if remaining > 0 {
check_again.push((entity, remaining));
let mut pending_packets = client.connection().len();
Copy link
Author

@ramon-bernardo ramon-bernardo Dec 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We look for the number of pending packets beforehand, so that new packets from the client are handled in the next execution. Avoiding a loop between current packets pending vs news.


'pkt: while pending_packets > 0 {
pending_packets -= 1;

match client.connection_mut().try_recv() {
Ok(Some(pkt)) => {
event_writer.send(PacketEvent {
client: entity,
timestamp: pkt.timestamp,
id: pkt.id,
data: pkt.body,
});
}
Ok(None) => {
break 'pkt;
}
Err(e) => {
// Client is disconnected.
debug!("disconnecting client: {e:#}");
commands.entity(entity).remove::<Client>();
break 'pkt;
}
}
Ok(None) => {}
Err(e) => {
// Client is disconnected.
debug!("disconnecting client: {e:#}");
commands.entity(entity).remove::<Client>();
}
}
}

state.apply(world);
run_event_loop_schedules(world);

while !check_again.is_empty() {
let (mut clients, mut event_writer, mut commands) = state.get_mut(world);

check_again.retain_mut(|(entity, remaining)| {
debug_assert!(*remaining > 0);

if let Ok((_, mut client)) = clients.get_mut(*entity) {
match client.connection_mut().try_recv() {
Ok(Some(pkt)) => {
event_writer.send(PacketEvent {
client: *entity,
timestamp: pkt.timestamp,
id: pkt.id,
data: pkt.body,
});
*remaining -= 1;
// Keep looping as long as there are packets to process this tick.
*remaining > 0
}
Ok(None) => false,
Err(e) => {
// Client is disconnected.
debug!("disconnecting client: {e:#}");
commands.entity(*entity).remove::<Client>();
false
}
}
} else {
// Client must have been deleted in the last run of the schedule.
false
}
});

state.apply(world);
run_event_loop_schedules(world);
}
run_event_loop_schedules(world);
}