-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathserver.rs
595 lines (544 loc) · 20.8 KB
/
server.rs
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
pub mod client_entity_map;
pub(super) mod despawn_buffer;
pub mod events;
pub(super) mod removal_buffer;
pub(super) mod replicated_archetypes;
pub(super) mod replication_messages;
pub mod server_tick;
use std::{io::Cursor, mem, time::Duration};
use bevy::{
ecs::{
archetype::ArchetypeEntity,
component::{ComponentId, ComponentTicks, StorageType},
entity::EntityHashSet,
storage::{SparseSets, Table},
system::SystemChangeTick,
},
prelude::*,
ptr::Ptr,
time::common_conditions::on_timer,
};
use crate::core::{
channels::{ReplicationChannel, RepliconChannels},
common_conditions::{server_just_stopped, server_running},
connected_clients::{
client_visibility::Visibility, ClientBuffers, ConnectedClient, ConnectedClients,
VisibilityPolicy,
},
ctx::SerializeCtx,
replication_registry::ReplicationRegistry,
replication_rules::ReplicationRules,
replicon_server::RepliconServer,
replicon_tick::RepliconTick,
ClientId,
};
use client_entity_map::ClientEntityMap;
use despawn_buffer::{DespawnBuffer, DespawnBufferPlugin};
use removal_buffer::{RemovalBuffer, RemovalBufferPlugin};
use replicated_archetypes::ReplicatedArchetypes;
use replication_messages::ReplicationMessages;
use server_tick::ServerTick;
pub struct ServerPlugin {
/// Tick configuration.
pub tick_policy: TickPolicy,
/// Visibility configuration.
pub visibility_policy: VisibilityPolicy,
/// The time after which updates will be considered lost if an acknowledgment is not received for them.
///
/// In practice updates will live at least `update_timeout`, and at most `2*update_timeout`.
pub update_timeout: Duration,
}
impl Default for ServerPlugin {
fn default() -> Self {
Self {
tick_policy: TickPolicy::MaxTickRate(30),
visibility_policy: Default::default(),
update_timeout: Duration::from_secs(10),
}
}
}
/// 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::<ClientBuffers>()
.init_resource::<ClientEntityMap>()
.insert_resource(ConnectedClients::new(self.visibility_policy))
.add_event::<ServerEvent>()
.configure_sets(
PreUpdate,
(
ServerSet::ReceivePackets,
ServerSet::SendEvents,
ServerSet::Receive,
)
.chain(),
)
.configure_sets(
PostUpdate,
(
ServerSet::StoreHierarchy,
ServerSet::Send,
ServerSet::SendPackets,
)
.chain(),
)
.add_systems(Startup, Self::setup_channels)
.add_systems(
PreUpdate,
(
Self::handle_connections,
Self::receive_acks,
Self::cleanup_acks(self.update_timeout).run_if(on_timer(self.update_timeout)),
)
.chain()
.in_set(ServerSet::Receive)
.run_if(server_running),
)
.add_systems(
PostUpdate,
(
Self::send_replication
.map(Result::unwrap)
.in_set(ServerSet::Send)
.run_if(server_running)
.run_if(resource_changed::<ServerTick>),
Self::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,
Self::increment_tick
.before(Self::send_replication)
.run_if(server_running)
.run_if(on_timer(tick_time)),
);
}
TickPolicy::EveryFrame => {
app.add_systems(
PostUpdate,
Self::increment_tick
.before(Self::send_replication)
.run_if(server_running),
);
}
TickPolicy::Manual => (),
}
}
}
impl ServerPlugin {
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_connections(
mut server_events: EventReader<ServerEvent>,
mut entity_map: ResMut<ClientEntityMap>,
mut connected_clients: ResMut<ConnectedClients>,
mut server: ResMut<RepliconServer>,
mut client_buffers: ResMut<ClientBuffers>,
) {
for event in server_events.read() {
match *event {
ServerEvent::ClientDisconnected { client_id, .. } => {
entity_map.0.remove(&client_id);
connected_clients.remove(&mut client_buffers, client_id);
server.remove_client(client_id);
}
ServerEvent::ClientConnected { client_id } => {
connected_clients.add(&mut client_buffers, client_id);
}
}
}
}
fn cleanup_acks(
update_timeout: Duration,
) -> impl FnMut(ResMut<ConnectedClients>, ResMut<ClientBuffers>, Res<Time>) {
move |mut connected_clients: ResMut<ConnectedClients>,
mut client_buffers: ResMut<ClientBuffers>,
time: Res<Time>| {
let min_timestamp = time.elapsed().saturating_sub(update_timeout);
for client in connected_clients.iter_mut() {
client.remove_older_updates(&mut client_buffers, min_timestamp);
}
}
}
fn receive_acks(
change_tick: SystemChangeTick,
mut server: ResMut<RepliconServer>,
mut connected_clients: ResMut<ConnectedClients>,
mut client_buffers: ResMut<ClientBuffers>,
) {
for (client_id, message) in server.receive(ReplicationChannel::Init) {
let mut cursor = Cursor::new(&*message);
let message_end = message.len() as u64;
while cursor.position() < message_end {
match bincode::deserialize_from(&mut cursor) {
Ok(update_index) => {
let client = connected_clients.client_mut(client_id);
client.acknowledge(
&mut client_buffers,
change_tick.this_run(),
update_index,
);
}
Err(e) => debug!("unable to deserialize update index from {client_id:?}: {e}"),
}
}
}
}
/// Collects [`ReplicationMessages`] and sends them.
pub(super) fn send_replication(
mut entities_with_removals: Local<EntityHashSet>,
mut messages: Local<ReplicationMessages>,
mut replicated_archetypes: Local<ReplicatedArchetypes>,
change_tick: SystemChangeTick,
mut set: ParamSet<(
&World,
ResMut<ConnectedClients>,
ResMut<ClientEntityMap>,
ResMut<DespawnBuffer>,
ResMut<RemovalBuffer>,
ResMut<ClientBuffers>,
ResMut<RepliconServer>,
)>,
registry: Res<ReplicationRegistry>,
rules: Res<ReplicationRules>,
server_tick: Res<ServerTick>,
time: Res<Time>,
) -> bincode::Result<()> {
replicated_archetypes.update(set.p0(), &rules);
let connected_clients = mem::take(&mut *set.p1()); // Take ownership to avoid borrowing issues.
messages.prepare(connected_clients);
collect_mappings(&mut messages, &mut set.p2())?;
collect_despawns(&mut messages, &mut set.p3())?;
collect_removals(&mut messages, &mut set.p4(), &mut entities_with_removals)?;
collect_changes(
&mut messages,
&replicated_archetypes,
®istry,
&entities_with_removals,
set.p0(),
&change_tick,
**server_tick,
)?;
entities_with_removals.clear();
let mut client_buffers = mem::take(&mut *set.p5());
let connected_clients = messages.send(
&mut set.p6(),
&mut client_buffers,
**server_tick,
change_tick.this_run(),
time.elapsed(),
)?;
// Return borrowed data back.
*set.p1() = connected_clients;
*set.p5() = client_buffers;
Ok(())
}
fn reset(
mut server_tick: ResMut<ServerTick>,
mut entity_map: ResMut<ClientEntityMap>,
mut connected_clients: ResMut<ConnectedClients>,
mut client_buffers: ResMut<ClientBuffers>,
) {
*server_tick = Default::default();
entity_map.0.clear();
connected_clients.clear(&mut client_buffers);
}
}
/// Collects and writes any new entity mappings that happened in this tick.
///
/// On deserialization mappings should be processed first, so all referenced entities after it will behave correctly.
fn collect_mappings(
messages: &mut ReplicationMessages,
entity_map: &mut ClientEntityMap,
) -> bincode::Result<()> {
for (message, _, client) in messages.iter_mut_with_clients() {
message.start_array();
if let Some(mappings) = entity_map.0.get_mut(&client.id()) {
for mapping in mappings.drain(..) {
message.write_client_mapping(&mapping)?;
}
}
message.end_array()?;
}
Ok(())
}
/// Collects component insertions from this tick into init messages, and changes into update messages
/// since the last entity tick.
fn collect_changes(
messages: &mut ReplicationMessages,
replicated_archetypes: &ReplicatedArchetypes,
registry: &ReplicationRegistry,
entities_with_removals: &EntityHashSet,
world: &World,
change_tick: &SystemChangeTick,
server_tick: RepliconTick,
) -> bincode::Result<()> {
for (init_message, _) in messages.iter_mut() {
init_message.start_array();
}
for replicated_archetype in replicated_archetypes.iter() {
// SAFETY: all IDs from replicated archetypes obtained from real archetypes.
let archetype = unsafe {
world
.archetypes()
.get(replicated_archetype.id)
.unwrap_unchecked()
};
// SAFETY: table obtained from this archetype.
let table = unsafe {
world
.storages()
.tables
.get(archetype.table_id())
.unwrap_unchecked()
};
for entity in archetype.entities() {
for (init_message, update_message, client) in messages.iter_mut_with_clients() {
init_message.start_entity_data(entity.id());
update_message.start_entity_data(entity.id());
client.visibility_mut().cache_visibility(entity.id());
}
// SAFETY: all replicated archetypes have marker component with table storage.
let (_, marker_ticks) = unsafe {
get_component_unchecked(
table,
&world.storages().sparse_sets,
entity,
StorageType::Table,
replicated_archetypes.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 {
// SAFETY: component and storage were obtained from this archetype.
let (component, ticks) = unsafe {
get_component_unchecked(
table,
&world.storages().sparse_sets,
entity,
replicated_component.storage_type,
replicated_component.component_id,
)
};
let (component_fns, rule_fns) = registry.get(replicated_component.fns_id);
let ctx = SerializeCtx { server_tick };
let mut shared_bytes = None;
for (init_message, update_message, client) in messages.iter_mut_with_clients() {
let visibility = client.visibility().cached_visibility();
if visibility == Visibility::Hidden {
continue;
}
if let Some(tick) = client
.get_change_tick(entity.id())
.filter(|_| !marker_added)
.filter(|_| visibility != Visibility::Gained)
.filter(|_| !ticks.is_added(change_tick.last_run(), change_tick.this_run()))
{
if ticks.is_changed(tick, change_tick.this_run()) {
update_message.write_component(
&mut shared_bytes,
rule_fns,
component_fns,
&ctx,
replicated_component.fns_id,
component,
)?;
}
} else {
init_message.write_component(
&mut shared_bytes,
rule_fns,
component_fns,
&ctx,
replicated_component.fns_id,
component,
)?;
}
}
}
for (init_message, update_message, client) in messages.iter_mut_with_clients() {
let visibility = client.visibility().cached_visibility();
if visibility == Visibility::Hidden {
continue;
}
let new_entity = marker_added || visibility == Visibility::Gained;
if new_entity
|| init_message.entity_data_size() != 0
|| entities_with_removals.contains(&entity.id())
{
// If there is any insertion, removal, or we must initialize, include all updates into init message.
// and bump the last acknowledged tick to keep entity updates atomic.
init_message.take_entity_data(update_message)?;
client.set_change_tick(entity.id(), change_tick.this_run());
} else {
update_message.end_entity_data()?;
}
init_message.end_entity_data(new_entity)?;
}
}
}
for (init_message, _) in messages.iter_mut() {
init_message.end_array()?;
}
Ok(())
}
/// Extracts component in form of [`Ptr`] and its ticks from table or sparse set based on its storage type.
///
/// # Safety
///
/// Component should be present in this archetype and have this storage type.
unsafe fn get_component_unchecked<'w>(
table: &'w Table,
sparse_sets: &'w SparseSets,
entity: &ArchetypeEntity,
storage_type: StorageType,
component_id: ComponentId,
) -> (Ptr<'w>, ComponentTicks) {
match storage_type {
StorageType::Table => {
let column = table.get_column(component_id).unwrap_unchecked();
let component = column.get_data_unchecked(entity.table_row());
let ticks = column.get_ticks_unchecked(entity.table_row());
(component, ticks)
}
StorageType::SparseSet => {
let sparse_set = sparse_sets.get(component_id).unwrap_unchecked();
let component = sparse_set.get(entity.id()).unwrap_unchecked();
let ticks = sparse_set.get_ticks(entity.id()).unwrap_unchecked();
(component, ticks)
}
}
}
/// Collect entity despawns from this tick into init messages.
fn collect_despawns(
messages: &mut ReplicationMessages,
despawn_buffer: &mut DespawnBuffer,
) -> bincode::Result<()> {
for (message, _) in messages.iter_mut() {
message.start_array();
}
for entity in despawn_buffer.drain(..) {
let mut shared_bytes = None;
for (message, _, client) in messages.iter_mut_with_clients() {
client.remove_despawned(entity);
message.write_entity(&mut shared_bytes, entity)?;
}
}
for (message, _, client) in messages.iter_mut_with_clients() {
for entity in client.drain_lost_visibility() {
message.write_entity(&mut None, entity)?;
}
message.end_array()?;
}
Ok(())
}
/// Collects component removals from this tick into init messages.
fn collect_removals(
messages: &mut ReplicationMessages,
removal_buffer: &mut RemovalBuffer,
entities_with_removals: &mut EntityHashSet,
) -> bincode::Result<()> {
for (message, _) in messages.iter_mut() {
message.start_array();
}
for (entity, remove_ids) in removal_buffer.iter() {
for (message, _) in messages.iter_mut() {
message.start_entity_data(entity);
for fns_info in remove_ids {
message.write_fns_id(fns_info.fns_id())?;
}
entities_with_removals.insert(entity);
message.end_entity_data(false)?;
}
}
removal_buffer.clear();
for (message, _) in messages.iter_mut() {
message.end_array()?;
}
Ok(())
}
/// 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 emit [`ServerEvent`].
///
/// The messaging backend should convert its own connection events into [`ServerEvents`](ServerEvent)
/// in this set.
///
/// Runs in [`PreUpdate`].
SendEvents,
/// Systems that receive data from [`RepliconServer`].
///
/// Used by `bevy_replicon`.
///
/// Runs in [`PreUpdate`].
Receive,
/// Systems that store hierarchy changes in [`ParentSync`](super::parent_sync::ParentSync).
///
/// Runs in [`PostUpdate`].
StoreHierarchy,
/// 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 updates are replicated over the unreliable channel, so if a component update packet is lost
/// then component updates 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.
///
/// By default it's 30 ticks per second.
MaxTickRate(u16),
/// The replicon tick is incremented every frame.
EveryFrame,
/// The user should manually configure [`ServerPlugin::increment_tick`] or manually increment
/// [`RepliconTick`].
Manual,
}
/// Connection and disconnection events on the server.
///
/// The messaging backend is responsible for emitting these in [`ServerSet::SendEvents`].
#[derive(Event, Debug, Clone)]
pub enum ServerEvent {
ClientConnected { client_id: ClientId },
ClientDisconnected { client_id: ClientId, reason: String },
}