Skip to content

Commit 5201c1e

Browse files
MorboMorbo
authored andcommitted
Add client and server iter_received method
1 parent 154a5c0 commit 5201c1e

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

src/shared/backend/client_messages.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ impl ClientMessages {
4040
channel_messages.len()
4141
}
4242

43+
/// Returns an iterator over received messages from the server on a channel without consuming them.
44+
///
45+
/// Unlike [`Self::receive`], the messages stay in the resource. Intended for tools
46+
/// that need to observe inbound traffic (e.g. debug overlays or client-side replay
47+
/// recording) alongside Replicon's normal consumption.
48+
pub fn iter_received<I: Into<usize>>(
49+
&self,
50+
channel_id: I,
51+
) -> impl ExactSizeIterator<Item = &Bytes> + '_ {
52+
let channel_id = channel_id.into();
53+
self.received_messages
54+
.get(channel_id)
55+
.unwrap_or_else(|| panic!("client should have a receive channel with id {channel_id}"))
56+
.iter()
57+
}
58+
4359
/// Receives all available messages from the server over a channel.
4460
///
4561
/// All messages will be drained.
@@ -90,6 +106,17 @@ impl ClientMessages {
90106
self.sent_messages.clear();
91107
}
92108

109+
/// Returns an iterator over sent messages without consuming them.
110+
///
111+
/// Unlike [`Self::drain_sent`], the messages stay in the resource. Intended for
112+
/// tools that need to observe outbound traffic before the messaging backend drains
113+
/// them.
114+
pub fn iter_sent(&self) -> impl ExactSizeIterator<Item = (usize, &Bytes)> + '_ {
115+
self.sent_messages
116+
.iter()
117+
.map(|(channel_id, bytes)| (*channel_id, bytes))
118+
}
119+
93120
/// Removes all sent messages, returning them as an iterator with channel.
94121
///
95122
/// <div class="warning">

src/shared/backend/server_messages.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ impl ServerMessages {
3737
self.sent_messages.retain(|&(entity, ..)| entity != client);
3838
}
3939

40+
/// Returns an iterator over received messages from clients on a channel without consuming them.
41+
///
42+
/// Unlike [`Self::receive`], the messages stay in the resource. Intended for tools
43+
/// that need to observe inbound traffic (e.g. debug overlays or replay recording)
44+
/// alongside Replicon's normal consumption.
45+
pub fn iter_received<I: Into<usize>>(
46+
&self,
47+
channel_id: I,
48+
) -> impl ExactSizeIterator<Item = (Entity, &Bytes)> + '_ {
49+
let channel_id = channel_id.into();
50+
self.received_messages
51+
.get(channel_id)
52+
.unwrap_or_else(|| panic!("server should have a receive channel with id {channel_id}"))
53+
.iter()
54+
.map(|(entity, bytes)| (*entity, bytes))
55+
}
56+
4057
/// Receives all available messages from clients over a channel.
4158
///
4259
/// All messages will be drained.
@@ -95,6 +112,17 @@ impl ServerMessages {
95112
self.sent_messages.retain(f)
96113
}
97114

115+
/// Returns an iterator over sent messages without consuming them.
116+
///
117+
/// Unlike [`Self::drain_sent`], the messages stay in the resource. Intended for
118+
/// tools that need to observe outbound traffic (e.g. server-side replay recording)
119+
/// before the messaging backend drains them.
120+
pub fn iter_sent(&self) -> impl ExactSizeIterator<Item = (Entity, usize, &Bytes)> + '_ {
121+
self.sent_messages
122+
.iter()
123+
.map(|(entity, channel_id, bytes)| (*entity, *channel_id, bytes))
124+
}
125+
98126
/// Removes all sent messages, returning them as an iterator with client entity and channel.
99127
///
100128
/// <div class="warning">

0 commit comments

Comments
 (0)