示例#1
0
        // It's quite easy to stream without compression using the same
        // logic as with compression; just CopyToAsync(grpcStream) directly.
        // But that adds an extra buffer (inside the CopyToAsync method) and
        // a corresponding extra copy. So we keep the direct stream without
        // compression logic seperate.
        private async Task <(long, long)> StreamContent(Stream reader, byte[] buffer, IServerStreamWriter <CopyReply> responseStream)
        {
            Debug.Assert(!(reader is null));
            Debug.Assert(!(responseStream is null));

            long bytes  = 0L;
            long chunks = 0;

            while (true)
            {
                int chunkSize = await reader.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

                if (chunkSize == 0)
                {
                    break;
                }

                ByteString content = ByteString.CopyFrom(buffer, 0, chunkSize);
                CopyReply  reply   = new CopyReply()
                {
                    Content = content, Index = chunks
                };
                await responseStream.WriteAsync(reply).ConfigureAwait(false);

                bytes += chunkSize;
                chunks++;
            }

            return(chunks, bytes);
        }
示例#2
0
        private async Task <(long, long)> StreamContentWithCompression(Stream reader, byte[] buffer, IServerStreamWriter <CopyReply> responseStream)
        {
            Debug.Assert(!(reader is null));
            Debug.Assert(!(responseStream is null));

            long bytes  = 0L;
            long chunks = 0L;

            using (Stream grpcStream = new BufferedWriteStream(
                       buffer,
                       async(byte[] bf, int offset, int count) =>
            {
                ByteString content = ByteString.CopyFrom(bf, offset, count);
                CopyReply reply = new CopyReply()
                {
                    Content = content, Index = chunks
                };
                await responseStream.WriteAsync(reply);
                bytes += count;
                chunks++;
            }
                       ))

            {
                using (Stream compressionStream = new GZipStream(grpcStream, System.IO.Compression.CompressionLevel.Fastest, true))
                {
                    await reader.CopyToAsync(compressionStream, buffer.Length).ConfigureAwait(false);

                    await compressionStream.FlushAsync().ConfigureAwait(false);
                }
                await grpcStream.FlushAsync().ConfigureAwait(false);
            }

            return(chunks, bytes);
        }
示例#3
0
 private async Task CopyContent(string file, IServerStreamWriter <CopyReply> responseStream)
 {
     byte[]     buffer  = File.ReadAllBytes(file);
     ByteString content = ByteString.CopyFrom(buffer, 0, buffer.Length);
     CopyReply  reply   = new CopyReply()
     {
         Content = content, Index = 0
     };
     await responseStream.WriteAsync(reply).ConfigureAwait(false);
 }
示例#4
0
        private async Task <(long, long)> StreamContent(Stream fileStream, IAsyncStreamReader <CopyReply> replyStream)
        {
            long chunks = 0L;
            long bytes  = 0L;

            while (await replyStream.MoveNext(CancellationToken.None).ConfigureAwait(false))
            {
                chunks++;
                CopyReply oneOfManyReply = replyStream.Current;
                bytes += oneOfManyReply.Content.Length;
                oneOfManyReply.Content.WriteTo(fileStream);
            }
            return(chunks, bytes);
        }