| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Transports; |
| | 4 | | using System.Buffers; |
| | 5 | |
|
| | 6 | | namespace IceRpc; |
| | 7 | |
|
| | 8 | | /// <summary>Represents a property bag used to configure client and server connections.</summary> |
| | 9 | | public record class ConnectionOptions |
| | 10 | | { |
| | 11 | | /// <summary>Gets or sets the dispatcher that dispatches requests received by this connection.</summary> |
| | 12 | | /// <value>The dispatcher that dispatches requests received by this connection, or <see langword="null" /> if this |
| | 13 | | /// connection does not accept requests.</value> |
| 3394 | 14 | | public IDispatcher? Dispatcher { get; set; } |
| | 15 | |
|
| | 16 | | /// <summary>Gets or sets a value indicating whether or not to enable the Ice idle check. This option is specific to |
| | 17 | | /// the ice protocol. When the Ice idle check is enabled, a read operation on the underlying transport connection |
| | 18 | | /// fails when this read waits for over <see cref="IceIdleTimeout" /> to receive any byte. When the Ice idle check |
| | 19 | | /// is disabled, the <see cref="IceIdleTimeout" /> has no effect on reads: a read on the underlying transport |
| | 20 | | /// connection can wait forever to receive a byte.</summary> |
| | 21 | | /// <value><see langword="true"/> if the Ice idle check is enabled; otherwise, <see langword="false"/>. Defaults to |
| | 22 | | /// <see langword="true"/>.</value> |
| | 23 | | /// <remarks>Set to <see langword="false"/> when the peer is an Ice application using Ice 3.7 or earlier and you |
| | 24 | | /// can't update this application to turn on HeartbeatAlways with |
| | 25 | | /// <see href="https://doc.zeroc.com/ice/3.7/property-reference/ice-acm#id-.Ice.ACM.*v3.7-Ice.ACM.Heartbeat"/>. |
| | 26 | | /// When this value is set to <see langword="true"/>, make sure the peer's idle timeout is equal to |
| | 27 | | /// <see cref="IceIdleTimeout" />.</remarks> |
| 1617 | 28 | | public bool EnableIceIdleCheck { get; set; } = true; |
| | 29 | |
|
| | 30 | | /// <summary>Gets or sets the Ice idle timeout. This option is specific to the ice protocol. Once the connection is |
| | 31 | | /// established, the runtime sends a heartbeat to the peer when there is no write on the connection for half this |
| | 32 | | /// Ice idle timeout.</summary> |
| | 33 | | /// <value>The Ice idle timeout. Defaults to <c>60</c> seconds to match the default ACM configuration in Ice 3.7. |
| | 34 | | /// </value> |
| | 35 | | /// <seealso cref="EnableIceIdleCheck" /> |
| | 36 | | public TimeSpan IceIdleTimeout |
| | 37 | | { |
| 1116 | 38 | | get => _iceIdleTimeout; |
| 6 | 39 | | set => _iceIdleTimeout = value != TimeSpan.Zero ? value : |
| 6 | 40 | | throw new ArgumentException($"0 is not a valid value for {nameof(IceIdleTimeout)}", nameof(value)); |
| | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary>Gets or sets the inactivity timeout. This timeout is used to gracefully shutdown the connection if |
| | 44 | | /// it's inactive for longer than this timeout. A connection is considered inactive when there's no invocation or |
| | 45 | | /// dispatch in progress.</summary> |
| | 46 | | /// <value>The inactivity timeout. Defaults to <c>5</c> minutes.</value> |
| | 47 | | public TimeSpan InactivityTimeout |
| | 48 | | { |
| 1084 | 49 | | get => _inactivityTimeout; |
| 22 | 50 | | set => _inactivityTimeout = value != TimeSpan.Zero ? value : |
| 22 | 51 | | throw new ArgumentException($"0 is not a valid value for {nameof(InactivityTimeout)}", nameof(value)); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary>Gets or sets the maximum number of requests that a connection can dispatch concurrently. Once this |
| | 55 | | /// limit is reached, the connection stops reading new requests off its underlying transport connection.</summary> |
| | 56 | | /// <value>The maximum number of requests that a connection can dispatch concurrently. <c>0</c> means no maximum. |
| | 57 | | /// Defaults to <c>100</c> requests.</value> |
| | 58 | | /// <remarks>With the icerpc protocol, you may also need to set <see cref="MaxIceRpcBidirectionalStreams" /> and |
| | 59 | | /// <see cref="MaxIceRpcUnidirectionalStreams" />. A typical two-way dispatch holds onto one bidirectional stream |
| | 60 | | /// while a typical one-way dispatch quickly releases its unidirectional stream and then executes without consuming |
| | 61 | | /// any stream.</remarks> |
| | 62 | | public int MaxDispatches |
| | 63 | | { |
| 3252 | 64 | | get => _maxDispatches; |
| 22 | 65 | | set => _maxDispatches = value >= 0 ? value : |
| 22 | 66 | | throw new ArgumentOutOfRangeException(nameof(value), "value must be 0 or greater"); |
| | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary>Gets or sets the maximum size of a frame received over the ice protocol.</summary> |
| | 70 | | /// <value>The maximum size of an incoming frame, in bytes. This value must be at least <c>256</c>. Defaults to |
| | 71 | | /// <c>1</c> MB.</value> |
| | 72 | | public int MaxIceFrameSize |
| | 73 | | { |
| 372 | 74 | | get => _maxIceFrameSize; |
| 2 | 75 | | set => _maxIceFrameSize = value >= IceMinFrameSize ? value : |
| 2 | 76 | | throw new ArgumentOutOfRangeException( |
| 2 | 77 | | nameof(value), |
| 2 | 78 | | $"{nameof(MaxIceFrameSize)} must be at least {IceMinFrameSize}"); |
| | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary>Gets or sets the maximum allowed number of simultaneous remote bidirectional streams that can be |
| | 82 | | /// accepted on an icerpc connection. When this limit is reached, the peer is not allowed to open any new |
| | 83 | | /// bidirectional stream. Since an bidirectional stream is opened for each two-way invocation, the sending of the |
| | 84 | | /// two-way invocation will be delayed until another two-way invocation's stream completes.</summary> |
| | 85 | | /// <value>The maximum number of bidirectional streams. It can't be less than <c>1</c>. Defaults to <c>100</c>. |
| | 86 | | /// </value> |
| | 87 | | public int MaxIceRpcBidirectionalStreams |
| | 88 | | { |
| 107 | 89 | | get => _maxIceRpcBidirectionalStreams; |
| 0 | 90 | | set => _maxIceRpcBidirectionalStreams = value > 0 ? value : |
| 0 | 91 | | throw new ArgumentException( |
| 0 | 92 | | $"{nameof(MaxIceRpcBidirectionalStreams)} can't be less than 1", |
| 0 | 93 | | nameof(value)); |
| | 94 | | } |
| | 95 | |
|
| | 96 | | /// <summary>Gets or sets the maximum size of icerpc protocol header.</summary> |
| | 97 | | /// <value>The maximum size in bytes of the header of an incoming request, response or control frame. Defaults to |
| | 98 | | /// <c>16,383</c>, and the range of this value is <c>63</c> to <c>1,048,575</c>.</value> |
| | 99 | | public int MaxIceRpcHeaderSize |
| | 100 | | { |
| 712 | 101 | | get => _maxIceRpcHeaderSize; |
| 4 | 102 | | set => _maxIceRpcHeaderSize = IceRpcCheckMaxHeaderSize(value); |
| | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary>Gets or sets the maximum allowed number of simultaneous remote unidirectional streams that can be |
| | 106 | | /// accepted on an icerpc connection. When this limit is reached, the peer is not allowed to open any new |
| | 107 | | /// unidirectional stream. Since an unidirectional stream is opened for each one-way invocation, the sending of the |
| | 108 | | /// one-way invocation will be delayed until another one-way invocation's stream completes.</summary> |
| | 109 | | /// <value>The maximum number of unidirectional streams. It can't be less than <c>1</c>. Defaults to |
| | 110 | | /// <c>100</c>.</value> |
| | 111 | | public int MaxIceRpcUnidirectionalStreams |
| | 112 | | { |
| 107 | 113 | | get => _maxIceRpcUnidirectionalStreams; |
| 0 | 114 | | set => _maxIceRpcUnidirectionalStreams = value > 0 ? value : |
| 0 | 115 | | throw new ArgumentException( |
| 0 | 116 | | $"{nameof(MaxIceRpcUnidirectionalStreams)} can't be less than 1", |
| 0 | 117 | | nameof(value)); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary>Gets or sets the minimum size of the segment requested from the <see cref="Pool" />.</summary> |
| | 121 | | /// <value>The minimum size of the segment requested from the <see cref="Pool" />. Defaults to <c>4096</c>.</value> |
| | 122 | | public int MinSegmentSize |
| | 123 | | { |
| 1566 | 124 | | get => _minSegmentSize; |
| 0 | 125 | | set => _minSegmentSize = value >= 1024 ? value : |
| 0 | 126 | | throw new ArgumentException($"{nameof(MinSegmentSize)} can't be less than 1KB", nameof(value)); |
| | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary>Gets or sets the <see cref="MemoryPool{T}" /> object used by the connection for allocating memory |
| | 130 | | /// blocks.</summary> |
| | 131 | | /// <value>A pool of memory blocks used for buffer management. Defaults to <see cref="MemoryPool{T}.Shared" |
| | 132 | | /// />.</value> |
| 2811 | 133 | | public MemoryPool<byte> Pool { get; set; } = MemoryPool<byte>.Shared; |
| | 134 | |
|
| | 135 | | /// <summary>The default value for <see cref="MaxIceRpcHeaderSize" />.</summary> |
| | 136 | | internal const int DefaultMaxIceRpcHeaderSize = 16_383; |
| | 137 | |
|
| | 138 | | private const int IceMinFrameSize = 256; |
| | 139 | |
|
| 1245 | 140 | | private TimeSpan _iceIdleTimeout = TimeSpan.FromSeconds(60); |
| 1245 | 141 | | private TimeSpan _inactivityTimeout = TimeSpan.FromMinutes(5); |
| 1245 | 142 | | private int _maxDispatches = 100; |
| 1245 | 143 | | private int _maxIceFrameSize = 1024 * 1024; |
| 1245 | 144 | | private int _maxIceRpcBidirectionalStreams = MultiplexedConnectionOptions.DefaultMaxBidirectionalStreams; |
| 1245 | 145 | | private int _maxIceRpcHeaderSize = DefaultMaxIceRpcHeaderSize; |
| 1245 | 146 | | private int _maxIceRpcUnidirectionalStreams = MultiplexedConnectionOptions.DefaultMaxUnidirectionalStreams; |
| 1245 | 147 | | private int _minSegmentSize = 4096; |
| | 148 | |
|
| 10 | 149 | | internal static int IceRpcCheckMaxHeaderSize(long value) => value is >= 63 and <= 1_048_575 ? (int)value : |
| 10 | 150 | | throw new ArgumentOutOfRangeException(nameof(value), "value must be between 63 and 1,048,575"); |
| | 151 | | } |