Example #1
0
        private async Task CollectStatsAsync()
        {
            while (!_statsCancellation.IsCancellationRequested)
            {
                if (_clients.IsEmpty)
                {
                    await Task.Delay(TimeSpan.FromSeconds(15))
                    .ConfigureAwait(false);

                    continue;
                }

                var process = Process.GetCurrentProcess();
                var stats   = new StatisticPacket
                {
                    ConnectedClients = _clients.Count,
                    ConnectedPlayers = _clients.Values
                                       .Sum(x => x.VoiceClients.Values
                                            .Count(v => v.Engine.IsReady)),

                    PlayingPlayers = _clients.Values
                                     .Sum(x => x.VoiceClients.Values
                                          .Count(v => v.Engine.IsPlaying)),

                    Uptime = (int)(DateTimeOffset.UtcNow - process.StartTime.ToUniversalTime()).TotalSeconds
                }.Populate(process);

                var rawString = JsonSerializer.ToString(stats);
                LogHandler <StatisticPacket> .Log.Debug(rawString);

                var sendTasks = _clients
                                .Where(x => !x.Value.IsDisposed)
                                .Select(x => x.Value.SendStatsAsync(stats));

                await Task.WhenAll(sendTasks)
                .ContinueWith(async _ => await Task.Delay(TimeSpan.FromSeconds(30)))
                .ConfigureAwait(false);
            }
        }
Example #2
0
 public async Task SendStatsAsync(StatisticPacket stats)
 {
     await _socket.SendAsync(stats)
     .ConfigureAwait(false);
 }