Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
refactor: remove iter() in favor of an owned read()
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Apr 8, 2024
1 parent 6ecde43 commit f806a2f
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe

- The `RtcClient` and `RtcServer` system parameters (prev. `NetworkReader`/`NetworkWriter`) have new methods:
- `clear()` to clear all incoming messages in the buffer.
- `iter()` to read all messages without consuming them.

### changed

- To fix name conflicts with the `server` and `client` feature, the respective types have changed names.
- `RtcState` has changed to `RtcClientState` or `RtcServerState`, depending on the feature.
- `RtcStatus` has changed to `RtcClientStatus` or `RtcServerStatus`, depending on the feature.
- `NetworkReader`/`NetworkWriter` have been both merged and changed to `RtcClient` or `RtcServer` respectively.
- `RtcClient.read()` (previously `NetworkReader`) now returns a `Vec<_>` rather than a `Drain<'_, _>`.

## 0.1.1

Expand Down
11 changes: 3 additions & 8 deletions bevy_rtc/src/client/system_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct RtcClient<'w, M: Payload> {
}

impl<'w, M: Payload> RtcClient<'w, M> {
/// Returns the capacity of this network reader.
/// Returns the capacity of incoming messages.
pub fn capacity(&self) -> usize {
self.incoming.bound
}
Expand All @@ -24,19 +24,14 @@ impl<'w, M: Payload> RtcClient<'w, M> {
self.incoming.messages.is_empty()
}

/// Iterate over all messages in the buffer without consuming them.
pub fn iter(&mut self) -> impl Iterator<Item = &M> {
self.incoming.messages.iter()
}

/// Clear all messages waiting in the buffer.
pub fn clear(&mut self) {
self.incoming.messages.clear()
}

/// Consumes all messages in the buffer and iterate on them.
pub fn read(&mut self) -> std::collections::vec_deque::Drain<'_, M> {
self.incoming.messages.drain(..)
pub fn read(&mut self) -> Vec<M> {
self.incoming.messages.drain(..).collect()
}

/// Send a payload to the host with reliability. The payload is created with
Expand Down
6 changes: 1 addition & 5 deletions bevy_rtc/src/client/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,12 @@ pub fn read_latency_tracers(
let peer_id = state.id.expect("expected peer id");
let mut tracer = trace_query.single_mut();

let mut server_tracer = None;
for payload in client.read() {
if payload.from == peer_id {
tracer.process(payload);
} else if payload.from == host_id {
// Server time payloads get sent right back to the server
server_tracer.replace(payload);
client.unreliable_to_host(payload);
}
// Process payloads we sent out
else {
Expand All @@ -173,9 +172,6 @@ pub fn read_latency_tracers(
);
}
}
if let Some(payload) = server_tracer.take() {
client.unreliable_to_host(payload);
}
}

pub fn calculate_latency(
Expand Down
12 changes: 0 additions & 12 deletions bevy_rtc/src/server/system_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ impl<'w, M: Payload> RtcServer<'w, M> {
})
}

/// Iterate over all messages in the buffer without consuming them.
pub fn iter(&mut self) -> impl Iterator<Item = (&PeerId, &M)> {
self.incoming
.messages
.iter()
.fold(vec![], |mut v, (peer, payloads)| {
v.extend(payloads.iter().map(|p| (peer, p)));
v
})
.into_iter()
}

/// Clear all messages waiting in the buffer.
pub fn clear(&mut self) {
self.incoming.messages.clear()
Expand Down
9 changes: 2 additions & 7 deletions bevy_rtc/src/server/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
latency::{LatencyTracer, LatencyTracerPayload},
socket::RtcSocket,
};
use bevy::{prelude::*, utils::hashbrown::HashMap};
use bevy::prelude::*;
use bevy_matchbox::{
matchbox_signaling::{
topologies::client_server::{ClientServer, ClientServerState},
Expand Down Expand Up @@ -144,7 +144,6 @@ pub fn read_latency_tracers(
let host_id = state.id.expect("expected host id");

// Handle payloads
let mut client_tracers = HashMap::new();
for (from, payload) in server.read() {
// 2 cases:
// 1) We sent a tracer to the client, and are receiving it
Expand All @@ -156,15 +155,11 @@ pub fn read_latency_tracers(
}
} else if payload.from == from {
// Case 2
client_tracers.insert(from, payload);
server.unreliable_to_peer(from, payload);
} else {
warn!("Invalid latency tracer from {from}: {payload:?}, ignoring");
}
}

for (client, payload) in client_tracers.into_iter() {
server.unreliable_to_peer(client, payload);
}
}

pub fn calculate_latency(
Expand Down

0 comments on commit f806a2f

Please sign in to comment.