private static async Task <long> RunClientAsync(ClientOptions options, RemoteCertificateValidationCallback rcvc, TcpClient[] clients) { try { var streams = new Stream[clients.Length]; for (var i = 0; i < streams.Length; i++) { var stream = new SslStream(clients[i].GetStream(), leaveInnerStreamOpen: false, userCertificateValidationCallback: rcvc); await stream.AuthenticateAsClientAsync(options.Host); streams[i] = stream; } using (var multiStream = new MultiStream(streams, options.MultiStreamBlockLength)) { var buffer = new byte[options.BufferLength]; var bytesRead = -1; var totalBytesRead = (long)0; if (options.Sync) { while ((bytesRead = multiStream.Read(buffer, 0, buffer.Length)) > 0) { totalBytesRead += bytesRead; } } else { while ((bytesRead = await multiStream.ReadAsync(buffer, 0, buffer.Length)) > 0) { totalBytesRead += bytesRead; } } return(totalBytesRead); } } finally { foreach (var client in clients) { ((IDisposable)client).Dispose(); } } }
private static async Task SendDataAsync(ServerOptions options, X509Certificate2 cert, TcpClient[] clients) { var data = new ZeroStream(options.Megabytes * 1024 * 1024 * options.Connections); try { var streams = new Stream[clients.Length]; for (var i = 0; i < streams.Length; i++) { var stream = new SslStream(clients[i].GetStream()); await stream.AuthenticateAsServerAsync(cert); streams[i] = stream; } using (var multiStream = new MultiStream(streams, options.MultiStreamBlockLength)) { Console.WriteLine(); Console.WriteLine($"Sending {string.Format("{0:n0}", data.Length)} bytes..."); var sw = Stopwatch.StartNew(); if (options.Sync) { data.CopyTo(multiStream, options.BufferLength); } else { await data.CopyToAsync(multiStream, options.BufferLength); } sw.Stop(); var mbps = ((data.Length * 8) / (1024 * 1024)) / sw.Elapsed.TotalSeconds; Console.WriteLine($"Sent {string.Format("{0:n0}", data.Length)} bytes in {Math.Round(sw.Elapsed.TotalSeconds, 3)} seconds ({mbps} Mbps)"); } } finally { foreach (var client in clients) { ((IDisposable)client).Dispose(); } } }