using LumaTunnel.Client.Interface; using XFEExtension.NetCore.WinUIHelper.Implements.Services; namespace LumaTunnel.Client.Implements; public sealed class TrafficMeterService : GlobalServiceBase, ITrafficMeter { private readonly Queue _history = new(); private long _lastUpload; private long _lastDownload; public TrafficSample Current { get; private set; } = new(DateTimeOffset.UtcNow, 0, 0); public IReadOnlyList History { get { lock (_history) return _history.ToArray(); } } public void Sample() { var connections = AppServices.ProxyEngine.Connections; var upload = connections.Sum(static item => item.UploadBytes); var download = connections.Sum(static item => item.DownloadBytes); Current = new TrafficSample( DateTimeOffset.UtcNow, Math.Max(0, upload - Interlocked.Exchange(ref _lastUpload, upload)), Math.Max(0, download - Interlocked.Exchange(ref _lastDownload, download))); lock (_history) { _history.Enqueue(Current); while (_history.Count > 60) _history.Dequeue(); } } }