| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Internal; |
| | 4 | | using System.Buffers; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.IO.Pipelines; |
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | | using ZeroC.Slice; |
| | 9 | |
|
| | 10 | | namespace IceRpc.Slice; |
| | 11 | |
|
| | 12 | | /// <summary>Provides extension methods for <see cref="PipeReader" /> to decode streamed elements.</summary> |
| | 13 | | public static class PipeReaderExtensions |
| | 14 | | { |
| | 15 | | /// <summary>Creates an async enumerable over a pipe reader to decode streamed elements.</summary> |
| | 16 | | /// <typeparam name="T">The type of the element being decoded.</typeparam> |
| | 17 | | /// <param name="reader">The pipe reader.</param> |
| | 18 | | /// <param name="encoding">The Slice encoding version.</param> |
| | 19 | | /// <param name="decodeFunc">The function used to decode the streamed member.</param> |
| | 20 | | /// <param name="elementSize">The size in bytes of one element.</param> |
| | 21 | | /// <param name="sliceFeature">The Slice feature to customize the decoding.</param> |
| | 22 | | /// <returns>The async enumerable to decode and return the streamed elements.</returns> |
| | 23 | | /// <exception cref="ArgumentException">Thrown if <paramref name="elementSize" /> is equal of inferior to |
| | 24 | | /// <c>0</c>.</exception> |
| | 25 | | /// <remarks>The reader ownership is transferred to the returned async enumerable. The caller should no longer use |
| | 26 | | /// the reader after this call.</remarks> |
| | 27 | | public static IAsyncEnumerable<T> ToAsyncEnumerable<T>( |
| | 28 | | this PipeReader reader, |
| | 29 | | SliceEncoding encoding, |
| | 30 | | DecodeFunc<T> decodeFunc, |
| | 31 | | int elementSize, |
| | 32 | | ISliceFeature? sliceFeature = null) |
| 13 | 33 | | { |
| 13 | 34 | | if (elementSize <= 0) |
| 0 | 35 | | { |
| 0 | 36 | | reader.Complete(); |
| 0 | 37 | | throw new ArgumentException("The element size must be greater than 0.", nameof(elementSize)); |
| | 38 | | } |
| | 39 | |
|
| 13 | 40 | | sliceFeature ??= SliceFeature.Default; |
| 13 | 41 | | return reader.ToAsyncEnumerable(ReadAsync, DecodeBuffer); |
| | 42 | |
|
| | 43 | | IEnumerable<T> DecodeBuffer(ReadOnlySequence<byte> buffer) |
| 18 | 44 | | { |
| | 45 | | // Since the elements are fixed-size, they can't contain service addresses hence baseProxy can remain null. |
| 18 | 46 | | var decoder = new SliceDecoder( |
| 18 | 47 | | buffer, |
| 18 | 48 | | encoding, |
| 18 | 49 | | maxCollectionAllocation: sliceFeature.MaxCollectionAllocation, |
| 18 | 50 | | maxDepth: sliceFeature.MaxDepth); |
| | 51 | |
|
| 18 | 52 | | var items = new T[buffer.Length / elementSize]; |
| 131418 | 53 | | for (int i = 0; i < items.Length; ++i) |
| 65692 | 54 | | { |
| 65692 | 55 | | items[i] = decodeFunc(ref decoder); |
| 65691 | 56 | | } |
| 17 | 57 | | decoder.CheckEndOfBuffer(); |
| 17 | 58 | | return items; |
| 17 | 59 | | } |
| | 60 | |
|
| | 61 | | async ValueTask<ReadResult> ReadAsync(PipeReader reader, CancellationToken cancellationToken) |
| 21 | 62 | | { |
| | 63 | | // Read the bytes for at least one element. |
| | 64 | | // Note that the max number of bytes we can read in one shot is limited by the flow control of the |
| | 65 | | // underlying transport. |
| 21 | 66 | | ReadResult readResult = await reader.ReadAtLeastAsync(elementSize, cancellationToken).ConfigureAwait(false); |
| | 67 | |
|
| | 68 | | // Check if the buffer contains extra bytes that we need to remove. |
| 20 | 69 | | ReadOnlySequence<byte> buffer = readResult.Buffer; |
| 20 | 70 | | if (elementSize > 1 && buffer.Length > elementSize) |
| 17 | 71 | | { |
| 17 | 72 | | long extra = buffer.Length % elementSize; |
| 17 | 73 | | if (extra > 0) |
| 0 | 74 | | { |
| 0 | 75 | | buffer = buffer.Slice(0, buffer.Length - extra); |
| 0 | 76 | | return new ReadResult(buffer, isCanceled: readResult.IsCanceled, isCompleted: false); |
| | 77 | | } |
| 17 | 78 | | } |
| | 79 | |
|
| | 80 | | // Return the read result as-is. |
| 20 | 81 | | return readResult; |
| 20 | 82 | | } |
| 13 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary>Creates an async enumerable over a pipe reader to decode variable size streamed elements.</summary> |
| | 86 | | /// <typeparam name="T">The stream element type.</typeparam> |
| | 87 | | /// <param name="reader">The pipe reader.</param> |
| | 88 | | /// <param name="encoding">The Slice encoding version.</param> |
| | 89 | | /// <param name="decodeFunc">The function used to decode the streamed member.</param> |
| | 90 | | /// <param name="sender">The proxy that sent the request, if applicable.</param> |
| | 91 | | /// <param name="sliceFeature">The slice feature to customize the decoding.</param> |
| | 92 | | /// <returns>The async enumerable to decode and return the streamed members.</returns> |
| | 93 | | /// <remarks>The reader ownership is transferred to the returned async enumerable. The caller should no longer use |
| | 94 | | /// the reader after this call.</remarks> |
| | 95 | | public static IAsyncEnumerable<T> ToAsyncEnumerable<T>( |
| | 96 | | this PipeReader reader, |
| | 97 | | SliceEncoding encoding, |
| | 98 | | DecodeFunc<T> decodeFunc, |
| | 99 | | IProxy? sender = null, |
| | 100 | | ISliceFeature? sliceFeature = null) |
| 13 | 101 | | { |
| 13 | 102 | | sliceFeature ??= SliceFeature.Default; |
| 13 | 103 | | IProxy? baseProxy = sliceFeature.BaseProxy ?? sender; |
| 13 | 104 | | return reader.ToAsyncEnumerable(ReadAsync, DecodeBuffer); |
| | 105 | |
|
| | 106 | | IEnumerable<T> DecodeBuffer(ReadOnlySequence<byte> buffer) |
| 13 | 107 | | { |
| | 108 | | // No activator or max depth since streams are Slice2+. |
| 13 | 109 | | var decoder = new SliceDecoder(buffer, encoding, baseProxy, sliceFeature.MaxCollectionAllocation); |
| | 110 | |
|
| 13 | 111 | | var items = new List<T>(); |
| | 112 | | do |
| 65679 | 113 | | { |
| 65679 | 114 | | items.Add(decodeFunc(ref decoder)); |
| 65678 | 115 | | } |
| 65678 | 116 | | while (decoder.Consumed < buffer.Length); |
| | 117 | |
|
| 12 | 118 | | return items; |
| 12 | 119 | | } |
| | 120 | |
|
| | 121 | | ValueTask<ReadResult> ReadAsync(PipeReader reader, CancellationToken cancellationToken) => |
| 14 | 122 | | reader.ReadSegmentAsync(encoding, sliceFeature.MaxSegmentSize, cancellationToken); |
| 13 | 123 | | } |
| | 124 | |
|
| | 125 | | /// <summary>Decodes an async enumerable from a pipe reader.</summary> |
| | 126 | | /// <param name="reader">The pipe reader.</param> |
| | 127 | | /// <param name="readFunc">The function used to read enough data to decode elements returned by the |
| | 128 | | /// enumerable.</param> |
| | 129 | | /// <param name="decodeBufferFunc">The function used to decode an element.</param> |
| | 130 | | /// <param name="cancellationToken">The cancellation token which is provided to <see |
| | 131 | | /// cref="IAsyncEnumerable{T}.GetAsyncEnumerator(CancellationToken)" />.</param> |
| | 132 | | private static async IAsyncEnumerable<T> ToAsyncEnumerable<T>( |
| | 133 | | this PipeReader reader, |
| | 134 | | Func<PipeReader, CancellationToken, ValueTask<ReadResult>> readFunc, |
| | 135 | | Func<ReadOnlySequence<byte>, IEnumerable<T>> decodeBufferFunc, |
| | 136 | | [EnumeratorCancellation] CancellationToken cancellationToken = default) |
| 25 | 137 | | { |
| | 138 | | try |
| 25 | 139 | | { |
| 35 | 140 | | while (true) |
| 35 | 141 | | { |
| | 142 | | ReadResult readResult; |
| | 143 | |
|
| | 144 | | try |
| 35 | 145 | | { |
| 35 | 146 | | readResult = await readFunc(reader, cancellationToken).ConfigureAwait(false); |
| | 147 | |
|
| 34 | 148 | | if (readResult.IsCanceled) |
| 0 | 149 | | { |
| | 150 | | // We never call CancelPendingRead; an interceptor or middleware can but it's not correct. |
| 0 | 151 | | throw new InvalidOperationException("Unexpected call to CancelPendingRead."); |
| | 152 | | } |
| 34 | 153 | | if (readResult.Buffer.IsEmpty) |
| 3 | 154 | | { |
| 3 | 155 | | Debug.Assert(readResult.IsCompleted); |
| 3 | 156 | | yield break; |
| | 157 | | } |
| 31 | 158 | | } |
| 1 | 159 | | catch (OperationCanceledException exception) when (exception.CancellationToken == cancellationToken) |
| 1 | 160 | | { |
| | 161 | | // Canceling the cancellation token is a normal way to complete an iteration. |
| 1 | 162 | | yield break; |
| | 163 | | } |
| | 164 | |
|
| 31 | 165 | | IEnumerable<T> elements = decodeBufferFunc(readResult.Buffer); |
| 29 | 166 | | reader.AdvanceTo(readResult.Buffer.End); |
| | 167 | |
|
| 262817 | 168 | | foreach (T item in elements) |
| 131366 | 169 | | { |
| 131366 | 170 | | if (cancellationToken.IsCancellationRequested) |
| 1 | 171 | | { |
| 1 | 172 | | yield break; |
| | 173 | | } |
| 131365 | 174 | | yield return item; |
| 131364 | 175 | | } |
| | 176 | |
|
| 27 | 177 | | if (readResult.IsCompleted) |
| 17 | 178 | | { |
| 17 | 179 | | yield break; |
| | 180 | | } |
| 10 | 181 | | } |
| | 182 | | } |
| | 183 | | finally |
| 25 | 184 | | { |
| 25 | 185 | | reader.Complete(); |
| 25 | 186 | | } |
| 23 | 187 | | } |
| | 188 | | } |