private async Task <(long chunks, long bytes)> StreamContentWithCompressionAsync(Stream targetStream, IAsyncStreamReader <CopyFileResponse> replyStream, CopyToOptions?options, CancellationToken ct) { long chunks = 0L; long bytes = 0L; using (var grpcStream = new BufferedReadStream(async() => { if (await replyStream.MoveNext(ct)) { chunks++; bytes += replyStream.Current.Content.Length; options?.UpdateTotalBytesCopied(bytes); return(replyStream.Current.Content); } else { return(null); } })) { using (Stream decompressedStream = new GZipStream(grpcStream, CompressionMode.Decompress, true)) { await decompressedStream.CopyToAsync(targetStream, _bufferSize, ct); } } return(chunks, bytes); }
private static async Task <CopyFileResult> CopyRandomToStreamAtSpeed(CancellationToken token, Stream stream, long totalBytes, double mbPerSec, CopyToOptions options) { var interval = TimeSpan.FromSeconds(0.1); var copied = 0; var bytesPerInterval = (int)BytesPerInterval(mbPerSec, interval); Assert.True(bytesPerInterval > 0); var buffer = new byte[bytesPerInterval]; while (!token.IsCancellationRequested) { var intervalTask = Task.Delay(interval); Random.NextBytes(buffer); await stream.WriteAsync(buffer, 0, bytesPerInterval); copied += bytesPerInterval; options.UpdateTotalBytesCopied(copied); if (copied >= totalBytes) { break; } await intervalTask; } return(new CopyFileResult()); }
private async Task <(long chunks, long bytes)> StreamContentAsync(Stream targetStream, IAsyncStreamReader <CopyFileResponse> replyStream, CopyToOptions?options, CancellationToken ct) { long chunks = 0L; long bytes = 0L; while (await replyStream.MoveNext(ct)) { chunks++; CopyFileResponse reply = replyStream.Current; bytes += reply.Content.Length; reply.Content.WriteTo(targetStream); options?.UpdateTotalBytesCopied(bytes); } return(chunks, bytes); }