-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathserver.rs
More file actions
731 lines (672 loc) · 24.9 KB
/
server.rs
File metadata and controls
731 lines (672 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
pub mod client_entity_map;
pub mod client_visibility;
pub(super) mod despawn_buffer;
pub mod event;
pub(super) mod removal_buffer;
pub(super) mod replication_messages;
pub mod server_tick;
mod server_world;
use core::{ops::Range, time::Duration};
use bevy::{
ecs::{component::StorageType, system::SystemChangeTick},
prelude::*,
ptr::Ptr,
reflect::TypeRegistry,
time::common_conditions::on_timer,
};
use bytes::Buf;
use log::{debug, trace};
use crate::shared::{
backend::{
connected_client::ConnectedClient,
replicon_channels::{ReplicationChannel, RepliconChannels},
replicon_server::RepliconServer,
},
common_conditions::*,
event::server_event::BufferedServerEvents,
postcard_utils,
replication::{
client_ticks::{ClientTicks, EntityBuffer},
replication_registry::{
ReplicationRegistry, component_fns::ComponentFns, ctx::SerializeCtx,
rule_fns::UntypedRuleFns,
},
track_mutate_messages::TrackMutateMessages,
},
replicon_tick::RepliconTick,
};
use client_entity_map::ClientEntityMap;
use client_visibility::{ClientVisibility, Visibility};
use despawn_buffer::{DespawnBuffer, DespawnBufferPlugin};
use removal_buffer::{RemovalBuffer, RemovalBufferPlugin};
use replication_messages::{
mutations::Mutations, serialized_data::SerializedData, updates::Updates,
};
use server_tick::ServerTick;
use server_world::{ReplicatedComponent, ServerWorld};
pub struct ServerPlugin {
/// Tick configuration.
///
/// By default it's 30 ticks per second.
pub tick_policy: TickPolicy,
/// Visibility configuration.
pub visibility_policy: VisibilityPolicy,
/// The time after which mutations will be considered lost if an acknowledgment is not received for them.
///
/// In practice mutations will live at least `mutations_timeout`, and at most `2*mutations_timeout`.
pub mutations_timeout: Duration,
/// If enabled, replication will be started automatically after connection.
///
/// If disabled, replication should be started manually by inserting [`ReplicatedClient`] on the client entity.
///
/// Until replication has started, the client and server can still exchange network events that are marked as
/// independent via [`ServerEventAppExt::make_independent`](crate::shared::event::server_event::ServerEventAppExt::make_independent).
/// **All other events will be ignored**.
pub replicate_after_connect: bool,
}
impl Default for ServerPlugin {
fn default() -> Self {
Self {
tick_policy: TickPolicy::MaxTickRate(30),
visibility_policy: Default::default(),
mutations_timeout: Duration::from_secs(10),
replicate_after_connect: true,
}
}
}
/// Server functionality and replication sending.
///
/// Can be disabled for client-only apps.
impl Plugin for ServerPlugin {
fn build(&self, app: &mut App) {
app.add_plugins((DespawnBufferPlugin, RemovalBufferPlugin))
.init_resource::<RepliconServer>()
.init_resource::<ServerTick>()
.init_resource::<EntityBuffer>()
.init_resource::<BufferedServerEvents>()
.configure_sets(
PreUpdate,
(ServerSet::ReceivePackets, ServerSet::Receive).chain(),
)
.configure_sets(
PostUpdate,
(ServerSet::Send, ServerSet::SendPackets).chain(),
)
.add_observer(handle_connects)
.add_observer(handle_disconnects)
.add_systems(Startup, setup_channels)
.add_systems(
PreUpdate,
(
receive_acks,
cleanup_acks(self.mutations_timeout).run_if(on_timer(self.mutations_timeout)),
)
.chain()
.in_set(ServerSet::Receive)
.run_if(server_running),
)
.add_systems(
PostUpdate,
(
send_replication
.map(Result::unwrap)
.in_set(ServerSet::Send)
.run_if(server_running)
.run_if(resource_changed::<ServerTick>),
reset.run_if(server_just_stopped),
),
);
match self.tick_policy {
TickPolicy::MaxTickRate(max_tick_rate) => {
let tick_time = Duration::from_millis(1000 / max_tick_rate as u64);
app.add_systems(
PostUpdate,
increment_tick
.before(send_replication)
.run_if(server_running)
.run_if(on_timer(tick_time)),
);
}
TickPolicy::EveryFrame => {
app.add_systems(
PostUpdate,
increment_tick
.before(send_replication)
.run_if(server_running),
);
}
TickPolicy::Manual => (),
}
match self.visibility_policy {
VisibilityPolicy::All => {}
VisibilityPolicy::Blacklist => {
app.register_required_components_with::<ReplicatedClient, _>(
ClientVisibility::blacklist,
);
}
VisibilityPolicy::Whitelist => {
app.register_required_components_with::<ReplicatedClient, _>(
ClientVisibility::whitelist,
);
}
}
if self.replicate_after_connect {
app.register_required_components::<ConnectedClient, ReplicatedClient>();
}
}
}
fn setup_channels(mut server: ResMut<RepliconServer>, channels: Res<RepliconChannels>) {
server.setup_client_channels(channels.client_channels().len());
}
/// Increments current server tick which causes the server to replicate this frame.
pub fn increment_tick(mut server_tick: ResMut<ServerTick>) {
server_tick.increment();
trace!("incremented {server_tick:?}");
}
fn handle_connects(
trigger: Trigger<OnAdd, ConnectedClient>,
mut buffered_events: ResMut<BufferedServerEvents>,
) {
debug!("client `{}` connected", trigger.target());
buffered_events.exclude_client(trigger.target());
}
fn handle_disconnects(
trigger: Trigger<OnRemove, ConnectedClient>,
mut server: ResMut<RepliconServer>,
) {
debug!("client `{}` disconnected", trigger.target());
server.remove_client(trigger.target());
}
fn cleanup_acks(
mutations_timeout: Duration,
) -> impl FnMut(Query<&mut ClientTicks>, ResMut<EntityBuffer>, Res<Time>) {
move |mut clients: Query<&mut ClientTicks>,
mut entity_buffer: ResMut<EntityBuffer>,
time: Res<Time>| {
let min_timestamp = time.elapsed().saturating_sub(mutations_timeout);
for mut ticks in &mut clients {
ticks.cleanup_older_mutations(&mut entity_buffer, min_timestamp);
}
}
}
fn receive_acks(
change_tick: SystemChangeTick,
mut server: ResMut<RepliconServer>,
mut clients: Query<&mut ClientTicks>,
mut entity_buffer: ResMut<EntityBuffer>,
) {
for (client_entity, mut message) in server.receive(ReplicationChannel::Updates) {
while message.has_remaining() {
match postcard_utils::from_buf(&mut message) {
Ok(mutate_index) => {
let mut ticks = clients.get_mut(client_entity).unwrap_or_else(|_| {
panic!("messages from client `{client_entity}` should have been removed on disconnect previously")
});
ticks.ack_mutate_message(
client_entity,
&mut entity_buffer,
change_tick.this_run(),
mutate_index,
);
}
Err(e) => {
debug!("unable to deserialize mutate index from client `{client_entity}`: {e}")
}
}
}
}
}
/// Collects [`ReplicationMessages`] and sends them.
pub(super) fn send_replication(
mut serialized: Local<SerializedData>,
change_tick: SystemChangeTick,
world: ServerWorld,
mut clients: Query<(
Entity,
&mut Updates,
&mut Mutations,
&mut ConnectedClient,
&mut ClientEntityMap,
&mut ClientTicks,
Option<&mut ClientVisibility>,
)>,
mut removal_buffer: ResMut<RemovalBuffer>,
mut entity_buffer: ResMut<EntityBuffer>,
mut despawn_buffer: ResMut<DespawnBuffer>,
mut server: ResMut<RepliconServer>,
track_mutate_messages: Res<TrackMutateMessages>,
registry: Res<ReplicationRegistry>,
type_registry: Res<AppTypeRegistry>,
server_tick: Res<ServerTick>,
time: Res<Time>,
) -> Result<()> {
for (_, mut mutations, mut updates, ..) in &mut clients {
updates.clear();
mutations.clear();
}
collect_mappings(&mut serialized, &mut clients)?;
collect_despawns(&mut serialized, &mut clients, &mut despawn_buffer)?;
collect_removals(&mut serialized, &mut clients, &removal_buffer)?;
collect_changes(
&mut serialized,
&mut clients,
®istry,
&type_registry.read(),
&removal_buffer,
&world,
&change_tick,
**server_tick,
)?;
removal_buffer.clear();
send_messages(
&mut clients,
&mut server,
**server_tick,
**track_mutate_messages,
&mut serialized,
&mut entity_buffer,
change_tick,
&time,
)?;
serialized.clear();
Ok(())
}
fn reset(
mut commands: Commands,
mut server_tick: ResMut<ServerTick>,
clients: Query<Entity, With<ConnectedClient>>,
mut buffered_events: ResMut<BufferedServerEvents>,
) {
*server_tick = Default::default();
buffered_events.clear();
for entity in &clients {
commands.entity(entity).despawn();
}
}
fn send_messages(
clients: &mut Query<(
Entity,
&mut Updates,
&mut Mutations,
&mut ConnectedClient,
&mut ClientEntityMap,
&mut ClientTicks,
Option<&mut ClientVisibility>,
)>,
server: &mut RepliconServer,
server_tick: RepliconTick,
track_mutate_messages: bool,
serialized: &mut SerializedData,
entity_buffer: &mut EntityBuffer,
change_tick: SystemChangeTick,
time: &Time,
) -> Result<()> {
let mut server_tick_range = None;
for (client_entity, updates, mut mutations, client, .., mut ticks, visibility) in clients {
if !updates.is_empty() {
ticks.set_update_tick(server_tick);
let server_tick = write_tick_cached(&mut server_tick_range, serialized, server_tick)?;
trace!("sending update message to client `{client_entity}`");
updates.send(server, client_entity, serialized, server_tick)?;
} else {
trace!("no updates to send for client `{client_entity}`");
}
if !mutations.is_empty() || track_mutate_messages {
let server_tick = write_tick_cached(&mut server_tick_range, serialized, server_tick)?;
let messages_count = mutations.send(
server,
client_entity,
&mut ticks,
entity_buffer,
serialized,
track_mutate_messages,
server_tick,
change_tick.this_run(),
time.elapsed(),
client.max_size,
)?;
trace!("sending {messages_count} mutate message(s) to client `{client_entity}`");
} else {
trace!("no mutations to send for client `{client_entity}`");
}
if let Some(mut visibility) = visibility {
visibility.update();
}
}
Ok(())
}
/// Collects and writes any new entity mappings that happened in this tick.
fn collect_mappings(
serialized: &mut SerializedData,
clients: &mut Query<(
Entity,
&mut Updates,
&mut Mutations,
&mut ConnectedClient,
&mut ClientEntityMap,
&mut ClientTicks,
Option<&mut ClientVisibility>,
)>,
) -> Result<()> {
for (_, mut message, _, _, mut entity_map, ..) in clients {
let len = entity_map.len();
let mappings = serialized.write_mappings(entity_map.0.drain(..))?;
message.set_mappings(mappings, len);
}
Ok(())
}
/// Collect entity despawns from this tick into update messages.
fn collect_despawns(
serialized: &mut SerializedData,
clients: &mut Query<(
Entity,
&mut Updates,
&mut Mutations,
&mut ConnectedClient,
&mut ClientEntityMap,
&mut ClientTicks,
Option<&mut ClientVisibility>,
)>,
despawn_buffer: &mut DespawnBuffer,
) -> Result<()> {
for entity in despawn_buffer.drain(..) {
let entity_range = serialized.write_entity(entity)?;
for (_, mut message, .., mut ticks, visibility) in &mut *clients {
if let Some(mut visibility) = visibility {
if visibility.is_visible(entity) {
message.add_despawn(entity_range.clone());
}
visibility.remove_despawned(entity);
} else {
message.add_despawn(entity_range.clone());
}
ticks.remove_entity(entity);
}
}
for (_, mut message, .., mut ticks, visibility) in clients {
if let Some(mut visibility) = visibility {
for entity in visibility.drain_lost() {
let entity_range = serialized.write_entity(entity)?;
message.add_despawn(entity_range);
ticks.remove_entity(entity);
}
}
}
Ok(())
}
/// Collects component removals from this tick into update messages.
fn collect_removals(
serialized: &mut SerializedData,
clients: &mut Query<(
Entity,
&mut Updates,
&mut Mutations,
&mut ConnectedClient,
&mut ClientEntityMap,
&mut ClientTicks,
Option<&mut ClientVisibility>,
)>,
removal_buffer: &RemovalBuffer,
) -> Result<()> {
for (&entity, remove_ids) in removal_buffer.iter() {
let entity_range = serialized.write_entity(entity)?;
let ids_len = remove_ids.len();
let fn_ids = serialized.write_fn_ids(remove_ids.iter().map(|&(_, fns_id)| fns_id))?;
for (_, mut message, .., visibility) in &mut *clients {
if visibility.is_none_or(|v| v.is_visible(entity)) {
message.add_removals(entity_range.clone(), ids_len, fn_ids.clone());
}
}
}
Ok(())
}
/// Collects component changes from this tick into update and mutate messages since the last entity tick.
fn collect_changes(
serialized: &mut SerializedData,
clients: &mut Query<(
Entity,
&mut Updates,
&mut Mutations,
&mut ConnectedClient,
&mut ClientEntityMap,
&mut ClientTicks,
Option<&mut ClientVisibility>,
)>,
registry: &ReplicationRegistry,
type_registry: &TypeRegistry,
removal_buffer: &RemovalBuffer,
world: &ServerWorld,
change_tick: &SystemChangeTick,
server_tick: RepliconTick,
) -> Result<()> {
for (archetype, replicated_archetype) in world.iter_archetypes() {
for entity in archetype.entities() {
let mut entity_range = None;
for (_, mut updates, mut mutations, .., visibility) in &mut *clients {
let visibility = visibility
.map(|v| v.state(entity.id()))
.unwrap_or(Visibility::Visible);
updates.start_entity_changes(visibility);
mutations.start_entity();
}
// SAFETY: all replicated archetypes have marker component with table storage.
let (_, marker_ticks) = unsafe {
world.get_component_unchecked(
entity,
archetype.table_id(),
StorageType::Table,
world.marker_id(),
)
};
// If the marker was added in this tick, the entity just started replicating.
// It could be a newly spawned entity or an old entity with just-enabled replication,
// so we need to include even old components that were registered for replication.
let marker_added =
marker_ticks.is_added(change_tick.last_run(), change_tick.this_run());
for replicated_component in &replicated_archetype.components {
let (component_id, component_fns, rule_fns) =
registry.get(replicated_component.fns_id);
// SAFETY: component and storage were obtained from this archetype.
let (component, ticks) = unsafe {
world.get_component_unchecked(
entity,
archetype.table_id(),
replicated_component.storage_type,
component_id,
)
};
let ctx = SerializeCtx {
server_tick,
component_id,
type_registry,
};
let mut component_range = None;
for (_, mut updates, mut mutations, .., client_ticks, _) in &mut *clients {
if updates.entity_visibility() == Visibility::Hidden {
continue;
}
if let Some(tick) = client_ticks
.mutation_tick(entity.id())
.filter(|_| !marker_added)
.filter(|_| updates.entity_visibility() != Visibility::Gained)
.filter(|_| !ticks.is_added(change_tick.last_run(), change_tick.this_run()))
{
if ticks.is_changed(tick, change_tick.this_run()) {
if !mutations.entity_added() {
let entity_range = write_entity_cached(
&mut entity_range,
serialized,
entity.id(),
)?;
mutations.add_entity(entity.id(), entity_range);
}
let component_range = write_component_cached(
&mut component_range,
serialized,
rule_fns,
component_fns,
&ctx,
replicated_component,
component,
)?;
mutations.add_component(component_range);
}
} else {
if !updates.changed_entity_added() {
let entity_range =
write_entity_cached(&mut entity_range, serialized, entity.id())?;
updates.add_changed_entity(entity_range);
}
let component_range = write_component_cached(
&mut component_range,
serialized,
rule_fns,
component_fns,
&ctx,
replicated_component,
component,
)?;
updates.add_inserted_component(component_range);
}
}
}
for (_, mut updates, mut mutations, .., mut ticks, _) in &mut *clients {
let visibility = updates.entity_visibility();
if visibility == Visibility::Hidden {
continue;
}
let new_entity = marker_added || visibility == Visibility::Gained;
if new_entity
|| updates.changed_entity_added()
|| removal_buffer.contains_key(&entity.id())
{
// If there is any insertion, removal, or it's a new entity for a client, include all mutations
// into update message and bump the last acknowledged tick to keep entity updates atomic.
updates.take_added_entity(&mut mutations);
ticks.set_mutation_tick(entity.id(), change_tick.this_run());
}
if new_entity && !updates.changed_entity_added() {
// Force-write new entity even if it doesn't have any components.
let entity_range =
write_entity_cached(&mut entity_range, serialized, entity.id())?;
updates.add_changed_entity(entity_range);
}
}
}
}
Ok(())
}
/// Writes an entity or re-uses previously written range if exists.
fn write_entity_cached(
entity_range: &mut Option<Range<usize>>,
serialized: &mut SerializedData,
entity: Entity,
) -> Result<Range<usize>> {
if let Some(range) = entity_range.clone() {
return Ok(range);
}
let range = serialized.write_entity(entity)?;
*entity_range = Some(range.clone());
Ok(range)
}
/// Writes a component or re-uses previously written range if exists.
fn write_component_cached(
component_range: &mut Option<Range<usize>>,
serialized: &mut SerializedData,
rule_fns: &UntypedRuleFns,
component_fns: &ComponentFns,
ctx: &SerializeCtx,
replicated_component: &ReplicatedComponent,
component: Ptr<'_>,
) -> Result<Range<usize>> {
if let Some(component_range) = component_range.clone() {
return Ok(component_range);
}
let range = serialized.write_component(
rule_fns,
component_fns,
ctx,
replicated_component.fns_id,
component,
)?;
*component_range = Some(range.clone());
Ok(range)
}
/// Writes an entity or re-uses previously written range if exists.
fn write_tick_cached(
tick_range: &mut Option<Range<usize>>,
serialized: &mut SerializedData,
tick: RepliconTick,
) -> Result<Range<usize>> {
if let Some(range) = tick_range.clone() {
return Ok(range);
}
let range = serialized.write_tick(tick)?;
*tick_range = Some(range.clone());
Ok(range)
}
/// Set with replication and event systems related to server.
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum ServerSet {
/// Systems that receive packets from the messaging backend.
///
/// Used by the messaging backend.
///
/// Runs in [`PreUpdate`].
ReceivePackets,
/// Systems that receive data from [`RepliconServer`].
///
/// Used by `bevy_replicon`.
///
/// Runs in [`PreUpdate`].
Receive,
/// Systems that send data to [`RepliconServer`].
///
/// Used by `bevy_replicon`.
///
/// Runs in [`PostUpdate`] on server tick, see [`TickPolicy`].
Send,
/// Systems that send packets to the messaging backend.
///
/// Used by the messaging backend.
///
/// Runs in [`PostUpdate`] on server tick, see [`TickPolicy`].
SendPackets,
}
/// Controls how often [`RepliconTick`] is incremented on the server.
///
/// When [`RepliconTick`] is mutated, the server's replication
/// system will run. This means the tick policy controls how often server state is replicated.
///
/// Note that component mutations are replicated over the unreliable channel, so if a component mutate message is lost
/// then component mutations won't be resent until the server's replication system runs again.
#[derive(Debug, Copy, Clone)]
pub enum TickPolicy {
/// The replicon tick is incremented at most max ticks per second. In practice the tick rate may be lower if the
/// app's update cycle duration is too long.
MaxTickRate(u16),
/// The replicon tick is incremented every frame.
EveryFrame,
/// The user should manually schedule [`increment_tick`] or increment [`RepliconTick`].
Manual,
}
/// Marker that enables replication for client entity.
///
/// If [`ServerPlugin::replicate_after_connect`] is set, it will be marked as required
/// for [`ConnectedClient`].
///
/// Pausing replication by temporarily removing this component is not supported.
#[derive(Component, Default)]
#[require(ClientTicks, ClientEntityMap, Updates, Mutations)]
pub struct ReplicatedClient;
/// Controls how visibility will be managed via [`ClientVisibility`].
#[derive(Default, Debug, Clone, Copy)]
pub enum VisibilityPolicy {
/// All entities are visible by default and visibility can't be changed.
#[default]
All,
/// All entities are visible by default and should be explicitly registered to be hidden.
Blacklist,
/// All entities are hidden by default and should be explicitly registered to be visible.
Whitelist,
}