Skip to content

Commit 9011de4

Browse files
author
Charles Bournhonesque
committed
move more server stuff in send to avoid feature bounds
Change-Id: Ica56566aeab40df4ab18f299e9eb91313668e0fa
1 parent 5ef2261 commit 9011de4

15 files changed

Lines changed: 167 additions & 179 deletions

File tree

src/lib.rs

Lines changed: 16 additions & 15 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`](shared::replication::send::server_tick::ServerTick) resource
108+
in [`SendSystems::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)
@@ -294,7 +294,7 @@ app.add_client_message::<Ping>(Channel::Ordered)
294294
.add_systems(
295295
PreUpdate,
296296
receive
297-
.after(ServerSystems::Receive)
297+
.after(SendSystems::Receive)
298298
.run_if(in_state(ServerState::Running)),
299299
)
300300
.add_systems(
@@ -380,7 +380,7 @@ app.add_server_message::<Pong>(Channel::Ordered)
380380
)
381381
.add_systems(
382382
PostUpdate,
383-
send.before(ServerSystems::Send).run_if(in_state(ServerState::Running)),
383+
send.before(SendSystems::Send).run_if(in_state(ServerState::Running)),
384384
);
385385
386386
fn send(mut pongs: MessageWriter<ToClients<Pong>>) {
@@ -499,8 +499,8 @@ For server messages we drain [`ToClients<E>`] and, if the [`ClientId::Server`] i
499499
re-emit it as `E` locally. The same applies to the event API. This emulates message receiving for both server
500500
and singleplayer without actually transmitting data over the network.
501501
502-
We also provide [`ClientSystems`] and [`ServerSystems`] to schedule your system at specific time in the frame.
503-
For example, you can run your systems right after receive using [`ClientSystems::Receive`] or [`ServerSystems::Receive`].
502+
We also provide [`ClientSystems`] and [`SendSystems`] to schedule your system at specific time in the frame.
503+
For example, you can run your systems right after receive using [`ClientSystems::Receive`] or [`SendSystems::Receive`].
504504
505505
## Organizing your game code
506506
@@ -722,7 +722,6 @@ pub mod prelude {
722722
receive_markers::AppMarkerExt,
723723
registry::rule_fns::RuleFns,
724724
rules::{AppRuleExt, component::ReplicationMode},
725-
send::related_entities::SyncRelatedAppExt,
726725
signature::Signature,
727726
},
728727
replicon_tick::RepliconTick,
@@ -734,18 +733,20 @@ pub mod prelude {
734733
ClientPlugin, ClientReplicationStats, ClientSystems, Remote, message::ClientMessagePlugin,
735734
};
736735

737-
#[cfg(feature = "server")]
738-
#[expect(deprecated, reason = "Re-export of deprecated aliases")]
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-
},
736+
#[expect(deprecated, reason = "Re-export of deprecated alias")]
737+
pub use super::shared::replication::send::{
738+
AuthorizedClient, SendSystems,
739+
priority_map::PriorityMap,
740+
related_entities::SyncRelatedAppExt,
741+
server_tick::ServerTick,
742+
visibility::{
743+
AppVisibilityExt, ComponentScope, FilterScope, SingleComponent, VisibilityFilter,
746744
},
747745
};
748746

747+
#[cfg(feature = "server")]
748+
pub use super::server::{ServerPlugin, message::ServerMessagePlugin};
749+
749750
#[cfg(feature = "client_diagnostics")]
750751
pub use super::client::diagnostics::ClientDiagnosticsPlugin;
751752
}

src/server.rs

Lines changed: 16 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bevy::{
88
prelude::*,
99
time::common_conditions::on_timer,
1010
};
11-
use log::{Level, debug, log_enabled, trace};
11+
use log::{Level, debug, log_enabled};
1212

