Пример #1
0
        private async Task <(long chunks, long bytes)> StreamContentWithCompressionAsync(IAsyncStreamReader <CopyFileResponse> input, Stream output, CopyOptions?options, CancellationToken cancellationToken)
        {
            long chunks = 0L;
            long bytes  = 0L;

            using (var grpcStream = new BufferedReadStream(async() =>
            {
                if (await input.MoveNext(cancellationToken))
                {
                    chunks++;
                    bytes += input.Current.Content.Length;

                    options?.UpdateTotalBytesCopied(bytes);

                    return(input.Current.Content);
                }
                else
                {
                    return(null);
                }
            }))
            {
                using (Stream decompressedStream = new GZipStream(grpcStream, CompressionMode.Decompress, true))
                {
                    await decompressedStream.CopyToAsync(output, _configuration.ClientBufferSizeBytes, cancellationToken);
                }
            }

            return(chunks, bytes);
        }
Пример #2
0
        private static async Task <CopyFileResult> CopyRandomToStreamAtSpeed(CancellationToken token, Stream stream, long totalBytes, double mbPerSec, CopyOptions 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());
        }
Пример #3
0
        private async Task <BoolResult> StreamContentAsync(IAsyncStreamReader <CopyFileResponse> input, Stream output, CopyOptions?options, CancellationToken cancellationToken)
        {
            try
            {
                await GrpcExtensions.CopyChunksToStreamAsync(
                    input,
                    output,
                    response =>
                {
                    if (response.Header != null)
                    {
                        var error = new ErrorResult(response.Header.ErrorMessage ?? "Unknown error", response.Header.Diagnostics);
                        error.ThrowIfFailure();
                    }
                    return(response.Content);
                },
                    totalBytes => options?.UpdateTotalBytesCopied(totalBytes),
                    cancellationToken);

                return(BoolResult.Success);
            }
            catch (ResultPropagationException e)
            {
                return(new BoolResult(e.Result));
            }
        }
Пример #4
0
 private Task <(long Chunks, long Bytes)> StreamContentAsync(IAsyncStreamReader <CopyFileResponse> input, Stream output, CopyOptions?options, CancellationToken cancellationToken)
 {
     return(GrpcExtensions.CopyChunksToStreamAsync(
                input,
                output,
                response => response.Content,
                totalBytes => options?.UpdateTotalBytesCopied(totalBytes),
                cancellationToken));
 }
Пример #5
0
 private Task <(long Chunks, long Bytes)> StreamContentAsync(Stream input, byte[] primaryBuffer, byte[] secondaryBuffer, IClientStreamWriter <PushFileRequest> requestStream, CopyOptions options, CancellationToken cancellationToken)
 {
     return(GrpcExtensions.CopyStreamToChunksAsync(
                input,
                requestStream,
                (content, _) => new PushFileRequest()
     {
         Content = content
     },
                primaryBuffer,
                secondaryBuffer,
                progressReport: (totalBytesRead) => options?.UpdateTotalBytesCopied(totalBytesRead),
                cancellationToken));
 }
Пример #6
0
        private async Task <BoolResult> StreamContentWithCompressionAsync(IAsyncStreamReader <CopyFileResponse> input, Stream output, CopyOptions?options, CancellationToken cancellationToken)
        {
            try
            {
                long chunks = 0L;
                long bytes  = 0L;

                using (var grpcStream = new BufferedReadStream(
                           async() =>
                {
                    if (await input.MoveNext(cancellationToken))
                    {
                        if (input.Current.Header is { } header)
                        {
                            var error = new ErrorResult(header.ErrorMessage ?? "Unknown error", header.Diagnostics);
                            error.ThrowIfFailure();
                        }

                        chunks++;
                        bytes += input.Current.Content.Length;

                        options?.UpdateTotalBytesCopied(bytes);

                        return(input.Current.Content);
                    }
                    else
                    {
                        return(null);
                    }
                }))
                {
                    using (Stream decompressedStream = new GZipStream(grpcStream, CompressionMode.Decompress, true))
                    {
                        await decompressedStream.CopyToAsync(output, _configuration.ClientBufferSizeBytes, cancellationToken);
                    }
                }

                return(BoolResult.Success);
            }
            catch (ResultPropagationException e)
            {
                return(new BoolResult(e.Result));
            }
        }