| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Buffers; |
| | | 4 | | using System.IO.Pipelines; |
| | | 5 | | |
| | | 6 | | namespace IceRpc.Transports.Slic.Internal; |
| | | 7 | | |
| | | 8 | | /// <summary>A helper class to write data to a duplex connection. Its methods shouldn't be called concurrently. The data |
| | | 9 | | /// written to this writer is copied and buffered with an internal pipe. The data from the pipe is written on the duplex |
| | | 10 | | /// connection with a background task.</summary> |
| | | 11 | | internal class SlicDuplexConnectionWriter : IBufferWriter<byte>, IAsyncDisposable |
| | | 12 | | { |
| | 1648 | 13 | | internal Task WriterTask { get; private init; } |
| | | 14 | | |
| | | 15 | | private readonly IDuplexConnection _connection; |
| | 753 | 16 | | private readonly CancellationTokenSource _disposeCts = new(); |
| | | 17 | | private Task? _disposeTask; |
| | | 18 | | private readonly Pipe _pipe; |
| | | 19 | | |
| | 80356 | 20 | | public void Advance(int bytes) => _pipe.Writer.Advance(bytes); |
| | | 21 | | |
| | | 22 | | /// <inheritdoc/> |
| | | 23 | | public ValueTask DisposeAsync() |
| | 752 | 24 | | { |
| | 752 | 25 | | _disposeTask ??= PerformDisposeAsync(); |
| | 752 | 26 | | return new(_disposeTask); |
| | | 27 | | |
| | | 28 | | async Task PerformDisposeAsync() |
| | 752 | 29 | | { |
| | 752 | 30 | | _disposeCts.Cancel(); |
| | | 31 | | |
| | 752 | 32 | | await WriterTask.ConfigureAwait(false); |
| | | 33 | | |
| | 752 | 34 | | _pipe.Reader.Complete(); |
| | 752 | 35 | | _pipe.Writer.Complete(); |
| | | 36 | | |
| | 752 | 37 | | _disposeCts.Dispose(); |
| | 752 | 38 | | } |
| | 752 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc/> |
| | 0 | 42 | | public Memory<byte> GetMemory(int sizeHint = 0) => _pipe.Writer.GetMemory(sizeHint); |
| | | 43 | | |
| | | 44 | | /// <inheritdoc/> |
| | 80356 | 45 | | public Span<byte> GetSpan(int sizeHint = 0) => _pipe.Writer.GetSpan(sizeHint); |
| | | 46 | | |
| | | 47 | | /// <summary>Constructs a duplex connection writer.</summary> |
| | | 48 | | /// <param name="connection">The duplex connection to write to.</param> |
| | | 49 | | /// <param name="pool">The memory pool to use.</param> |
| | | 50 | | /// <param name="minimumSegmentSize">The minimum segment size for buffers allocated from <paramref |
| | | 51 | | /// name="pool"/>.</param> |
| | | 52 | | /// <param name="pauseWriterThreshold">The pipe pause writer threshold. When buffered data exceeds this value, <see |
| | | 53 | | /// cref="FlushAsync" /> blocks until the background writer task drains enough data from the pipe.</param> |
| | 753 | 54 | | internal SlicDuplexConnectionWriter( |
| | 753 | 55 | | IDuplexConnection connection, |
| | 753 | 56 | | MemoryPool<byte> pool, |
| | 753 | 57 | | int minimumSegmentSize, |
| | 753 | 58 | | int pauseWriterThreshold) |
| | 753 | 59 | | { |
| | 753 | 60 | | _connection = connection; |
| | | 61 | | |
| | 753 | 62 | | _pipe = new Pipe(new PipeOptions( |
| | 753 | 63 | | pool: pool, |
| | 753 | 64 | | minimumSegmentSize: minimumSegmentSize, |
| | 753 | 65 | | pauseWriterThreshold: pauseWriterThreshold, |
| | 753 | 66 | | // Match the algorithm PipeOptions uses for its defaults: resumeWriterThreshold = pauseWriterThreshold / 2. |
| | 753 | 67 | | // Without this override, the PipeOptions default of 32 KB would exceed pauseWriterThreshold for small |
| | 753 | 68 | | // thresholds and throw. When pauseWriterThreshold is 0, leave both at 0 to disable pausing. |
| | 753 | 69 | | resumeWriterThreshold: pauseWriterThreshold == 0 ? 0 : pauseWriterThreshold / 2, |
| | 753 | 70 | | useSynchronizationContext: false)); |
| | | 71 | | |
| | 753 | 72 | | WriterTask = Task.Run( |
| | 753 | 73 | | async () => |
| | 753 | 74 | | { |
| | 753 | 75 | | try |
| | 753 | 76 | | { |
| | 7184 | 77 | | while (true) |
| | 7184 | 78 | | { |
| | 7184 | 79 | | ReadResult readResult = await _pipe.Reader.ReadAsync(_disposeCts.Token).ConfigureAwait(false); |
| | 753 | 80 | | |
| | 6577 | 81 | | if (!readResult.Buffer.IsEmpty) |
| | 6482 | 82 | | { |
| | 6482 | 83 | | await _connection.WriteAsync(readResult.Buffer, _disposeCts.Token).ConfigureAwait(false); |
| | 6479 | 84 | | _pipe.Reader.AdvanceTo(readResult.Buffer.End); |
| | 6479 | 85 | | } |
| | 753 | 86 | | |
| | 6574 | 87 | | if (readResult.IsCompleted) |
| | 143 | 88 | | { |
| | 143 | 89 | | await _connection.ShutdownWriteAsync(_disposeCts.Token).ConfigureAwait(false); |
| | 141 | 90 | | break; |
| | 753 | 91 | | } |
| | 6431 | 92 | | } |
| | 141 | 93 | | _pipe.Reader.Complete(); |
| | 141 | 94 | | } |
| | 611 | 95 | | catch (OperationCanceledException) |
| | 611 | 96 | | { |
| | 753 | 97 | | // DisposeAsync was called. |
| | 611 | 98 | | } |
| | 0 | 99 | | catch (Exception exception) |
| | 0 | 100 | | { |
| | 0 | 101 | | _pipe.Reader.Complete(exception); |
| | 0 | 102 | | } |
| | 1505 | 103 | | }); |
| | 753 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary>Flushes the underlying pipe. May block when the buffered data exceeds the configured pause writer |
| | | 107 | | /// threshold.</summary> |
| | | 108 | | internal ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken) => |
| | 12239 | 109 | | _pipe.Writer.FlushAsync(cancellationToken); |
| | | 110 | | |
| | | 111 | | /// <summary>Requests the shut down of the duplex connection writes after the buffered data is written on the |
| | | 112 | | /// duplex connection.</summary> |
| | | 113 | | internal void Shutdown() => |
| | | 114 | | // Completing the pipe writer makes the background write task complete successfully. |
| | 143 | 115 | | _pipe.Writer.Complete(); |
| | | 116 | | } |