1313
use crate::{
1414
prelude::*,
@@ -17,23 +17,14 @@ use crate::{
1717
replication::{
1818
rules::ReplicationRules,
1919
send::{
20-
DespawnBuffer, ServerChangeTick, buffer_despawn, buffer_removals,
21-
check_mutation_ticks, cleanup_acks,
22-
client_pools::ClientPools,
23-
client_ticks::ClientTicks,
24-
collect_changes, collect_despawns, collect_mappings, collect_removals,
25-
prepare_messages,
26-
priority_map::PriorityMap,
27-
receive_acks,
28-
related_entities::RelatedEntities,
29-
removal_buffer::RemovalBuffer,
20+
self, AuthorizedClient, DespawnBuffer, SendSystems, ServerChangeTick,
21+
buffer_despawn, buffer_removals, check_mutation_ticks, cleanup_acks,
22+
client_pools::ClientPools, collect_changes, collect_despawns, collect_mappings,
23+
collect_removals, increment_tick, prepare_messages, receive_acks,
24+
related_entities::RelatedEntities, removal_buffer::RemovalBuffer,
3025
replicated_archetypes::ReplicatedArchetypes,
31-
replication_messages::{
32-
mutations::Mutations, serialized_data::SerializedData, updates::Updates,
33-
},
34-
send_messages,
35-
server_tick::ServerTick,
36-
visibility::{client_visibility::ClientVisibility, registry::FilterRegistry},
26+
replication_messages::serialized_data::SerializedData, send_messages,
27+
server_tick::ServerTick, visibility::registry::FilterRegistry,
3728
},
3829
},
3930
},
@@ -109,14 +100,14 @@ impl Plugin for ServerPlugin {
109100
.init_resource::<FilterRegistry>()
110101
.configure_sets(
111102
PreUpdate,
112-
(ServerSystems::ReceivePackets, ServerSystems::Receive).chain(),
103+
(SendSystems::ReceivePackets, SendSystems::Receive).chain(),
113104
)
114105
.configure_sets(
115106
PostUpdate,
116107
(
117-
ServerSystems::IncrementTick,
118-
ServerSystems::Send,
119-
ServerSystems::SendPackets,
108+
SendSystems::IncrementTick,
109+
SendSystems::Send,
110+
SendSystems::SendPackets,
120111
)
121112
.chain(),
122113
)
@@ -131,10 +122,10 @@ impl Plugin for ServerPlugin {
131122
cleanup_acks(self.mutations_timeout).run_if(on_timer(self.mutations_timeout)),
132123
)
133124
.chain()
134-
.in_set(ServerSystems::Receive)
125+
.in_set(SendSystems::Receive)
135126
.run_if(in_state(ServerState::Running)),
136127
)
137-
.add_systems(OnExit(ServerState::Running), reset)
128+
.add_systems(OnExit(ServerState::Running), send::reset)
138129
.add_systems(
139130
PostUpdate,
140131
(
@@ -147,7 +138,7 @@ impl Plugin for ServerPlugin {
147138
)
148139
.chain()
149140
.run_if(resource_changed::<ServerTick>)
150-
.in_set(ServerSystems::Send)
141+
.in_set(SendSystems::Send)
151142
.run_if(in_state(ServerState::Running)),
152143
);
153144

@@ -156,7 +147,7 @@ impl Plugin for ServerPlugin {
156147
app.add_systems(
157148
tick_schedule,
158149
increment_tick
159-
.in_set(ServerSystems::IncrementTick)
150+
.in_set(SendSystems::IncrementTick)
160151
.run_if(in_state(ServerState::Running)),
161152
);
162153
}
@@ -242,74 +233,3 @@ fn check_protocol(
242233
disconnects.write(DisconnectRequest { client });
243234
}
244235
}
245-
246-
/// Increments current server tick which causes the server to replicate this frame.
247-
pub fn increment_tick(mut server_tick: ResMut<ServerTick>) {
248-
trace!("incrementing `{:?}`", *server_tick);
249-
server_tick.increment();
250-
}
251-
252-
fn reset(
253-
mut commands: Commands,
254-
mut messages: ResMut<ServerMessages>,
255-
mut server_tick: ResMut<ServerTick>,
256-
mut related_entities: ResMut<RelatedEntities>,
257-
clients: Query<Entity, With<ConnectedClient>>,
258-
mut message_buffer: ResMut<MessageBuffer>,
259-
) {
260-
messages.clear();
261-
*server_tick = Default::default();
262-
message_buffer.clear();
263-
related_entities.clear();
264-
for client in &clients {
265-
commands.entity(client).despawn();
266-
}
267-
}
268-
269-
/// Set with replication and event systems related to server.
270-
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone, Copy)]
271-
pub enum ServerSystems {
272-
/// Systems that receive packets from the messaging backend and update [`ServerState`].
273-
///
274-
/// Used by the messaging backend.
275-
///
276-
/// Runs in [`PreUpdate`].
277-
ReceivePackets,
278-
/// Systems that read data from [`ServerMessages`].
279-
///
280-
/// Runs in [`PreUpdate`].
281-
Receive,
282-
/// Systems that build the initial graph with all related entities registered via
283-
/// [`crate::shared::replication::send::related_entities::SyncRelatedAppExt::sync_related_entities`].
284-
///
285-
/// The graph is kept in sync with observers.
286-
///
287-
/// Runs in [`OnEnter`] for [`ServerState::Running`].
288-
ReadRelations,
289-
/// System that increments [`ServerTick`].
290-
///
291-
/// Runs in [`ServerPlugin::tick_schedule`].
292-
IncrementTick,
293-
/// Systems that write data to [`ServerMessages`].
294-
///
295-
/// Runs in [`PostUpdate`] if [`ServerTick`] changes.
296-
Send,
297-
/// Systems that send packets to the messaging backend.
298-
///
299-
/// Used by the messaging backend.
300-
///
301-
/// Runs in [`PostUpdate`] if [`ServerTick`] changes.
302-
SendPackets,
303-
}
304-
305-
/// Marker that enables replication and all events for a client.
306-
///
307-
/// Until authorization happened, the client and server can still exchange network events that are marked as
308-
/// independent via [`ServerMessageAppExt::make_message_independent`] or [`ServerEventAppExt::make_event_independent`].
309-
/// **All other events will be ignored**.
310-
///
311-
/// See also [`ConnectedClient`] and [`RepliconSharedPlugin::auth_method`].
312-
#[derive(Component, Reflect, Default)]
313-
#[component(immutable)]
314-
#[require(ClientTicks, ClientVisibility, PriorityMap, Updates, Mutations)]
315-
pub struct AuthorizedClient;

