forked from Rollocraft/CS2MultiplayerMod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldResyncSystem.cs
More file actions
593 lines (538 loc) · 23.8 KB
/
Copy pathWorldResyncSystem.cs
File metadata and controls
593 lines (538 loc) · 23.8 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using CS2MultiplayerMod.Core.Diagnostics;
using CS2MultiplayerMod.Core.Networking;
using CS2MultiplayerMod.Core.Protocol.Messages;
using CS2MultiplayerMod.Core.Session;
using CS2MultiplayerMod.Game.Diagnostics;
using CS2MultiplayerMod.Game.Sync.Infrastructure;
using CS2MultiplayerMod.Game.Sync.Systems.Net;
using Game;
namespace CS2MultiplayerMod.Game.Sync.Systems
{
/// <summary>
/// Runs world replacement as a distributed transaction:
/// Begin -> every client quiesced -> local native work drained -> save -> epoch-tagged map ->
/// every client loaded -> Resume. Requests are coalesced and a second save can never overtake
/// an active map stream.
/// </summary>
public partial class WorldResyncSystem : GameSystemBase
{
private const long QuiesceTimeoutMs = 20000;
private const long LoadTimeoutMs = 180000;
private const int RequiredCleanFrames = 2;
private enum RecoveryState : byte
{
Idle,
WaitingForQuiescence,
Saving,
WaitingForLoaded,
}
private struct ControlEvent
{
public WorldSyncStage Stage;
public long Epoch;
public ConnectionId Connection;
}
private struct RecoveryRequest
{
public ConnectionId Connection;
public bool IsJoin;
}
private readonly ConcurrentQueue<RecoveryRequest> _requests =
new ConcurrentQueue<RecoveryRequest>();
private readonly ConcurrentQueue<ControlEvent> _controls =
new ConcurrentQueue<ControlEvent>();
private readonly ConcurrentQueue<ConnectionId> _leaves =
new ConcurrentQueue<ConnectionId>();
private readonly List<ConnectionId> _participants = new List<ConnectionId>();
private readonly HashSet<int> _quiesced = new HashSet<int>();
private readonly HashSet<int> _loaded = new HashSet<int>();
private readonly HashSet<int> _pendingJoinRequests = new HashSet<int>();
private readonly List<ConnectionId> _joiningParticipants = new List<ConnectionId>();
private readonly List<ConnectionId> _snapshotTargets = new List<ConnectionId>();
private NetSyncSystem _netSync;
private Observer _observer;
private Task<BlobSource> _saveTask;
private CancellationTokenSource _saveCancellation;
private RecoveryState _state;
private bool _recoveryRequested;
private bool _rerunRequested;
private bool _fullSnapshotRequested;
private long _epochCounter;
private long _epoch;
private long _deadlineMs;
private long _saveStartMs;
private long _lastTransferProgress;
private float _resumeSpeed;
private int _cleanFrames;
protected override void OnCreate()
{
base.OnCreate();
_netSync = World.GetOrCreateSystemManaged<NetSyncSystem>();
_observer = SyncObserverBinding.Bind(
() => new Observer(_requests, _controls, _leaves));
}
protected override void OnDestroy()
{
CancelSave();
SyncObserverBinding.Unbind(_observer);
MultiplayerService service = Mod.Service;
if (_state != RecoveryState.Idle && service != null &&
service.Session.Role == SessionRole.Host)
AbortEpoch(service, "world-sync system was destroyed");
base.OnDestroy();
}
protected override void OnUpdate()
{
using (Diagnostics.SyncProfiler.Measure("WorldResync"))
{
MultiplayerService service = Mod.Service;
if (service == null) return;
MultiplayerSession session = service.Session;
if (session.Role != SessionRole.Host || session.Status != SessionStatus.Connected)
{
ResetInactiveState();
return;
}
long now = service.NowMs;
DrainObserverEvents(session);
if (_state == RecoveryState.Idle)
{
if (_recoveryRequested) StartEpoch(service, session, now);
return;
}
PruneDisconnectedParticipants(session);
if (_participants.Count == 0)
{
AbortEpoch(service, "all snapshot participants disconnected");
return;
}
switch (_state)
{
case RecoveryState.WaitingForQuiescence:
PumpQuiescence(service, session, now);
break;
case RecoveryState.Saving:
PumpSave(service, session, now);
break;
case RecoveryState.WaitingForLoaded:
PumpLoaded(service, session, now);
break;
}
}
}
/// <summary>
/// This system survives session restarts inside the same loaded city. Discard
/// observer events and join labels from the previous role/session so a later
/// host never attributes its first refresh to an old connection id.
/// </summary>
private void ResetInactiveState()
{
RecoveryRequest request;
while (_requests.TryDequeue(out request)) { }
ControlEvent control;
while (_controls.TryDequeue(out control)) { }
ConnectionId left;
while (_leaves.TryDequeue(out left)) { }
_state = RecoveryState.Idle;
CancelSave();
_participants.Clear();
_quiesced.Clear();
_loaded.Clear();
_pendingJoinRequests.Clear();
_joiningParticipants.Clear();
_recoveryRequested = false;
_rerunRequested = false;
_fullSnapshotRequested = false;
_snapshotTargets.Clear();
_epoch = 0;
_deadlineMs = 0;
_cleanFrames = 0;
}
private void DrainObserverEvents(MultiplayerSession session)
{
RecoveryRequest request;
while (_requests.TryDequeue(out request))
{
if (request.IsJoin && !request.Connection.IsNone)
_pendingJoinRequests.Add(request.Connection.Value);
else if (!request.IsJoin)
_fullSnapshotRequested = true;
if (_state == RecoveryState.Idle) _recoveryRequested = true;
else _rerunRequested = true;
}
ConnectionId left;
while (_leaves.TryDequeue(out left))
{
_pendingJoinRequests.Remove(left.Value);
RemoveParticipant(left);
}
ControlEvent evt;
while (_controls.TryDequeue(out evt))
{
if (_state == RecoveryState.Idle || evt.Epoch != _epoch ||
!ContainsParticipant(evt.Connection))
continue;
if (evt.Stage == WorldSyncStage.Quiesced &&
_state == RecoveryState.WaitingForQuiescence)
_quiesced.Add(evt.Connection.Value);
else if (evt.Stage == WorldSyncStage.Loaded &&
_state == RecoveryState.WaitingForLoaded)
_loaded.Add(evt.Connection.Value);
else if (evt.Stage == WorldSyncStage.Failed &&
_state == RecoveryState.WaitingForLoaded)
{
SyncLog.Error(LogTopic.Resync, DescribePeer(session, evt.Connection) +
" could not install world-sync epoch " + _epoch +
"; disconnecting it rather than resuming divergent worlds.");
session.DisconnectPeer(evt.Connection);
RemoveParticipant(evt.Connection);
}
}
}
private void StartEpoch(MultiplayerService service, MultiplayerSession session, long now)
{
if (_saveTask != null)
{
if (!_saveTask.IsCompleted) return;
ObserveFinishedSave();
}
_recoveryRequested = false;
_participants.Clear();
foreach (Peer peer in session.Peers)
if (peer.Handshaked) _participants.Add(peer.Connection);
if (_participants.Count == 0) return;
_joiningParticipants.Clear();
for (int i = 0; i < _participants.Count; i++)
if (_pendingJoinRequests.Contains(_participants[i].Value))
_joiningParticipants.Add(_participants[i]);
service.PrepareHostWorldSyncUi(_joiningParticipants);
// A join only owes the world to whoever joined; everyone else is already holding it
// and just crosses the barrier. Divergence-driven and player-requested epochs
// re-baseline every peer, which is the whole point of them.
_snapshotTargets.Clear();
if (_fullSnapshotRequested || _joiningParticipants.Count == 0)
_snapshotTargets.AddRange(_participants);
else
_snapshotTargets.AddRange(_joiningParticipants);
_fullSnapshotRequested = false;
_epoch = ++_epochCounter;
if (!service.TryBeginHostWorldSync(_epoch, out _resumeSpeed))
{
SyncLog.Error(LogTopic.Resync, "Could not enter the local world-sync barrier.");
ResetEpoch();
return;
}
if (!session.BeginWorldSync(_epoch, _resumeSpeed, _participants, _snapshotTargets))
{
service.AbortHostWorldSync(_epoch, _resumeSpeed);
SyncLog.Error(LogTopic.Resync, "Could not open world-sync epoch " + _epoch + ".");
ResetEpoch();
return;
}
for (int i = 0; i < _joiningParticipants.Count; i++)
_pendingJoinRequests.Remove(_joiningParticipants[i].Value);
_quiesced.Clear();
_loaded.Clear();
_cleanFrames = 0;
_deadlineMs = now + QuiesceTimeoutMs;
_state = RecoveryState.WaitingForQuiescence;
SyncLog.Event(LogTopic.Resync, "World sync epoch " + _epoch + " waiting for " +
_participants.Count + " client quiescence acknowledgement(s).");
}
private void PumpQuiescence(MultiplayerService service, MultiplayerSession session, long now)
{
if (now > _deadlineMs)
{
DisconnectMissing(session, _quiesced, "quiescence acknowledgement");
if (_participants.Count == 0)
{
AbortEpoch(service, "no client crossed the quiescence barrier");
return;
}
if (AllParticipantsIn(_quiesced) && _netSync != null &&
!_netSync.IsRecoveryQuiescent)
{
AbortEpoch(service, "local native transaction did not quiesce safely");
return;
}
}
if (!AllParticipantsIn(_quiesced) ||
(_netSync != null && !_netSync.IsRecoveryQuiescent))
{
_cleanFrames = 0;
return;
}
// Two complete UI frames after every acknowledgement/native Temp drain close races
// with a tool apply that was already scheduled when Begin arrived.
if (++_cleanFrames < RequiredCleanFrames) return;
StartSave(service, now);
}
private void StartSave(MultiplayerService service, long now)
{
try
{
if (_saveTask != null && !_saveTask.IsCompleted) return;
ObserveFinishedSave();
_saveCancellation = new CancellationTokenSource();
_saveTask = service.CreateWorldSnapshot(World, _epoch, _saveCancellation.Token);
long savingEpoch = _epoch;
CancellationToken saveToken = _saveCancellation.Token;
_saveTask.ContinueWith(task =>
{
if (task.IsFaulted)
SyncLog.Error(LogTopic.Resync, "World snapshot epoch " + savingEpoch +
" failed: " + task.Exception.GetBaseException().Message);
else if (task.Status == TaskStatus.RanToCompletion && saveToken.IsCancellationRequested)
task.Result?.Dispose();
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
_saveStartMs = now;
_state = RecoveryState.Saving;
service.SetHostWorldSyncUiStage(HostWorldSyncUiStage.Saving);
SyncLog.Event(LogTopic.Resync, "World sync epoch " + _epoch +
": barrier closed; saving the authoritative world.");
}
catch (Exception ex)
{
AbortEpoch(service, "could not start authoritative save: " + ex.Message);
}
}
private void PumpSave(MultiplayerService service, MultiplayerSession session, long now)
{
if (_saveTask != null && !_saveTask.IsCompleted) return;
if (_saveTask == null || _saveTask.IsFaulted || _saveTask.IsCanceled)
{
string detail = _saveTask != null && _saveTask.Exception != null
? _saveTask.Exception.GetBaseException().Message
: (_saveTask != null && _saveTask.IsCanceled ? "save was canceled" : "save task disappeared");
AbortEpoch(service, "authoritative save failed: " + detail);
return;
}
BlobSource snapshot = _saveTask.Result;
_saveTask = null;
ObserveFinishedSave();
if (snapshot == null || snapshot.Length == 0)
{
AbortEpoch(service, "authoritative save produced no snapshot data");
return;
}
string saveName = MultiplayerService.WorldSnapshotFileName;
try
{
for (int i = 0; i < _snapshotTargets.Count; i++)
service.StreamWorldSnapshot(_snapshotTargets[i], _epoch, snapshot, saveName);
}
catch (Exception ex)
{
AbortEpoch(service, "could not stream snapshot: " + ex.Message);
return;
}
finally { snapshot.Dispose(); }
_loaded.Clear();
// Barrier-only participants install nothing, so they never acknowledge a load. They
// are already where the snapshot would have put them.
for (int i = 0; i < _participants.Count; i++)
if (!_snapshotTargets.Contains(_participants[i]))
_loaded.Add(_participants[i].Value);
_lastTransferProgress = 0;
_deadlineMs = now + LoadTimeoutMs;
_state = RecoveryState.WaitingForLoaded;
service.SetHostWorldSyncUiStage(HostWorldSyncUiStage.WaitingForLoaded);
SyncLog.Event(LogTopic.Resync, "World sync epoch " + _epoch + ": queued one " +
(snapshot.Length / 1024) + " KB snapshot for " + _snapshotTargets.Count +
" of " + _participants.Count +
" participant(s); waiting for load acknowledgement(s). Save took " +
(now - _saveStartMs) + " ms.");
}
private void PumpLoaded(MultiplayerService service, MultiplayerSession session, long now)
{
if (session.OutgoingBlobSent > _lastTransferProgress)
{
_lastTransferProgress = session.OutgoingBlobSent;
_deadlineMs = now + LoadTimeoutMs;
}
if (AllParticipantsIn(_loaded))
{
CompleteEpoch(service, session, now);
return;
}
if (now <= _deadlineMs) return;
DisconnectMissing(session, _loaded, "snapshot load acknowledgement");
if (_participants.Count == 0)
AbortEpoch(service, "no client installed the authoritative snapshot");
else
CompleteEpoch(service, session, now);
}
private void CompleteEpoch(MultiplayerService service, MultiplayerSession session, long now)
{
var targets = new List<ConnectionId>(_participants);
// A peer that asked for a world mid-epoch is not a new participant, so it would
// otherwise be left waiting: this epoch may only have streamed to whoever joined.
bool needsRerun = _rerunRequested &&
(_fullSnapshotRequested || HasNewParticipant(session, targets));
// Resume is queued first. Session command sends are reopened only afterward, preserving
// Resume-before-command order on every TCP connection.
session.ResumeWorldSync(_epoch, _resumeSpeed, targets);
service.CompleteHostWorldSync(_epoch, _resumeSpeed);
SyncLog.Event(LogTopic.Resync, "World sync epoch " + _epoch + " completed for " +
targets.Count + " participant(s).");
ResetEpoch();
// A peer that joined after this snapshot was queued needs another snapshot. Open the
// next Begin immediately after Resume in the same update, leaving no gameplay frame
// between epochs and never overlapping blob streams.
_rerunRequested = false;
if (needsRerun)
{
_recoveryRequested = true;
StartEpoch(service, session, now);
}
}
private void AbortEpoch(MultiplayerService service, string reason)
{
MultiplayerSession session = service.Session;
var targets = new List<ConnectionId>(_participants);
if (_epoch > 0)
{
session.AbortWorldSync(_epoch, _resumeSpeed, targets);
service.AbortHostWorldSync(_epoch, _resumeSpeed);
}
SyncLog.Error(LogTopic.Resync, "World sync epoch " + _epoch + " aborted: " + reason +
".");
ResetEpoch();
}
private void CancelSave()
{
_saveCancellation?.Cancel();
if (_saveTask == null || _saveTask.IsCompleted) ObserveFinishedSave();
}
private void ObserveFinishedSave()
{
if (_saveTask != null && !_saveTask.IsCompleted) return;
if (_saveTask != null && _saveTask.IsFaulted) _ = _saveTask.Exception;
if (_saveTask != null && _saveTask.Status == TaskStatus.RanToCompletion)
_saveTask.Result?.Dispose();
_saveTask = null;
_saveCancellation?.Dispose();
_saveCancellation = null;
}
private void ResetEpoch()
{
_state = RecoveryState.Idle;
CancelSave();
_participants.Clear();
_quiesced.Clear();
_loaded.Clear();
_joiningParticipants.Clear();
_snapshotTargets.Clear();
_epoch = 0;
_deadlineMs = 0;
_cleanFrames = 0;
}
private void DisconnectMissing(MultiplayerSession session, HashSet<int> acknowledgements,
string expected)
{
for (int i = _participants.Count - 1; i >= 0; i--)
{
ConnectionId connection = _participants[i];
if (acknowledgements.Contains(connection.Value)) continue;
SyncLog.Error(LogTopic.Resync, DescribePeer(session, connection) +
" timed out waiting for " + expected + " in epoch " + _epoch +
"; disconnecting it.");
session.DisconnectPeer(connection);
RemoveParticipant(connection);
}
}
private void PruneDisconnectedParticipants(MultiplayerSession session)
{
for (int i = _participants.Count - 1; i >= 0; i--)
if (!IsConnected(session, _participants[i])) RemoveParticipant(_participants[i]);
}
private static bool IsConnected(MultiplayerSession session, ConnectionId connection)
{
foreach (Peer peer in session.Peers)
if (peer.Connection == connection && peer.Handshaked) return true;
return false;
}
private static string DescribePeer(MultiplayerSession session, ConnectionId connection)
{
foreach (Peer peer in session.Peers)
if (peer.Connection == connection) return peer.ToString();
return connection.ToString();
}
private void RemoveParticipant(ConnectionId connection)
{
_participants.Remove(connection);
_snapshotTargets.Remove(connection);
_quiesced.Remove(connection.Value);
_loaded.Remove(connection.Value);
}
private bool ContainsParticipant(ConnectionId connection) =>
_participants.Contains(connection);
private bool AllParticipantsIn(HashSet<int> set)
{
if (_participants.Count == 0) return false;
for (int i = 0; i < _participants.Count; i++)
if (!set.Contains(_participants[i].Value)) return false;
return true;
}
private static bool HasNewParticipant(MultiplayerSession session,
List<ConnectionId> completedParticipants)
{
foreach (Peer peer in session.Peers)
if (peer.Handshaked && !completedParticipants.Contains(peer.Connection)) return true;
return false;
}
private sealed class Observer : SessionObserver
{
private readonly ConcurrentQueue<RecoveryRequest> _requests;
private readonly ConcurrentQueue<ControlEvent> _controls;
private readonly ConcurrentQueue<ConnectionId> _leaves;
public Observer(ConcurrentQueue<RecoveryRequest> requests,
ConcurrentQueue<ControlEvent> controls, ConcurrentQueue<ConnectionId> leaves)
{
_requests = requests;
_controls = controls;
_leaves = leaves;
}
public override void OnPeerJoined(Peer peer)
{
_requests.Enqueue(new RecoveryRequest
{
Connection = peer.Connection,
IsJoin = true,
});
SyncLog.Event(LogTopic.Resync, "Queued atomic initial world sync for " + peer +
".");
}
public override void OnPeerLeft(Peer peer, string reason) =>
_leaves.Enqueue(peer.Connection);
public override void OnResyncRequested(int playerId, ConnectionId connection)
{
_requests.Enqueue(new RecoveryRequest
{
Connection = connection,
IsJoin = false,
});
SyncLog.Event(LogTopic.Resync, "Queued atomic world-sync for peer #" +
playerId + ".");
}
public override void OnWorldSyncControl(WorldSyncStage stage, long epoch,
float resumeSpeed, ConnectionId connection)
{
if (stage != WorldSyncStage.Quiesced && stage != WorldSyncStage.Loaded &&
stage != WorldSyncStage.Failed)
return;
_controls.Enqueue(new ControlEvent
{
Stage = stage,
Epoch = epoch,
Connection = connection,
});
}
}
}
}