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