| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Features; |
| | | 4 | | using IceRpc.Internal; |
| | | 5 | | using IceRpc.Slice.Operations.Internal; |
| | | 6 | | using System.Buffers; |
| | | 7 | | using System.IO.Pipelines; |
| | | 8 | | using ZeroC.Slice.Codec; |
| | | 9 | | |
| | | 10 | | namespace IceRpc.Slice.Operations; |
| | | 11 | | |
| | | 12 | | /// <summary>Provides extension methods for <see cref="PipeReader" />.</summary> |
| | | 13 | | public static class PipeReaderExtensions |
| | | 14 | | { |
| | | 15 | | // 4 = varuint62 encoding of the size (1) |
| | | 16 | | // 252 = varint32 encoding of the tag end marker (-1) |
| | 1 | 17 | | private static readonly ReadOnlySequence<byte> _emptyStructPayload = new([4, 252]); |
| | | 18 | | |
| | | 19 | | extension(PipeReader) |
| | | 20 | | { |
| | | 21 | | /// <summary>Creates a request or response payload holding an empty Slice struct.</summary> |
| | | 22 | | /// <returns>The payload.</returns> |
| | 12 | 23 | | public static PipeReader CreateEmptySliceStructPayload() => PipeReader.Create(_emptyStructPayload); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary>Creates an async stream over a pipe reader to decode streamed elements.</summary> |
| | | 27 | | /// <typeparam name="T">The type of the element being decoded.</typeparam> |
| | | 28 | | /// <param name="reader">The pipe reader.</param> |
| | | 29 | | /// <param name="decodeFunc">The function used to decode the streamed member.</param> |
| | | 30 | | /// <param name="elementSize">The size in bytes of one element.</param> |
| | | 31 | | /// <param name="sliceFeature">The Slice feature to customize the decoding.</param> |
| | | 32 | | /// <returns>The async stream to decode and return the streamed elements.</returns> |
| | | 33 | | /// <exception cref="ArgumentException">Thrown if <paramref name="elementSize" /> is equal of inferior to |
| | | 34 | | /// <c>0</c>.</exception> |
| | | 35 | | /// <remarks>The reader ownership is transferred to the returned async stream. The caller should no longer use |
| | | 36 | | /// the reader after this call, and must dispose the returned async stream when done to release the reader. |
| | | 37 | | /// </remarks> |
| | | 38 | | public static IAsyncStream<T> ToAsyncStream<T>( |
| | | 39 | | this PipeReader reader, |
| | | 40 | | DecodeFunc<T> decodeFunc, |
| | | 41 | | int elementSize, |
| | | 42 | | ISliceFeature? sliceFeature = null) |
| | 222 | 43 | | { |
| | 222 | 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 | | |
| | 222 | 50 | | sliceFeature ??= SliceFeature.Default; |
| | 222 | 51 | | return new AsyncStream<T>(reader, ReadAsync, DecodeBuffer); |
| | | 52 | | |
| | | 53 | | IEnumerable<T> DecodeBuffer(ReadOnlySequence<byte> buffer) |
| | 20 | 54 | | { |
| | | 55 | | // Since the elements are fixed-size, they can't contain service addresses hence baseProxy can remain null. |
| | 20 | 56 | | var decoder = new SliceDecoder( |
| | 20 | 57 | | buffer, |
| | 20 | 58 | | maxCollectionAllocation: sliceFeature.MaxCollectionAllocation); |
| | | 59 | | |
| | 20 | 60 | | var items = new T[buffer.Length / elementSize]; |
| | 131434 | 61 | | for (int i = 0; i < items.Length; ++i) |
| | 65698 | 62 | | { |
| | 65698 | 63 | | items[i] = decodeFunc(ref decoder); |
| | 65697 | 64 | | } |
| | 19 | 65 | | decoder.CheckEndOfBuffer(); |
| | 19 | 66 | | return items; |
| | 19 | 67 | | } |
| | | 68 | | |
| | | 69 | | async ValueTask<ReadResult> ReadAsync(PipeReader reader, CancellationToken cancellationToken) |
| | 221 | 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. |
| | 221 | 74 | | ReadResult readResult = await reader.ReadAtLeastAsync(elementSize, cancellationToken).ConfigureAwait(false); |
| | | 75 | | |
| | | 76 | | // Check if the buffer contains extra bytes that we need to remove. |
| | 22 | 77 | | ReadOnlySequence<byte> buffer = readResult.Buffer; |
| | 22 | 78 | | if (elementSize > 1 && buffer.Length > elementSize) |
| | 19 | 79 | | { |
| | 19 | 80 | | long extra = buffer.Length % elementSize; |
| | 19 | 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 | | } |
| | 19 | 86 | | } |
| | | 87 | | |
| | | 88 | | // Return the read result as-is. |
| | 22 | 89 | | return readResult; |
| | 22 | 90 | | } |
| | 222 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary>Creates an async stream 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 stream to decode and return the streamed members.</returns> |
| | | 100 | | /// <remarks>The reader ownership is transferred to the returned async stream. The caller should no longer use |
| | | 101 | | /// the reader after this call, and must dispose the returned async stream when done to release the reader. |
| | | 102 | | /// </remarks> |
| | | 103 | | public static IAsyncStream<T> ToAsyncStream<T>( |
| | | 104 | | this PipeReader reader, |
| | | 105 | | DecodeFunc<T> decodeFunc, |
| | | 106 | | ISliceProxy? sender = null, |
| | | 107 | | ISliceFeature? sliceFeature = null) |
| | 13 | 108 | | { |
| | 13 | 109 | | sliceFeature ??= SliceFeature.Default; |
| | 13 | 110 | | ISliceProxy? baseProxy = sliceFeature.BaseProxy ?? sender; |
| | 13 | 111 | | return new AsyncStream<T>(reader, ReadAsync, DecodeBuffer); |
| | | 112 | | |
| | | 113 | | IEnumerable<T> DecodeBuffer(ReadOnlySequence<byte> buffer) |
| | 13 | 114 | | { |
| | 13 | 115 | | var decoder = new SliceDecoder(buffer, baseProxy, sliceFeature.MaxCollectionAllocation); |
| | | 116 | | |
| | 13 | 117 | | var items = new List<T>(); |
| | | 118 | | do |
| | 65679 | 119 | | { |
| | 65679 | 120 | | items.Add(decodeFunc(ref decoder)); |
| | 65678 | 121 | | } |
| | 65678 | 122 | | while (decoder.Consumed < buffer.Length); |
| | | 123 | | |
| | 12 | 124 | | return items; |
| | 12 | 125 | | } |
| | | 126 | | |
| | | 127 | | ValueTask<ReadResult> ReadAsync(PipeReader reader, CancellationToken cancellationToken) => |
| | 14 | 128 | | reader.ReadSliceSegmentAsync(sliceFeature.MaxSegmentSize, cancellationToken); |
| | 13 | 129 | | } |
| | | 130 | | } |