private async Task CloseConnections() { for (var i = 0; i < _ClientChannelConnections.Count(); i += Options.Parallelism) { var clientChannelConnections = _ClientChannelConnections.Skip(i).Take(Options.Parallelism); Console.Error.WriteLine($"Closing {clientChannelConnections.Count()} connections (group #{1 + i / Options.Parallelism})..."); var stopwatch = new ManagedStopwatch(); stopwatch.Start(); await Task.WhenAll( clientChannelConnections.Select(async ccc => { await ccc.Connection?.Close(); ccc.RemoteAudioTrack?.Destroy(); ccc.RemoteVideoTrack?.Destroy(); }) ).ConfigureAwait(false); stopwatch.Stop(); Console.Error.WriteLine($"Closing completed in {stopwatch.ElapsedMilliseconds}ms ({clientChannelConnections.Count() * 1000.0 / stopwatch.ElapsedMilliseconds:n2}/s)."); } }
private async Task OpenConnections(CancellationToken cancellationToken) { _ClientChannelConnections = _ClientChannels.SelectMany(cc => Enumerable.Range(0, Options.ConnectionCount).Select(_ => new ClientChannelConnection { Client = cc.Client, ChannelId = cc.ChannelId, Channel = cc.Channel })).ToArray(); for (var i = 0; i < _ClientChannelConnections.Count(); i += Options.Parallelism) { var clientChannelConnections = _ClientChannelConnections.Skip(i).Take(Options.Parallelism); Console.Error.WriteLine($"Opening {clientChannelConnections.Count()} connections (group #{1 + i / Options.Parallelism})..."); var stopwatch = new ManagedStopwatch(); stopwatch.Start(); var result = await Task.WhenAny( Task.Delay(Timeout.Infinite, cancellationToken), Task.WhenAll( clientChannelConnections.Select(ccc => { ccc.RemoteAudioTrack = new AudioTrack(new NullAudioSink(new Opus.Format { IsPacketized = true })); ccc.RemoteVideoTrack = new VideoTrack(new NullVideoSink(new Vp8.Format { IsPacketized = true })); ccc.Connection = ccc.Channel.CreateSfuDownstreamConnection( Utility.GenerateId(), new AudioStream(null, ccc.RemoteAudioTrack), new VideoStream(null, ccc.RemoteVideoTrack) ); ccc.Connection.PrivateIPAddresses = _PrivateIPAddresses; return(ccc.Connection.Open().AsTaskAsync()); }) ) ).ConfigureAwait(false); stopwatch.Stop(); // check cancellation cancellationToken.ThrowIfCancellationRequested(); // check faulted if (result.IsFaulted) { throw new ConnectionOpenException("One or more connections could not be opened.", result.Exception); } Console.Error.WriteLine($"Opening completed in {stopwatch.ElapsedMilliseconds}ms ({clientChannelConnections.Count() * 1000.0 / stopwatch.ElapsedMilliseconds:n2}/s)."); } }
private async Task JoinChannels(CancellationToken cancellationToken) { var channelIds = Enumerable.Range(0, Options.ChannelCount).Select(_ => Utility.GenerateId()).ToArray(); if (Options.ChannelBurst) { _ClientChannels = channelIds.SelectMany(channelId => _Clients.Select(client => new ClientChannel { Client = client, ChannelId = channelId })).ToArray(); } else { _ClientChannels = _Clients.SelectMany(client => channelIds.Select(channelId => new ClientChannel { Client = client, ChannelId = channelId })).ToArray(); } for (var i = 0; i < _ClientChannels.Count(); i += Options.Parallelism) { var clientChannels = _ClientChannels.Skip(i).Take(Options.Parallelism); Console.Error.WriteLine($"Joining {clientChannels.Count()} channels (group #{1 + i / Options.Parallelism})..."); var stopwatch = new ManagedStopwatch(); stopwatch.Start(); var result = await Task.WhenAny( Task.Delay(Timeout.Infinite, cancellationToken), Task.WhenAll( clientChannels.Select(async cc => { cc.Channel = await cc.Client.Join(Token.GenerateClientJoinToken(cc.Client, new ChannelClaim(cc.ChannelId), Options.SharedSecret)).AsTask(TaskCreationOptions.RunContinuationsAsynchronously); }) ) ).ConfigureAwait(false); stopwatch.Stop(); // check cancellation cancellationToken.ThrowIfCancellationRequested(); // check faulted if (result.IsFaulted) { throw new ChannelJoinException("One or more channels could not be joined.", result.Exception); } Console.Error.WriteLine($"Joining completed in {stopwatch.ElapsedMilliseconds}ms ({clientChannels.Count() * 1000.0 / stopwatch.ElapsedMilliseconds:n2}/s)."); } }
private async Task LeaveChannels() { for (var i = 0; i < _ClientChannels.Count(); i += Options.Parallelism) { var clientChannels = _ClientChannels.Skip(i).Take(Options.Parallelism); Console.Error.WriteLine($"Leaving {clientChannels.Count()} channels (group #{1 + i / Options.Parallelism})..."); var stopwatch = new ManagedStopwatch(); stopwatch.Start(); await Task.WhenAll( clientChannels.Select(cc => { return(cc.Client.Leave(cc.ChannelId).AsTask(TaskCreationOptions.RunContinuationsAsynchronously)); }) ).ConfigureAwait(false); stopwatch.Stop(); Console.Error.WriteLine($"Leaving completed in {stopwatch.ElapsedMilliseconds}ms ({clientChannels.Count() * 1000.0 / stopwatch.ElapsedMilliseconds:n2}/s)."); } }
private async Task RegisterClients(CancellationToken cancellationToken) { _Clients = Enumerable.Range(0, Options.ClientCount).Select(_ => new Client(Options.GatewayUrl, Options.ApplicationId)).ToArray(); for (var i = 0; i < _Clients.Count(); i += Options.Parallelism) { var clients = _Clients.Skip(i).Take(Options.Parallelism); Console.Error.WriteLine($"Registering {clients.Count()} clients (group #{1 + i / Options.Parallelism})..."); var stopwatch = new ManagedStopwatch(); stopwatch.Start(); var result = await Task.WhenAny( Task.Delay(Timeout.Infinite, cancellationToken), Task.WhenAll( clients.Select(client => { return(client.Register(Token.GenerateClientRegisterToken(client, new ChannelClaim[0], Options.SharedSecret)).AsTask(TaskCreationOptions.RunContinuationsAsynchronously)); }) ) ).ConfigureAwait(false); stopwatch.Stop(); // check cancellation cancellationToken.ThrowIfCancellationRequested(); // check faulted if (result.IsFaulted) { throw new ClientRegisterException("One or more clients could not be registered.", result.Exception); } Console.Error.WriteLine($"Registering completed in {stopwatch.ElapsedMilliseconds}ms ({clients.Count() * 1000.0 / stopwatch.ElapsedMilliseconds:n2}/s)."); } }