| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Ice; |
| | | 4 | | using IceRpc.Ice.Codec; |
| | | 5 | | |
| | | 6 | | namespace IceRpc.Features; |
| | | 7 | | |
| | | 8 | | /// <summary>Default implementation of <see cref="IIceFeature" />.</summary> |
| | | 9 | | public sealed class IceFeature : IIceFeature |
| | | 10 | | { |
| | | 11 | | /// <summary>Gets a <see cref="IIceFeature" /> with default values for all properties.</summary> |
| | 268 | 12 | | public static IIceFeature Default { get; } = new DefaultIceFeature(); |
| | | 13 | | |
| | | 14 | | /// <inheritdoc/> |
| | 1 | 15 | | public IActivator? Activator { get; } |
| | | 16 | | |
| | | 17 | | /// <inheritdoc/> |
| | 1 | 18 | | public IIceProxy? BaseProxy { get; } |
| | | 19 | | |
| | | 20 | | /// <inheritdoc/> |
| | 0 | 21 | | public IceEncodeOptions? EncodeOptions { get; } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc/> |
| | | 24 | | /// <value>The maximum collection allocation. Defaults to 8 times <see cref="MaxPayloadSize" />.</value> |
| | 1 | 25 | | public int MaxCollectionAllocation { get; } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc/> |
| | | 28 | | /// <value>The maximum depth. Defaults to <c>100</c>.</value> |
| | 1 | 29 | | public int MaxDepth { get; } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc/> |
| | | 32 | | /// <value>The maximum size of an Ice-encoded payload, in bytes. Defaults to <c>1</c> MB.</value> |
| | 1 | 33 | | public int MaxPayloadSize { get; } |
| | | 34 | | |
| | | 35 | | /// <summary>Constructs an Ice feature.</summary> |
| | | 36 | | /// <param name="activator">The activator.</param> |
| | | 37 | | /// <param name="encodeOptions">The encode options.</param> |
| | | 38 | | /// <param name="maxCollectionAllocation">The maximum collection allocation. Use <c>-1</c> to get the default value: |
| | | 39 | | /// 8 times <paramref name="maxPayloadSize" /> if set, otherwise the value provided by |
| | | 40 | | /// <paramref name="defaultFeature" />.</param> |
| | | 41 | | /// <param name="maxDepth">The maximum depth. Use <c>-1</c> to get the default value.</param> |
| | | 42 | | /// <param name="maxPayloadSize">The maximum size of an Ice-encoded payload, in bytes. Use <c>-1</c> to get the |
| | | 43 | | /// default value.</param> |
| | | 44 | | /// <param name="baseProxy">The base proxy, used when decoding service addresses into proxies.</param> |
| | | 45 | | /// <param name="defaultFeature">A feature that provides default values for all parameters. <see langword="null" /> |
| | | 46 | | /// is equivalent to <see cref="Default" />.</param> |
| | | 47 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="maxCollectionAllocation" /> is a |
| | | 48 | | /// negative value other than <c>-1</c>, when <paramref name="maxDepth" /> is <c>0</c> or a negative value other |
| | | 49 | | /// than <c>-1</c>, when <paramref name="maxPayloadSize" /> is <see cref="int.MaxValue" /> or a negative value other |
| | | 50 | | /// than <c>-1</c>, or when <paramref name="maxPayloadSize" /> is greater than <c>int.MaxValue / 8</c> |
| | | 51 | | /// (256 MB) while <paramref name="maxCollectionAllocation" /> is <c>-1</c>.</exception> |
| | 1 | 52 | | public IceFeature( |
| | 1 | 53 | | IActivator? activator = null, |
| | 1 | 54 | | IceEncodeOptions? encodeOptions = null, |
| | 1 | 55 | | int maxCollectionAllocation = -1, |
| | 1 | 56 | | int maxDepth = -1, |
| | 1 | 57 | | int maxPayloadSize = -1, |
| | 1 | 58 | | IIceProxy? baseProxy = null, |
| | 1 | 59 | | IIceFeature? defaultFeature = null) |
| | 1 | 60 | | { |
| | 1 | 61 | | if (maxCollectionAllocation < -1) |
| | 0 | 62 | | { |
| | 0 | 63 | | throw new ArgumentOutOfRangeException( |
| | 0 | 64 | | nameof(maxCollectionAllocation), |
| | 0 | 65 | | $"The value of {nameof(maxCollectionAllocation)} must be greater than or equal to 0, or -1."); |
| | | 66 | | } |
| | 1 | 67 | | if (maxDepth is < -1 or 0) |
| | 0 | 68 | | { |
| | 0 | 69 | | throw new ArgumentOutOfRangeException( |
| | 0 | 70 | | nameof(maxDepth), |
| | 0 | 71 | | $"The value of {nameof(maxDepth)} must be greater than 0, or -1."); |
| | | 72 | | } |
| | 1 | 73 | | if (maxPayloadSize is < -1 or int.MaxValue) |
| | 0 | 74 | | { |
| | 0 | 75 | | throw new ArgumentOutOfRangeException( |
| | 0 | 76 | | nameof(maxPayloadSize), |
| | 0 | 77 | | $"The value of {nameof(maxPayloadSize)} must be greater than or equal to 0 and less than int.MaxValue, o |
| | | 78 | | } |
| | 1 | 79 | | if (maxCollectionAllocation == -1 && maxPayloadSize > int.MaxValue / 8) |
| | 0 | 80 | | { |
| | 0 | 81 | | throw new ArgumentOutOfRangeException( |
| | 0 | 82 | | nameof(maxPayloadSize), |
| | 0 | 83 | | $"The value of {nameof(maxPayloadSize)} must be less than or equal to int.MaxValue / 8 when {nameof(maxC |
| | | 84 | | } |
| | | 85 | | |
| | 1 | 86 | | defaultFeature ??= Default; |
| | | 87 | | |
| | 1 | 88 | | Activator = activator ?? defaultFeature.Activator; |
| | 1 | 89 | | EncodeOptions = encodeOptions ?? defaultFeature.EncodeOptions; |
| | | 90 | | |
| | 1 | 91 | | MaxCollectionAllocation = maxCollectionAllocation >= 0 ? maxCollectionAllocation : |
| | 1 | 92 | | (maxPayloadSize >= 0 ? 8 * maxPayloadSize : defaultFeature.MaxCollectionAllocation); |
| | | 93 | | |
| | 1 | 94 | | MaxDepth = maxDepth > 0 ? maxDepth : defaultFeature.MaxDepth; |
| | | 95 | | |
| | 1 | 96 | | MaxPayloadSize = maxPayloadSize >= 0 ? maxPayloadSize : defaultFeature.MaxPayloadSize; |
| | | 97 | | |
| | 1 | 98 | | BaseProxy = baseProxy ?? defaultFeature.BaseProxy; |
| | 1 | 99 | | } |
| | | 100 | | |
| | | 101 | | private class DefaultIceFeature : IIceFeature |
| | | 102 | | { |
| | 193 | 103 | | public IActivator? Activator => null; |
| | | 104 | | |
| | 132 | 105 | | public IIceProxy? BaseProxy => null; |
| | | 106 | | |
| | 1 | 107 | | public IceEncodeOptions? EncodeOptions => null; |
| | | 108 | | |
| | 133 | 109 | | public int MaxCollectionAllocation => 8 * MaxPayloadSize; |
| | | 110 | | |
| | 133 | 111 | | public int MaxDepth => 100; |
| | | 112 | | |
| | 382 | 113 | | public int MaxPayloadSize => 1024 * 1024; |
| | | 114 | | } |
| | | 115 | | } |