src/server/message.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use crate::{
1010
registry::RemoteMessageRegistry,
1111
server_message::message_buffer::MessageBuffer,
1212
},
13-
shared::replication::send::{client_ticks::ClientTicks, server_tick::ServerTick},
13+
shared::replication::send::{
14+
self, SendSystems, client_ticks::ClientTicks, server_tick::ServerTick,
15+
},
1416
};
1517

1618
/// Sending messages and events from the server to clients.
@@ -94,7 +96,7 @@ impl Plugin for ServerMessagePlugin {
9496
trigger_fn.run_if(in_state(ClientState::Disconnected)),
9597
)
9698
.chain()
97-
.in_set(ServerSystems::Receive),
99+
.in_set(SendSystems::Receive),
98100
)
99101
.add_systems(
100102
PostUpdate,
@@ -106,8 +108,8 @@ impl Plugin for ServerMessagePlugin {
106108
send_locally_fn.run_if(in_state(ClientState::Disconnected)),
107109
)
108110
.chain()
109-
.after(super::send_messages)
110-
.in_set(ServerSystems::Send),
111+
.after(send::send_messages)
112+
.in_set(SendSystems::Send),
111113
);
112114
}
113115
}

src/shared.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use replication::{
1616
receive_markers::ReceiveMarkers, registry::ReplicationRegistry, rules::ReplicationRules,
1717
track_mutate_messages::TrackMutateMessages,
1818
};
19+
use server_entity_map::ServerEntityMap;
1920

2021
/// Initializes types, resources and events needed for both client and server.
2122
#[derive(Default)]
@@ -30,8 +31,6 @@ pub struct RepliconSharedPlugin {
3031
[`AuthMethod::ProtocolCheck`], but it could be any event.
3132
3233
```
33-
# #[cfg(feature = "server")]
34-
# {
3534
use bevy::{prelude::*, state::app::StatesPlugin};
3635
use bevy_replicon::prelude::*;
3736
use serde::{Deserialize, Serialize};
@@ -95,7 +94,6 @@ pub struct RepliconSharedPlugin {
9594
protocol: ProtocolHash,
9695
player_name: String,
9796
}
98-
# }
9997
```
10098
**/
10199
pub auth_method: AuthMethod,
@@ -107,6 +105,7 @@ impl Plugin for RepliconSharedPlugin {
107105
.init_state::<ServerState>()
108106
.init_resource::<ProtocolHasher>()
109107
.init_resource::<NetworkIdMap>()
108+
.init_resource::<ServerEntityMap>()
110109
.init_resource::<TrackMutateMessages>()
111110
.init_resource::<RepliconChannels>()
112111
.init_resource::<ReplicationRegistry>()

src/shared/backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub enum ClientState {
5353
/// <div class="warning">
5454
///
5555
/// Should only be changed from the messaging backend when the server changes its state
56-
/// in [`ServerSystems::ReceivePackets`](crate::server::ServerSystems::ReceivePackets).
56+
/// in [`SendSystems::ReceivePackets`](crate::prelude::SendSystems::ReceivePackets).
5757
///
5858
/// </div>
5959
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]

src/shared/backend/server_messages.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use log::trace;
66
///
77
/// The messaging backend is responsible for updating this resource:
88
/// - Received messages should be forwarded to Replicon via [`Self::insert_received`] in
9-
/// [`ServerSystems::ReceivePackets`](crate::prelude::ServerSystems::ReceivePackets).
9+
/// [`SendSystems::ReceivePackets`](crate::prelude::SendSystems::ReceivePackets).
1010
/// - Replicon messages needs to be forwarded to the backend via [`Self::drain_sent`] in
11-
/// [`ServerSystems::SendPackets`](crate::prelude::ServerSystems::SendPackets).
11+
/// [`SendSystems::SendPackets`](crate::prelude::SendSystems::SendPackets).
1212
///
1313
/// Inserted as resource by [`ServerPlugin`](crate::server::ServerPlugin).
1414
#[derive(Resource, Default)]

src/shared/replication/registry/test_fns.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,13 @@ use bevy_replicon::{
3434
test_fns::TestFnsEntityExt, ReplicationRegistry,
3535
},
3636
replicon_tick::RepliconTick,
37-
server_entity_map::ServerEntityMap,
3837
},
3938
prelude::*,
4039
};
4140
use serde::{Deserialize, Serialize};
4241
4342
let mut app = App::new();
44-
app.add_plugins((MinimalPlugins, StatesPlugin, RepliconPlugins))
45-
.init_resource::<ServerEntityMap>();
43+
app.add_plugins((MinimalPlugins, StatesPlugin, RepliconPlugins));
4644
4745
let tick = RepliconTick::default();
4846

0 commit comments

Comments
 (0)