| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.IO.Compression; |
| | | 5 | | using System.IO.Pipelines; |
| | | 6 | | |
| | | 7 | | namespace IceRpc.Compressor; |
| | | 8 | | |
| | | 9 | | /// <summary>A payload writer decorator that compresses the data written to it. Unlike the pipe writer created by |
| | | 10 | | /// <see cref="PipeWriter.Create(Stream, StreamPipeWriterOptions?)" /> over a compression stream, this decorator |
| | | 11 | | /// implements the completion semantics of payload writers: completing it with an exception completes the decoratee |
| | | 12 | | /// with this exception (and not gracefully), and completing it without an exception never throws — when the writing of |
| | | 13 | | /// the last compressed bytes fails, the decoratee is completed with the exception.</summary> |
| | | 14 | | internal class CompressorPipeWriter : PipeWriter |
| | | 15 | | { |
| | 0 | 16 | | public override bool CanGetUnflushedBytes => _compressedDataWriter.CanGetUnflushedBytes; |
| | | 17 | | |
| | 0 | 18 | | public override long UnflushedBytes => _compressedDataWriter.UnflushedBytes; |
| | | 19 | | |
| | | 20 | | private readonly PipeWriter _compressedDataWriter; |
| | | 21 | | private readonly PipeWriter _decoratee; |
| | | 22 | | private bool _isCompleted; |
| | | 23 | | |
| | 0 | 24 | | public override void Advance(int bytes) => _compressedDataWriter.Advance(bytes); |
| | | 25 | | |
| | 0 | 26 | | public override void CancelPendingFlush() => _compressedDataWriter.CancelPendingFlush(); |
| | | 27 | | |
| | | 28 | | public override void Complete(Exception? exception = null) |
| | 12 | 29 | | { |
| | 12 | 30 | | if (!_isCompleted) |
| | 12 | 31 | | { |
| | 12 | 32 | | if (exception is null && _compressedDataWriter.UnflushedBytes > 0) |
| | 0 | 33 | | { |
| | 0 | 34 | | throw new InvalidOperationException( |
| | 0 | 35 | | $"Completing a {nameof(CompressorPipeWriter)} without an exception is not allowed when this pipe wri |
| | | 36 | | } |
| | | 37 | | |
| | 12 | 38 | | _isCompleted = true; |
| | | 39 | | |
| | 12 | 40 | | if (exception is null) |
| | 10 | 41 | | { |
| | | 42 | | try |
| | 10 | 43 | | { |
| | | 44 | | // Writes the compression trailer into the decoratee. |
| | 10 | 45 | | _compressedDataWriter.Complete(); |
| | 8 | 46 | | } |
| | 2 | 47 | | catch (Exception completeException) |
| | 2 | 48 | | { |
| | 2 | 49 | | exception = completeException; |
| | 2 | 50 | | } |
| | | 51 | | |
| | 10 | 52 | | _decoratee.Complete(exception); |
| | 10 | 53 | | } |
| | | 54 | | else |
| | 2 | 55 | | { |
| | | 56 | | // Complete the decoratee first: this prevents the disposal of the compression stream below from |
| | | 57 | | // writing the compression trailer to the transport — such a write could block on flow control with no |
| | | 58 | | // cancellation. The trailer writes fail immediately on the completed decoratee. |
| | 2 | 59 | | _decoratee.Complete(exception); |
| | | 60 | | try |
| | 2 | 61 | | { |
| | | 62 | | // Releases the compression stream. |
| | 2 | 63 | | _compressedDataWriter.Complete(exception); |
| | 0 | 64 | | } |
| | 2 | 65 | | catch |
| | 2 | 66 | | { |
| | | 67 | | // Expected: see comment above. |
| | 2 | 68 | | } |
| | 2 | 69 | | } |
| | 12 | 70 | | } |
| | 12 | 71 | | } |
| | | 72 | | |
| | | 73 | | public override ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default) => |
| | 0 | 74 | | _compressedDataWriter.FlushAsync(cancellationToken); |
| | | 75 | | |
| | 0 | 76 | | public override Memory<byte> GetMemory(int sizeHint = 0) => _compressedDataWriter.GetMemory(sizeHint); |
| | | 77 | | |
| | 0 | 78 | | public override Span<byte> GetSpan(int sizeHint = 0) => _compressedDataWriter.GetSpan(sizeHint); |
| | | 79 | | |
| | | 80 | | public override ValueTask<FlushResult> WriteAsync( |
| | | 81 | | ReadOnlyMemory<byte> source, |
| | | 82 | | CancellationToken cancellationToken = default) => |
| | 12 | 83 | | _compressedDataWriter.WriteAsync(source, cancellationToken); |
| | | 84 | | |
| | 12 | 85 | | internal CompressorPipeWriter( |
| | 12 | 86 | | PipeWriter decoratee, |
| | 12 | 87 | | CompressionFormat compressionFormat, |
| | 12 | 88 | | CompressionLevel compressionLevel) |
| | 12 | 89 | | { |
| | 12 | 90 | | Debug.Assert(compressionFormat is CompressionFormat.Brotli or CompressionFormat.Deflate); |
| | | 91 | | |
| | 12 | 92 | | _decoratee = decoratee; |
| | | 93 | | |
| | | 94 | | // leaveOpen: true so that the completion of the compressed data writer never completes the decoratee. |
| | 12 | 95 | | Stream decorateeStream = decoratee.AsStream(leaveOpen: true); |
| | 12 | 96 | | _compressedDataWriter = PipeWriter.Create( |
| | 12 | 97 | | compressionFormat == CompressionFormat.Brotli ? |
| | 12 | 98 | | new BrotliStream(decorateeStream, compressionLevel) : |
| | 12 | 99 | | new DeflateStream(decorateeStream, compressionLevel)); |
| | 12 | 100 | | } |
| | | 101 | | } |