| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Buffers; |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.IO.Pipelines; |
| | | 6 | | |
| | | 7 | | namespace IceRpc.Transports.Slic.Internal; |
| | | 8 | | |
| | | 9 | | /// <summary>A helper class to write data to a duplex connection. Its methods shouldn't be called concurrently. The data |
| | | 10 | | /// written to this writer is copied and buffered with an internal pipe. The data from the pipe is written on the duplex |
| | | 11 | | /// connection with a background task.</summary> |
| | | 12 | | internal class SlicDuplexConnectionWriter : IBufferWriter<byte>, IAsyncDisposable |
| | | 13 | | { |
| | 3014 | 14 | | internal Task WriterTask { get; private init; } |
| | | 15 | | |
| | | 16 | | private readonly IDuplexConnection _connection; |
| | 1372 | 17 | | private readonly CancellationTokenSource _disposeCts = new(); |
| | | 18 | | private Task? _disposeTask; |
| | | 19 | | private readonly Pipe _pipe; |
| | | 20 | | |
| | 178338 | 21 | | public void Advance(int bytes) => _pipe.Writer.Advance(bytes); |
| | | 22 | | |
| | | 23 | | /// <inheritdoc/> |
| | | 24 | | public ValueTask DisposeAsync() |
| | 1370 | 25 | | { |
| | 1370 | 26 | | _disposeTask ??= PerformDisposeAsync(); |
| | 1370 | 27 | | return new(_disposeTask); |
| | | 28 | | |
| | | 29 | | async Task PerformDisposeAsync() |
| | 1370 | 30 | | { |
| | 1370 | 31 | | _disposeCts.Cancel(); |
| | | 32 | | |
| | 1370 | 33 | | await WriterTask.ConfigureAwait(false); |
| | | 34 | | |
| | 1370 | 35 | | _pipe.Reader.Complete(); |
| | 1370 | 36 | | _pipe.Writer.Complete(); |
| | | 37 | | |
| | 1370 | 38 | | _disposeCts.Dispose(); |
| | 1370 | 39 | | } |
| | 1370 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc/> |
| | 0 | 43 | | public Memory<byte> GetMemory(int sizeHint = 0) => _pipe.Writer.GetMemory(sizeHint); |
| | | 44 | | |
| | | 45 | | /// <inheritdoc/> |
| | 178338 | 46 | | public Span<byte> GetSpan(int sizeHint = 0) => _pipe.Writer.GetSpan(sizeHint); |
| | | 47 | | |
| | | 48 | | /// <summary>Constructs a duplex connection writer.</summary> |
| | | 49 | | /// <param name="connection">The duplex connection to write to.</param> |
| | | 50 | | /// <param name="pool">The memory pool to use.</param> |
| | | 51 | | /// <param name="minimumSegmentSize">The minimum segment size for buffers allocated from <paramref |
| | | 52 | | /// name="pool"/>.</param> |
| | 1372 | 53 | | internal SlicDuplexConnectionWriter(IDuplexConnection connection, MemoryPool<byte> pool, int minimumSegmentSize) |
| | 1372 | 54 | | { |
| | 1372 | 55 | | _connection = connection; |
| | | 56 | | |
| | | 57 | | // We set pauseWriterThreshold to 0 because Slic implements flow-control at the stream level. So there's no need |
| | | 58 | | // to limit the amount of data buffered by the writer pipe. The amount of data buffered is limited to |
| | | 59 | | // (MaxBidirectionalStreams + MaxUnidirectionalStreams) * PeerPauseWriterThreshold bytes. |
| | 1372 | 60 | | _pipe = new Pipe(new PipeOptions( |
| | 1372 | 61 | | pool: pool, |
| | 1372 | 62 | | minimumSegmentSize: minimumSegmentSize, |
| | 1372 | 63 | | pauseWriterThreshold: 0, |
| | 1372 | 64 | | useSynchronizationContext: false)); |
| | | 65 | | |
| | 1372 | 66 | | WriterTask = Task.Run( |
| | 1372 | 67 | | async () => |
| | 1372 | 68 | | { |
| | 1372 | 69 | | try |
| | 1372 | 70 | | { |
| | 10170 | 71 | | while (true) |
| | 10170 | 72 | | { |
| | 10170 | 73 | | ReadResult readResult = await _pipe.Reader.ReadAsync(_disposeCts.Token).ConfigureAwait(false); |
| | 1372 | 74 | | |
| | 9086 | 75 | | if (!readResult.Buffer.IsEmpty) |
| | 8901 | 76 | | { |
| | 8901 | 77 | | await _connection.WriteAsync(readResult.Buffer, _disposeCts.Token).ConfigureAwait(false); |
| | 8885 | 78 | | _pipe.Reader.AdvanceTo(readResult.Buffer.End); |
| | 8885 | 79 | | } |
| | 1372 | 80 | | |
| | 9070 | 81 | | if (readResult.IsCompleted) |
| | 272 | 82 | | { |
| | 272 | 83 | | await _connection.ShutdownWriteAsync(_disposeCts.Token).ConfigureAwait(false); |
| | 268 | 84 | | break; |
| | 1372 | 85 | | } |
| | 8798 | 86 | | } |
| | 268 | 87 | | _pipe.Reader.Complete(); |
| | 268 | 88 | | } |
| | 1099 | 89 | | catch (OperationCanceledException) |
| | 1099 | 90 | | { |
| | 1372 | 91 | | // DisposeAsync was called. |
| | 1099 | 92 | | } |
| | 3 | 93 | | catch (Exception exception) |
| | 3 | 94 | | { |
| | 3 | 95 | | _pipe.Reader.Complete(exception); |
| | 3 | 96 | | } |
| | 2742 | 97 | | }); |
| | 1372 | 98 | | } |
| | | 99 | | |
| | | 100 | | internal void Flush() |
| | 25508 | 101 | | { |
| | 25508 | 102 | | ValueTask<FlushResult> flushResult = _pipe.Writer.FlushAsync(CancellationToken.None); |
| | | 103 | | |
| | | 104 | | // PauseWriterThreshold is 0 so FlushAsync should always complete synchronously. |
| | 25508 | 105 | | Debug.Assert(flushResult.IsCompletedSuccessfully); |
| | 25508 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary>Requests the shut down of the duplex connection writes after the buffered data is written on the |
| | | 109 | | /// duplex connection.</summary> |
| | | 110 | | internal void Shutdown() => |
| | | 111 | | // Completing the pipe writer makes the background write task complete successfully. |
| | 272 | 112 | | _pipe.Writer.Complete(); |
| | | 113 | | } |