Skip to content

Commit 5ef2261

Browse files
author
Charles Bournhonesque
committed
move visibility into send
Change-Id: Ice21174c0c5486b058530a42e4fec421de2ce627
1 parent ae794d8 commit 5ef2261

27 files changed

Lines changed: 199 additions & 124 deletions

Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ harness = false
9393
name = "mutations"
9494
required-features = ["client", "server"]
9595

96+
[[test]]
97+
name = "bidirectional_event"
98+
required-features = ["client", "server"]
99+
96100
[[test]]
97101
name = "client_message"
98102
required-features = ["client", "server"]
@@ -117,6 +121,14 @@ required-features = ["client"]
117121
name = "insertion"
118122
required-features = ["client", "server"]
119123

124+
[[test]]
125+
name = "mutate_ticks"
126+
required-features = ["client", "server"]
127+
128+
[[test]]
129+
name = "priority"
130+
required-features = ["client", "server"]
131+
120132
[[test]]
121133
name = "removal"
122134
required-features = ["client", "server"]

src/client.rs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ pub mod diagnostics;
33
pub mod message;
44

55
pub use crate::shared::replication::receive::{
6-
ServerUpdateTick, confirm_history, server_mutate_ticks,
6+
ServerUpdateTick, client_replication_stats::ClientReplicationStats, confirm_history,
7+
remote::Remote, server_mutate_ticks,
78
};
89

910
use bevy::prelude::*;
@@ -149,35 +150,3 @@ pub enum ClientSystems {
149150
/// Runs in [`OnExit`] for [`ClientState::Connected`].
150151
Reset,
151152
}
152-
153-
/// Replication stats during message processing.
154-
///
155-
/// Statistic will be collected only if the resource is present.
156-
/// The resource is not added by default.
157-
///
158-
/// See also [`ClientDiagnosticsPlugin`]
159-
/// for automatic integration with Bevy diagnostics.
160-
#[derive(Resource, Default, Reflect, Debug, Clone, Copy)]
161-
pub struct ClientReplicationStats {
162-
/// Incremented per entity that changes.
163-
pub entities_changed: usize,
164-
/// Incremented for every component that changes.
165-
pub components_changed: usize,
166-
/// Incremented per client mapping added.
167-
pub mappings: usize,
168-
/// Incremented per entity despawn.
169-
pub despawns: usize,
170-
/// Replication messages received.
171-
pub messages: usize,
172-
/// Replication bytes received in message payloads (without internal messaging plugin data).
173-
pub bytes: usize,
174-
}
175-
176-
/// Marker for entities spawned by replication.
177-
///
178-
/// Automatically inserted for each newly received entity.
179-
///
180-
/// See also [`Replicated`].
181-
#[derive(Component, Default, Reflect, Debug, Clone, Copy)]
182-
#[reflect(Component)]
183-
pub struct Remote;

