| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Transports; |
| | 4 | | using System.Buffers; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.IO.Pipelines; |
| | 7 | |
|
| | 8 | | namespace IceRpc.Internal; |
| | 9 | |
|
| | 10 | | internal static class PipeWriterExtensions |
| | 11 | | { |
| 2 | 12 | | private static readonly Exception _outputCompleteException = new(); |
| | 13 | |
|
| | 14 | | /// <summary>Completes the output provided by a <see cref="IMultiplexedStream" />.</summary> |
| | 15 | | /// <param name="output">The output (a pipe writer).</param> |
| | 16 | | /// <param name="success">When <see langword="true" />, the output is completed with a <see langword="null" /> |
| | 17 | | /// exception. Otherwise, it's completed with an exception. The exception used does not matter since Output behaves |
| | 18 | | /// the same when completed with any exception.</param> |
| | 19 | | internal static void CompleteOutput(this PipeWriter output, bool success) |
| 3739 | 20 | | { |
| 3739 | 21 | | if (success) |
| 3611 | 22 | | { |
| 3611 | 23 | | output.Complete(null); |
| 3611 | 24 | | } |
| | 25 | | else |
| 128 | 26 | | { |
| 128 | 27 | | output.Complete(_outputCompleteException); |
| 128 | 28 | | } |
| 3739 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary>Copies the contents of a <see cref="PipeReader"/> into this <see cref="PipeWriter" />.</summary> |
| | 32 | | /// <param name="writer">This pipe writer.</param> |
| | 33 | | /// <param name="reader">The pipe reader to copy. This method does not complete it.</param> |
| | 34 | | /// <param name="writesClosed">A task that completes when the writer can no longer write.</param> |
| | 35 | | /// <param name="endStream">When <see langword="true" />, no more data will be written to the writer after the |
| | 36 | | /// contents of the pipe reader.</param> |
| | 37 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 38 | | /// <returns>The flush result. <see cref="FlushResult.IsCanceled" /> is <langword name="true"/> when the copying is |
| | 39 | | /// interrupted by a call to <see cref="PipeReader.CancelPendingRead" /> on <paramref name="reader" />.</returns> |
| | 40 | | internal static async ValueTask<FlushResult> CopyFromAsync( |
| | 41 | | this PipeWriter writer, |
| | 42 | | PipeReader reader, |
| | 43 | | Task writesClosed, |
| | 44 | | bool endStream, |
| | 45 | | CancellationToken cancellationToken) |
| 3566 | 46 | | { |
| | 47 | | FlushResult flushResult; |
| 3566 | 48 | | if (reader.TryRead(out ReadResult readResult)) |
| 3534 | 49 | | { |
| | 50 | | // We optimize for the very common situation where the all the reader bytes are available. |
| 3534 | 51 | | flushResult = await WriteReadResultAsync().ConfigureAwait(false); |
| | 52 | |
|
| 3521 | 53 | | if (readResult.IsCompleted || flushResult.IsCanceled || flushResult.IsCompleted) |
| 3506 | 54 | | { |
| 3506 | 55 | | return flushResult; |
| | 56 | | } |
| 15 | 57 | | } |
| | 58 | |
|
| | 59 | | // If the peer is no longer reading, we want to cancel the reading of the payload. |
| 35 | 60 | | CancellationToken readToken = writesClosed.AsCancellationToken(cancellationToken); |
| | 61 | |
|
| | 62 | | do |
| 43 | 63 | | { |
| | 64 | | try |
| 43 | 65 | | { |
| 43 | 66 | | readResult = await reader.ReadAsync(readToken).ConfigureAwait(false); |
| 22 | 67 | | } |
| 17 | 68 | | catch (OperationCanceledException exception) when (exception.CancellationToken == readToken) |
| 13 | 69 | | { |
| 13 | 70 | | cancellationToken.ThrowIfCancellationRequested(); |
| 7 | 71 | | Debug.Assert(writesClosed.IsCompleted); |
| | 72 | |
|
| | 73 | | // This FlushAsync either throws an exception because the writer failed, or returns a completed |
| | 74 | | // FlushResult. |
| 7 | 75 | | return await writer.FlushAsync(CancellationToken.None).ConfigureAwait(false); |
| | 76 | | } |
| | 77 | | // we let other exceptions thrown by ReadAsync (including possibly an OperationCanceledException |
| | 78 | | // thrown incorrectly) escape. |
| | 79 | |
|
| 22 | 80 | | flushResult = await WriteReadResultAsync().ConfigureAwait(false); |
| 22 | 81 | | } |
| 22 | 82 | | while (!readResult.IsCompleted && !flushResult.IsCanceled && !flushResult.IsCompleted); |
| | 83 | |
|
| 14 | 84 | | return flushResult; |
| | 85 | |
|
| | 86 | | async ValueTask<FlushResult> WriteReadResultAsync() |
| 3556 | 87 | | { |
| 3556 | 88 | | if (readResult.IsCanceled) |
| 4 | 89 | | { |
| | 90 | | // The application (or an interceptor/middleware) called CancelPendingRead on reader. |
| 4 | 91 | | reader.AdvanceTo(readResult.Buffer.Start); // Did not consume any byte in reader. |
| | 92 | |
|
| | 93 | | // The copy was canceled. |
| 4 | 94 | | return new FlushResult(isCanceled: true, isCompleted: true); |
| | 95 | | } |
| | 96 | | else |
| 3552 | 97 | | { |
| | 98 | | try |
| 3552 | 99 | | { |
| 3552 | 100 | | return await writer.WriteAsync( |
| 3552 | 101 | | readResult.Buffer, |
| 3552 | 102 | | readResult.IsCompleted && endStream, |
| 3552 | 103 | | cancellationToken).ConfigureAwait(false); |
| | 104 | | } |
| | 105 | | finally |
| 3552 | 106 | | { |
| 3552 | 107 | | reader.AdvanceTo(readResult.Buffer.End); |
| 3552 | 108 | | } |
| | 109 | | } |
| 3543 | 110 | | } |
| 3525 | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary>Writes a read only sequence of bytes to this writer.</summary> |
| | 114 | | /// <param name="writer">The pipe writer.</param> |
| | 115 | | /// <param name="source">The source sequence.</param> |
| | 116 | | /// <param name="endStream">When <see langword="true" />, no more data will be written to the writer.</param> |
| | 117 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 118 | | /// <returns>The flush result.</returns> |
| | 119 | | private static async ValueTask<FlushResult> WriteAsync( |
| | 120 | | this PipeWriter writer, |
| | 121 | | ReadOnlySequence<byte> source, |
| | 122 | | bool endStream, |
| | 123 | | CancellationToken cancellationToken) |
| 3552 | 124 | | { |
| 3552 | 125 | | if (writer is ReadOnlySequencePipeWriter readOnlySequenceWriter) |
| 3536 | 126 | | { |
| 3536 | 127 | | return await readOnlySequenceWriter.WriteAsync(source, endStream, cancellationToken).ConfigureAwait(false); |
| | 128 | | } |
| | 129 | | else |
| 16 | 130 | | { |
| 16 | 131 | | FlushResult flushResult = default; |
| 16 | 132 | | if (source.IsEmpty) |
| 8 | 133 | | { |
| 8 | 134 | | flushResult = await writer.FlushAsync(cancellationToken).ConfigureAwait(false); |
| 6 | 135 | | } |
| 8 | 136 | | else if (source.IsSingleSegment) |
| 8 | 137 | | { |
| 8 | 138 | | flushResult = await writer.WriteAsync(source.First, cancellationToken).ConfigureAwait(false); |
| 8 | 139 | | } |
| | 140 | | else |
| 0 | 141 | | { |
| 0 | 142 | | foreach (ReadOnlyMemory<byte> buffer in source) |
| 0 | 143 | | { |
| 0 | 144 | | flushResult = await writer.WriteAsync(buffer, cancellationToken).ConfigureAwait(false); |
| 0 | 145 | | if (flushResult.IsCompleted || flushResult.IsCanceled) |
| 0 | 146 | | { |
| 0 | 147 | | break; |
| | 148 | | } |
| 0 | 149 | | } |
| 0 | 150 | | } |
| 14 | 151 | | return flushResult; |
| | 152 | | } |
| 3539 | 153 | | } |
| | 154 | | } |