| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Slice; |
| | | 4 | | |
| | | 5 | | namespace IceRpc.Features; |
| | | 6 | | |
| | | 7 | | /// <summary>Default implementation of <see cref="ISliceFeature" />.</summary> |
| | | 8 | | public sealed class SliceFeature : ISliceFeature |
| | | 9 | | { |
| | | 10 | | /// <summary>Gets a <see cref="ISliceFeature" /> with default values for all properties.</summary> |
| | 441 | 11 | | public static ISliceFeature Default { get; } = new DefaultSliceFeature(); |
| | | 12 | | |
| | | 13 | | /// <inheritdoc/> |
| | 1 | 14 | | public ISliceProxy? BaseProxy { get; } |
| | | 15 | | |
| | | 16 | | /// <inheritdoc/> |
| | 0 | 17 | | public SliceEncodeOptions? EncodeOptions { get; } |
| | | 18 | | |
| | | 19 | | /// <inheritdoc/> |
| | | 20 | | /// <value>The maximum collection allocation. Defaults to 8 times <see cref="MaxSegmentSize" />.</value> |
| | 1 | 21 | | public int MaxCollectionAllocation { get; } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc/> |
| | | 24 | | /// <value>The maximum size of a Slice payload segment, in bytes. Defaults to <c>1</c> MB.</value> |
| | 1 | 25 | | public int MaxSegmentSize { get; } |
| | | 26 | | |
| | | 27 | | /// <summary>Constructs a Slice feature.</summary> |
| | | 28 | | /// <param name="encodeOptions">The encode options.</param> |
| | | 29 | | /// <param name="maxCollectionAllocation">The maximum collection allocation. Use <c>-1</c> to get the default value: |
| | | 30 | | /// 8 times <paramref name="maxSegmentSize" /> if set, otherwise the value provided by <paramref |
| | | 31 | | /// name="defaultFeature" />.</param> |
| | | 32 | | /// <param name="maxSegmentSize">The maximum segment size. Use <c>-1</c> to get the default value.</param> |
| | | 33 | | /// <param name="baseProxy">The base proxy, used when decoding service addresses into proxies.</param> |
| | | 34 | | /// <param name="defaultFeature">A feature that provides default values for all parameters. <see langword="null" /> |
| | | 35 | | /// is equivalent to <see cref="Default" />.</param> |
| | | 36 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="maxCollectionAllocation" /> is a |
| | | 37 | | /// negative value other than <c>-1</c>, when <paramref name="maxSegmentSize" /> is <c>0</c> or a negative value |
| | | 38 | | /// other than <c>-1</c>, or when <paramref name="maxSegmentSize" /> is greater than <c>int.MaxValue / 8</c> |
| | | 39 | | /// (256 MB) while <paramref name="maxCollectionAllocation" /> is <c>-1</c>.</exception> |
| | 1 | 40 | | public SliceFeature( |
| | 1 | 41 | | SliceEncodeOptions? encodeOptions = null, |
| | 1 | 42 | | int maxCollectionAllocation = -1, |
| | 1 | 43 | | int maxSegmentSize = -1, |
| | 1 | 44 | | ISliceProxy? baseProxy = null, |
| | 1 | 45 | | ISliceFeature? defaultFeature = null) |
| | 1 | 46 | | { |
| | 1 | 47 | | if (maxCollectionAllocation < -1) |
| | 0 | 48 | | { |
| | 0 | 49 | | throw new ArgumentOutOfRangeException( |
| | 0 | 50 | | nameof(maxCollectionAllocation), |
| | 0 | 51 | | $"The value of {nameof(maxCollectionAllocation)} must be greater than or equal to 0, or -1."); |
| | | 52 | | } |
| | 1 | 53 | | if (maxSegmentSize is < -1 or 0) |
| | 0 | 54 | | { |
| | 0 | 55 | | throw new ArgumentOutOfRangeException( |
| | 0 | 56 | | nameof(maxSegmentSize), |
| | 0 | 57 | | $"The value of {nameof(maxSegmentSize)} must be greater than 0, or -1."); |
| | | 58 | | } |
| | 1 | 59 | | if (maxCollectionAllocation == -1 && maxSegmentSize > int.MaxValue / 8) |
| | 0 | 60 | | { |
| | 0 | 61 | | throw new ArgumentOutOfRangeException( |
| | 0 | 62 | | nameof(maxSegmentSize), |
| | 0 | 63 | | $"The value of {nameof(maxSegmentSize)} must be less than or equal to int.MaxValue / 8 when {nameof(maxC |
| | | 64 | | } |
| | | 65 | | |
| | 1 | 66 | | defaultFeature ??= Default; |
| | | 67 | | |
| | 1 | 68 | | EncodeOptions = encodeOptions ?? defaultFeature.EncodeOptions; |
| | | 69 | | |
| | 1 | 70 | | MaxCollectionAllocation = maxCollectionAllocation >= 0 ? maxCollectionAllocation : |
| | 1 | 71 | | (maxSegmentSize > 0 ? 8 * maxSegmentSize : defaultFeature.MaxCollectionAllocation); |
| | | 72 | | |
| | 1 | 73 | | MaxSegmentSize = maxSegmentSize > 0 ? maxSegmentSize : defaultFeature.MaxSegmentSize; |
| | | 74 | | |
| | 1 | 75 | | BaseProxy = baseProxy ?? defaultFeature.BaseProxy; |
| | 1 | 76 | | } |
| | | 77 | | |
| | | 78 | | private class DefaultSliceFeature : ISliceFeature |
| | | 79 | | { |
| | 161 | 80 | | public ISliceProxy? BaseProxy => null; |
| | | 81 | | |
| | 1 | 82 | | public SliceEncodeOptions? EncodeOptions => null; |
| | | 83 | | |
| | 182 | 84 | | public int MaxCollectionAllocation => 8 * MaxSegmentSize; |
| | | 85 | | |
| | 402 | 86 | | public int MaxSegmentSize => 1024 * 1024; |
| | | 87 | | } |
| | | 88 | | } |