|
| 1 | +using System.Collections.Concurrent; |
| 2 | + |
| 3 | +using Chaos.Networking.Entities.Server; |
| 4 | + |
| 5 | +namespace Darkages.Network.Client.Coalescer; |
| 6 | + |
| 7 | +public sealed class BodyAnimationCoalescer : IDisposable |
| 8 | +{ |
| 9 | + private readonly Action<BodyAnimationArgs> _sendImmediate; |
| 10 | + private readonly int _windowMs; |
| 11 | + private readonly int _maxPerWindow; |
| 12 | + |
| 13 | + // Latest-wins per SourceId |
| 14 | + private readonly ConcurrentDictionary<uint, BodyAnimationArgs> _pending = new(); |
| 15 | + |
| 16 | + private int _flushScheduled; // 0/1 |
| 17 | + private CancellationTokenSource? _cts; |
| 18 | + |
| 19 | + public BodyAnimationCoalescer(Action<BodyAnimationArgs>? sendImmediate, int windowMs = 50, int maxPerWindow = 32) |
| 20 | + { |
| 21 | + // Default no-op |
| 22 | + sendImmediate ??= static _ => { }; |
| 23 | + |
| 24 | + _sendImmediate = sendImmediate; |
| 25 | + _windowMs = windowMs <= 0 ? 30 : windowMs; |
| 26 | + _maxPerWindow = maxPerWindow <= 0 ? 64 : maxPerWindow; |
| 27 | + } |
| 28 | + |
| 29 | + public void Enqueue(BodyAnimationArgs args) |
| 30 | + { |
| 31 | + // Dedupe: if the newest is identical to what we already have pending for this SourceId, kick out |
| 32 | + if (_pending.TryGetValue(args.SourceId, out var existing) && AreEquivalent(existing, args)) |
| 33 | + return; |
| 34 | + |
| 35 | + // Latest wins for this SourceId |
| 36 | + _pending[args.SourceId] = args; |
| 37 | + |
| 38 | + // Schedule one flush task per window |
| 39 | + if (Interlocked.Exchange(ref _flushScheduled, 1) == 0) |
| 40 | + { |
| 41 | + _cts ??= new CancellationTokenSource(); |
| 42 | + _ = FlushAfterDelayAsync(_cts.Token); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static bool AreEquivalent(BodyAnimationArgs a, BodyAnimationArgs b) |
| 47 | + => a.BodyAnimation == b.BodyAnimation |
| 48 | + && a.AnimationSpeed == b.AnimationSpeed |
| 49 | + && a.Sound == b.Sound; |
| 50 | + |
| 51 | + private async Task FlushAfterDelayAsync(CancellationToken ct) |
| 52 | + { |
| 53 | + try |
| 54 | + { |
| 55 | + await Task.Delay(_windowMs, ct).ConfigureAwait(false); |
| 56 | + |
| 57 | + // Send at most _maxPerWindow unique SourceIds this tick |
| 58 | + var sent = 0; |
| 59 | + |
| 60 | + foreach (var kv in _pending) |
| 61 | + { |
| 62 | + if (sent >= _maxPerWindow) |
| 63 | + break; |
| 64 | + |
| 65 | + if (_pending.TryRemove(kv.Key, out var args)) |
| 66 | + { |
| 67 | + _sendImmediate(args); |
| 68 | + sent++; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Hard cap protection: drop any remaining this window |
| 73 | + if (!_pending.IsEmpty) |
| 74 | + _pending.Clear(); |
| 75 | + } |
| 76 | + catch (OperationCanceledException) { } |
| 77 | + finally |
| 78 | + { |
| 79 | + // Allow another window to schedule |
| 80 | + Interlocked.Exchange(ref _flushScheduled, 0); |
| 81 | + |
| 82 | + // If more arrived after finished, schedule again |
| 83 | + if (!_pending.IsEmpty && Interlocked.Exchange(ref _flushScheduled, 1) == 0) |
| 84 | + { |
| 85 | + _ = FlushAfterDelayAsync(ct); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void Dispose() |
| 91 | + { |
| 92 | + _cts?.Cancel(); |
| 93 | + _cts?.Dispose(); |
| 94 | + _cts = null; |
| 95 | + _pending.Clear(); |
| 96 | + } |
| 97 | +} |
0 commit comments