src/lib.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ For implementation details see [`ServerChannel`](shared::backend::channels::Serv
105105
### Tick rate
106106
107107
By default, updates are not sent every frame in order to save bandwidth. Replication runs
108-
in [`ServerSystems::Send`] whenever the [`ServerTick`](server::server_tick::ServerTick) resource
108+
in [`ServerSystems::Send`] whenever the [`ServerTick`](shared::replication::send::server_tick::ServerTick) resource
109109
changes and if the state is [`ServerState::Running`].
110110
111111
By default, the tick is incremented in [`FixedPostUpdate`] each time [`FixedMain`](bevy::app::FixedMain)
@@ -283,6 +283,8 @@ These messages will appear on server as [`FromClient`] wrapper message that
283283
contains sender ID and the message.
284284
285285
```
286+
# #[cfg(all(feature = "client", feature = "server"))]
287+
# {
286288
# use bevy::{prelude::*, state::app::StatesPlugin};
287289
# use bevy_replicon::prelude::*;
288290
# use serde::{Deserialize, Serialize};
@@ -312,6 +314,7 @@ fn receive(mut pings: MessageReader<FromClient<Ping>>) {
312314
313315
#[derive(Message, Deserialize, Serialize)]
314316
struct Ping;
317+
# }
315318
```
316319
317320
If a message contains an entity, implement
@@ -327,6 +330,8 @@ Alternatively, you can use events with a similar API. First, you need to registe
327330
using [`ClientEventAppExt::add_client_event`], and then use [`ClientTriggerExt::client_trigger`].
328331
329332
```
333+
# #[cfg(all(feature = "client", feature = "server"))]
334+
# {
330335
# use bevy::{prelude::*, state::app::StatesPlugin};
331336
# use bevy_replicon::prelude::*;
332337
# use serde::{Deserialize, Serialize};
@@ -345,6 +350,7 @@ fn receive(ping: On<FromClient<Ping>>) {
345350
}
346351
# #[derive(Event, Deserialize, Serialize)]
347352
# struct Ping;
353+
# }
348354
```
349355
350356
For events with entities inside use [`ClientEventAppExt::add_mapped_client_event`].
@@ -358,6 +364,8 @@ and send it from server using [`ToClients`]. This wrapper contains send paramete
358364
and the message itself.
359365
360366
```
367+
# #[cfg(all(feature = "client", feature = "server"))]
368+
# {
361369
# use bevy::{prelude::*, state::app::StatesPlugin};
362370
# use bevy_replicon::prelude::*;
363371
# use serde::{Deserialize, Serialize};
@@ -390,6 +398,7 @@ fn receive(mut pongs: MessageReader<Pong>) {
390398
391399
#[derive(Message, Deserialize, Serialize)]
392400
struct Pong;
401+
# }
393402
```
394403
395404
Just like for client messages, we provide [`ServerMessageAppExt::add_mapped_server_message`]
@@ -727,11 +736,13 @@ pub mod prelude {
727736

728737
#[cfg(feature = "server")]
729738
#[expect(deprecated, reason = "Re-export of deprecated aliases")]
730-
pub use super::server::{
731-
AuthorizedClient, PriorityMap, ServerPlugin, ServerSystems,
732-
message::ServerMessagePlugin,
733-
visibility::{
734-
AppVisibilityExt, ComponentScope, FilterScope, SingleComponent, VisibilityFilter,
739+
pub use super::{
740+
server::{AuthorizedClient, ServerPlugin, ServerSystems, message::ServerMessagePlugin},
741+
shared::replication::send::{
742+
priority_map::PriorityMap,
743+
visibility::{
744+
AppVisibilityExt, ComponentScope, FilterScope, SingleComponent, VisibilityFilter,
745+
},
735746
},
736747
};
737748

src/server.rs

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
pub mod message;
2-
pub mod server_tick;
3-
pub mod visibility;
42

53
use core::time::Duration;
64

75
use bevy::{
8-
ecs::{entity::EntityHashMap, intern::Interned, schedule::ScheduleLabel},
6+
ecs::{intern::Interned, schedule::ScheduleLabel},
97
platform::collections::HashSet,
108
prelude::*,
119
time::common_conditions::on_timer,
@@ -14,10 +12,6 @@ use log::{Level, debug, log_enabled, trace};
1412

1513
use crate::{
1614
prelude::*,
17-
server::{
18-
server_tick::ServerTick, visibility::client_visibility::ClientVisibility,
19-
visibility::registry::FilterRegistry,
20-
},
2115
shared::{
2216
message::server_message::message_buffer::MessageBuffer,
2317
replication::{
@@ -28,14 +22,18 @@ use crate::{
2822
client_pools::ClientPools,
2923
client_ticks::ClientTicks,
3024
collect_changes, collect_despawns, collect_mappings, collect_removals,
31-
prepare_messages, receive_acks,
25+
prepare_messages,
26+
priority_map::PriorityMap,
27+
receive_acks,
3228
related_entities::RelatedEntities,
3329
removal_buffer::RemovalBuffer,
3430
replicated_archetypes::ReplicatedArchetypes,
3531
replication_messages::{
3632
mutations::Mutations, serialized_data::SerializedData, updates::Updates,
3733
},
3834
send_messages,
35+
server_tick::ServerTick,
36+
visibility::{client_visibility::ClientVisibility, registry::FilterRegistry},
3937
},
4038
},
4139
},
@@ -315,26 +313,3 @@ pub enum ServerSystems {
315313
#[component(immutable)]
316314
#[require(ClientTicks, ClientVisibility, PriorityMap, Updates, Mutations)]
317315
pub struct AuthorizedClient;
318-
319-
/// Controls how often mutations are sent for an authorized client.
320-
///
321-
/// Associates entities with a priority number configurable by the user.
322-
/// If the priority is not set, it defaults to 1.0.
323-
///
324-
/// During replication, we multiply the difference between the last acknowledged tick
325-
/// and [`ServerTick`] by the priority. If the result is greater than or equal to 1.0,
326-
/// we send mutations for this entity.
327-
///
328-
/// This means the priority accumulates across server ticks until an entity is acknowledged,
329-
/// at which point its priority is reset. As a result, even low-priority objects eventually
330-
/// reach a high enough priority to be considered for replication.
331-
///
332-
/// For example, if the base priority is 0.5, mutations for an entity will be sent
333-
/// no more often than once every 2 ticks. With the default priority of 1.0,
334-
/// all unacknowledged mutations will be sent every tick.
335-
///
336-
/// All of this only affects mutations. For any component insertion or removal, the changes
337-
/// will be sent using [`ServerChannel::Updates`](crate::shared::backend::channels::ServerChannel::Updates).
338-
/// See its documentation for more details.
339-
#[derive(Component, Reflect, Deref, DerefMut, Default, Debug, Clone)]
340-
pub struct PriorityMap(EntityHashMap<f32>);

src/server/message.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ use bevy::{
33
prelude::*,
44
};
55

6-
use super::server_tick::ServerTick;
76
use crate::{
87
prelude::*,
98
shared::message::{
109
ctx::{ServerReceiveCtx, ServerSendCtx},
1110
registry::RemoteMessageRegistry,
1211
server_message::message_buffer::MessageBuffer,
1312
},
14-
shared::replication::send::client_ticks::ClientTicks,
13+
shared::replication::send::{client_ticks::ClientTicks, server_tick::ServerTick},
1514
};
1615

1716
/// Sending messages and events from the server to clients.

src/shared.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct RepliconSharedPlugin {
3030
[`AuthMethod::ProtocolCheck`], but it could be any event.
3131
3232
```
33+
# #[cfg(feature = "server")]
34+
# {
3335
use bevy::{prelude::*, state::app::StatesPlugin};
3436
use bevy_replicon::prelude::*;
3537
use serde::{Deserialize, Serialize};
@@ -93,6 +95,7 @@ pub struct RepliconSharedPlugin {
9395
protocol: ProtocolHash,
9496
player_name: String,
9597
}
98+
# }
9699
```
97100
**/
98101
pub auth_method: AuthMethod,

src/shared/replication/receive.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
pub mod client_replication_stats;
12
pub mod confirm_history;
3+
pub mod remote;
24
pub mod server_mutate_ticks;
35

46
use bevy::prelude::*;
@@ -7,7 +9,6 @@ use log::{debug, error, trace};
79
use postcard::experimental::max_size::MaxSize;
810

911
use crate::{
10-
client::{ClientReplicationStats, Remote},
1112
postcard_utils,
1213
prelude::*,
1314
shared::{
@@ -26,7 +27,9 @@ use crate::{
2627
server_entity_map::{EntityEntry, ServerEntityMap},
2728
},
2829
};
30+
use client_replication_stats::ClientReplicationStats;
2931
use confirm_history::{ConfirmHistory, EntityReplicated};
32+
use remote::Remote;
3033
use server_mutate_ticks::{MutateTickReceived, ServerMutateTicks};
3134

3235
/// Receives and applies replication messages from the server.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use bevy::prelude::*;
2+
3+
/// Replication stats during message processing.
4+
///
5+
/// Statistic will be collected only if the resource is present.
6+
/// The resource is not added by default.
7+
///
8+
/// See also [`ClientDiagnosticsPlugin`](crate::client::diagnostics::ClientDiagnosticsPlugin)
9+
/// for automatic integration with Bevy diagnostics.
10+
#[derive(Resource, Default, Reflect, Debug, Clone, Copy)]
11+
pub struct ClientReplicationStats {
12+
/// Incremented per entity that changes.
13+
pub entities_changed: usize,
14+
/// Incremented for every component that changes.
15+
pub components_changed: usize,
16+
/// Incremented per client mapping added.
17+
pub mappings: usize,
18+
/// Incremented per entity despawn.
19+
pub despawns: usize,
20+
/// Replication messages received.
21+
pub messages: usize,
22+
/// Replication bytes received in message payloads (without internal messaging plugin data).
23+
pub bytes: usize,
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use bevy::prelude::*;
2+
3+
/// Marker for entities spawned by replication.
4+
///
5+
/// Automatically inserted for each newly received entity.
6+
///
7+
/// See also [`Replicated`](crate::shared::replication::Replicated).
8+
#[derive(Component, Default, Reflect, Debug, Clone, Copy)]
9+
#[reflect(Component)]
10+
pub struct Remote;

src/shared/replication/registry/test_fns.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ use bevy_replicon::{
3434
test_fns::TestFnsEntityExt, ReplicationRegistry,
3535
},
3636
replicon_tick::RepliconTick,
37+
server_entity_map::ServerEntityMap,
3738
},
3839
prelude::*,
3940
};
4041
use serde::{Deserialize, Serialize};
4142
4243
let mut app = App::new();
43-
app.add_plugins((MinimalPlugins, StatesPlugin, RepliconPlugins));
44+
app.add_plugins((MinimalPlugins, StatesPlugin, RepliconPlugins))
45+
.init_resource::<ServerEntityMap>();
4446
4547
let tick = RepliconTick::default();
4648

0 commit comments

Comments
 (0)