| | 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; |
| 1373 | 17 | | private readonly CancellationTokenSource _disposeCts = new(); |
| | 18 | | private Task? _disposeTask; |
| | 19 | | private readonly Pipe _pipe; |
| | 20 | |
|
| 178219 | 21 | | public void Advance(int bytes) => _pipe.Writer.Advance(bytes); |
| | 22 | |
|
| | 23 | | /// <inheritdoc/> |
| | 24 | | public ValueTask DisposeAsync() |
| 1371 | 25 | | { |
| 1371 | 26 | | _disposeTask ??= PerformDisposeAsync(); |
| 1371 | 27 | | return new(_disposeTask); |
| | 28 | |
|
| | 29 | | async Task PerformDisposeAsync() |
| 1371 | 30 | | { |
| 1371 | 31 | | _disposeCts.Cancel(); |
| | 32 | |
|
| 1371 | 33 | | await WriterTask.ConfigureAwait(false); |
| | 34 | |
|
| 1371 | 35 | | _pipe.Reader.Complete(); |
| 1371 | 36 | | _pipe.Writer.Complete(); |
| | 37 | |
|
| 1371 | 38 | | _disposeCts.Dispose(); |
| 1371 | 39 | | } |
| 1371 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <inheritdoc/> |
| 0 | 43 | | public Memory<byte> GetMemory(int sizeHint = 0) => _pipe.Writer.GetMemory(sizeHint); |
| | 44 | |
|
| | 45 | | /// <inheritdoc/> |
| 178219 | 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> |
| 1373 | 53 | | internal SlicDuplexConnectionWriter(IDuplexConnection connection, MemoryPool<byte> pool, int minimumSegmentSize) |
| 1373 | 54 | | { |
| 1373 | 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. |
| 1373 | 60 | | _pipe = new Pipe(new PipeOptions( |
| 1373 | 61 | | pool: pool, |
| 1373 | 62 | | minimumSegmentSize: minimumSegmentSize, |
| 1373 | 63 | | pauseWriterThreshold: 0, |
| 1373 | 64 | | useSynchronizationContext: false)); |
| | 65 | |
|
| 1373 | 66 | | WriterTask = Task.Run( |
| 1373 | 67 | | async () => |
| 1373 | 68 | | { |
| 1373 | 69 | | try |
| 1373 | 70 | | { |
| 8923 | 71 | | while (true) |
| 8923 | 72 | | { |
| 8923 | 73 | | ReadResult readResult = await _pipe.Reader.ReadAsync(_disposeCts.Token).ConfigureAwait(false); |
| 1373 | 74 | |
|
| 7839 | 75 | | if (!readResult.Buffer.IsEmpty) |
| 7645 | 76 | | { |
| 7645 | 77 | | await _connection.WriteAsync(readResult.Buffer, _disposeCts.Token).ConfigureAwait(false); |
| 7626 | 78 | | _pipe.Reader.AdvanceTo(readResult.Buffer.End); |
| 7626 | 79 | | } |
| 1373 | 80 | |
|
| 7820 | 81 | | if (readResult.IsCompleted) |
| 270 | 82 | | { |
| 270 | 83 | | await _connection.ShutdownWriteAsync(_disposeCts.Token).ConfigureAwait(false); |
| 266 | 84 | | break; |
| 1373 | 85 | | } |
| 7550 | 86 | | } |
| 266 | 87 | | _pipe.Reader.Complete(); |
| 266 | 88 | | } |
| 1098 | 89 | | catch (OperationCanceledException) |
| 1098 | 90 | | { |
| 1373 | 91 | | // DisposeAsync was called. |
| 1098 | 92 | | } |
| 7 | 93 | | catch (Exception exception) |
| 7 | 94 | | { |
| 7 | 95 | | _pipe.Reader.Complete(exception); |
| 7 | 96 | | } |
| 2744 | 97 | | }); |
| 1373 | 98 | | } |
| | 99 | |
|
| | 100 | | internal void Flush() |
| 25475 | 101 | | { |
| 25475 | 102 | | ValueTask<FlushResult> flushResult = _pipe.Writer.FlushAsync(CancellationToken.None); |
| | 103 | |
|
| | 104 | | // PauseWriterThreshold is 0 so FlushAsync should always complete synchronously. |
| 25475 | 105 | | Debug.Assert(flushResult.IsCompletedSuccessfully); |
| 25475 | 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. |
| 270 | 112 | | _pipe.Writer.Complete(); |
| | 113 | | } |