| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Internal; |
| | 4 | | using IceRpc.Slice.Internal; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Globalization; |
| | 7 | | using ZeroC.Slice; |
| | 8 | |
|
| | 9 | | namespace IceRpc.Slice; |
| | 10 | |
|
| | 11 | | /// <summary>Provides extension methods for <see cref="SliceEncoder" /> to encode service addresses.</summary> |
| | 12 | | public static class ServiceAddressSliceEncoderExtensions |
| | 13 | | { |
| | 14 | | /// <summary>The default timeout value for tcp/ssl server addresses with Slice1.</summary> |
| | 15 | | internal const int DefaultTcpTimeout = 60_000; // 60s |
| | 16 | |
|
| | 17 | | internal const string OpaqueName = "opaque"; |
| | 18 | | internal const string SslName = "ssl"; |
| | 19 | | internal const string TcpName = "tcp"; |
| | 20 | |
|
| | 21 | | /// <summary>Encodes a service address.</summary> |
| | 22 | | /// <param name="encoder">The Slice encoder.</param> |
| | 23 | | /// <param name="value">The value to encode.</param> |
| | 24 | | public static void EncodeServiceAddress(this ref SliceEncoder encoder, ServiceAddress value) |
| 69 | 25 | | { |
| 69 | 26 | | if (encoder.Encoding == SliceEncoding.Slice1) |
| 46 | 27 | | { |
| 46 | 28 | | if (value.Protocol is not Protocol protocol) |
| 0 | 29 | | { |
| 0 | 30 | | throw new NotSupportedException("Cannot encode a relative service address with Slice1."); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | // With Slice1, a non-null proxy/service address is encoded as: |
| | 34 | | // - identity, fragment, invocation mode, secure, protocol major and minor, and the encoding major and minor |
| | 35 | | // - a sequence of server addresses (can be empty) |
| | 36 | | // - an adapter ID string present only when the sequence of server addresses is empty |
| | 37 | |
|
| 46 | 38 | | var identity = Identity.Parse(value.Path); |
| 46 | 39 | | if (identity.Name.Length == 0) |
| 2 | 40 | | { |
| 2 | 41 | | throw new ArgumentException( |
| 2 | 42 | | "Cannot encode a non-null service address with a null Ice identity.", |
| 2 | 43 | | nameof(value)); |
| | 44 | | } |
| 44 | 45 | | identity.Encode(ref encoder); |
| | 46 | |
|
| 44 | 47 | | encoder.EncodeFragment(value.Fragment); |
| 44 | 48 | | encoder.EncodeInvocationMode(InvocationMode.Twoway); |
| 44 | 49 | | encoder.EncodeBool(false); // Secure |
| 44 | 50 | | encoder.EncodeUInt8(protocol.ByteValue); // Protocol Major |
| 44 | 51 | | encoder.EncodeUInt8(0); // Protocol Minor |
| 44 | 52 | | encoder.EncodeUInt8(1); // Encoding Major |
| 44 | 53 | | encoder.EncodeUInt8(1); // Encoding Minor |
| | 54 | |
|
| 44 | 55 | | if (value.ServerAddress is ServerAddress serverAddress) |
| 42 | 56 | | { |
| 42 | 57 | | encoder.EncodeSize(1 + value.AltServerAddresses.Count); // server address count |
| 42 | 58 | | encoder.EncodeServerAddress(serverAddress); |
| 109 | 59 | | foreach (ServerAddress altServer in value.AltServerAddresses) |
| 2 | 60 | | { |
| 2 | 61 | | encoder.EncodeServerAddress(altServer); |
| 2 | 62 | | } |
| 35 | 63 | | } |
| | 64 | | else |
| 2 | 65 | | { |
| 2 | 66 | | encoder.EncodeSize(0); // 0 server addresses |
| 2 | 67 | | int maxCount = value.Params.TryGetValue("adapter-id", out string? adapterId) ? 1 : 0; |
| | 68 | |
|
| 2 | 69 | | if (value.Params.Count > maxCount) |
| 0 | 70 | | { |
| 0 | 71 | | throw new NotSupportedException( |
| 0 | 72 | | "Cannot encode a service address with a parameter other than adapter-id using Slice1."); |
| | 73 | | } |
| 2 | 74 | | encoder.EncodeString(adapterId ?? ""); |
| 2 | 75 | | } |
| 37 | 76 | | } |
| | 77 | | else |
| 23 | 78 | | { |
| 23 | 79 | | encoder.EncodeString(value.ToString()); // a URI or an absolute path |
| 23 | 80 | | } |
| 60 | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary>Encodes a nullable service address (Slice1 only).</summary> |
| | 84 | | /// <param name="encoder">The Slice encoder.</param> |
| | 85 | | /// <param name="value">The service address to encode, or <see langword="null" />.</param> |
| | 86 | | public static void EncodeNullableServiceAddress(this ref SliceEncoder encoder, ServiceAddress? value) |
| 23 | 87 | | { |
| 23 | 88 | | if (encoder.Encoding != SliceEncoding.Slice1) |
| 0 | 89 | | { |
| 0 | 90 | | throw new InvalidOperationException( |
| 0 | 91 | | "Encoding a nullable service address without a bit sequence is only supported with Slice1."); |
| | 92 | | } |
| | 93 | |
|
| 23 | 94 | | if (value is not null) |
| 14 | 95 | | { |
| 14 | 96 | | encoder.EncodeServiceAddress(value); |
| 14 | 97 | | } |
| | 98 | | else |
| 9 | 99 | | { |
| 9 | 100 | | Identity.Empty.Encode(ref encoder); |
| 9 | 101 | | } |
| 23 | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary>Encodes a server address in a nested encapsulation (Slice1 only).</summary> |
| | 105 | | /// <param name="encoder">The Slice encoder.</param> |
| | 106 | | /// <param name="serverAddress">The server address to encode.</param> |
| | 107 | | private static void EncodeServerAddress(this ref SliceEncoder encoder, ServerAddress serverAddress) |
| 44 | 108 | | { |
| | 109 | | // If the server address does not specify a transport, we default to TCP. We can't encode "default". |
| 44 | 110 | | string transport = serverAddress.Transport ?? TcpName; |
| | 111 | |
|
| | 112 | | // The Slice1 encoding of ice server addresses is transport-specific, and hard-coded here. |
| | 113 | |
|
| 44 | 114 | | if (serverAddress.Protocol == Protocol.Ice && transport == OpaqueName) |
| 11 | 115 | | { |
| | 116 | | // Opaque server address encoding |
| | 117 | |
|
| 11 | 118 | | (short transportCode, byte encodingMajor, byte encodingMinor, ReadOnlyMemory<byte> bytes) = |
| 11 | 119 | | serverAddress.ParseOpaqueParams(); |
| | 120 | |
|
| 4 | 121 | | encoder.EncodeInt16(transportCode); |
| | 122 | |
|
| | 123 | | // encapsulation size includes size-length and 2 bytes for encoding |
| 4 | 124 | | encoder.EncodeInt32(4 + 2 + bytes.Length); |
| 4 | 125 | | encoder.EncodeUInt8(encodingMajor); |
| 4 | 126 | | encoder.EncodeUInt8(encodingMinor); |
| 4 | 127 | | encoder.WriteByteSpan(bytes.Span); |
| 4 | 128 | | } |
| | 129 | | else |
| 33 | 130 | | { |
| 33 | 131 | | TransportCode transportCode = serverAddress.Protocol == Protocol.Ice ? |
| 33 | 132 | | transport switch |
| 33 | 133 | | { |
| 3 | 134 | | SslName => TransportCode.Ssl, |
| 11 | 135 | | TcpName => TransportCode.Tcp, |
| 1 | 136 | | _ => TransportCode.Uri |
| 33 | 137 | | } : |
| 33 | 138 | | TransportCode.Uri; |
| | 139 | |
|
| 33 | 140 | | encoder.EncodeInt16((short)transportCode); |
| | 141 | |
|
| 33 | 142 | | int startPos = encoder.EncodedByteCount; // size includes size-length |
| 33 | 143 | | Span<byte> sizePlaceholder = encoder.GetPlaceholderSpan(4); // encapsulation size |
| 33 | 144 | | encoder.EncodeUInt8(1); // encoding version major |
| 33 | 145 | | encoder.EncodeUInt8(1); // encoding version minor |
| | 146 | |
|
| 33 | 147 | | switch (transportCode) |
| | 148 | | { |
| | 149 | | case TransportCode.Tcp: |
| | 150 | | case TransportCode.Ssl: |
| 14 | 151 | | encoder.EncodeTcpServerAddressBody(serverAddress); |
| 14 | 152 | | break; |
| | 153 | |
|
| | 154 | | default: |
| 19 | 155 | | Debug.Assert(transportCode == TransportCode.Uri); |
| 19 | 156 | | encoder.EncodeString(serverAddress.ToString()); |
| 19 | 157 | | break; |
| | 158 | | } |
| | 159 | |
|
| 33 | 160 | | SliceEncoder.EncodeInt32(encoder.EncodedByteCount - startPos, sizePlaceholder); |
| 33 | 161 | | } |
| 37 | 162 | | } |
| | 163 | |
|
| | 164 | | /// <summary>Encodes the body of a tcp or ssl server address using Slice1.</summary> |
| | 165 | | private static void EncodeTcpServerAddressBody(this ref SliceEncoder encoder, ServerAddress serverAddress) |
| 14 | 166 | | { |
| 14 | 167 | | Debug.Assert(encoder.Encoding == SliceEncoding.Slice1); |
| 14 | 168 | | Debug.Assert(serverAddress.Protocol == Protocol.Ice); |
| | 169 | |
|
| 14 | 170 | | new TcpServerAddressBody( |
| 14 | 171 | | serverAddress.Host, |
| 14 | 172 | | serverAddress.Port, |
| 14 | 173 | | timeout: serverAddress.Params.TryGetValue("t", out string? timeoutValue) ? |
| 14 | 174 | | (timeoutValue == "infinite" ? -1 : int.Parse(timeoutValue, CultureInfo.InvariantCulture)) : |
| 14 | 175 | | DefaultTcpTimeout, |
| 14 | 176 | | compress: serverAddress.Params.ContainsKey("z")).Encode(ref encoder); |
| 14 | 177 | | } |
| | 178 | |
|
| | 179 | | /// <summary>Parses the params of an opaque server address (Slice1 only).</summary> |
| | 180 | | private static (short TransportCode, byte EncodingMajor, byte EncodingMinor, ReadOnlyMemory<byte> Bytes) ParseOpaque |
| | 181 | | this ServerAddress serverAddress) |
| 11 | 182 | | { |
| 11 | 183 | | short transportCode = -1; |
| 11 | 184 | | ReadOnlyMemory<byte> bytes = default; |
| 11 | 185 | | byte encodingMajor = 1; |
| 11 | 186 | | byte encodingMinor = 1; |
| | 187 | |
|
| 70 | 188 | | foreach ((string name, string value) in serverAddress.Params) |
| 21 | 189 | | { |
| 21 | 190 | | switch (name) |
| | 191 | | { |
| | 192 | | case "e": |
| 4 | 193 | | (encodingMajor, encodingMinor) = value switch |
| 4 | 194 | | { |
| 2 | 195 | | "1.0" => ((byte)1, (byte)0), |
| 1 | 196 | | "1.1" => ((byte)1, (byte)1), |
| 1 | 197 | | _ => throw new FormatException( |
| 1 | 198 | | $"Invalid value for parameter 'e' in server address: '{serverAddress}'.") |
| 4 | 199 | | }; |
| 3 | 200 | | break; |
| | 201 | |
|
| | 202 | | case "t": |
| | 203 | | try |
| 8 | 204 | | { |
| 8 | 205 | | transportCode = short.Parse(value, CultureInfo.InvariantCulture); |
| 7 | 206 | | } |
| 1 | 207 | | catch (FormatException exception) |
| 1 | 208 | | { |
| 1 | 209 | | throw new FormatException( |
| 1 | 210 | | $"Invalid value for parameter 't' in server address: '{serverAddress}'.", exception); |
| | 211 | | } |
| | 212 | |
|
| 7 | 213 | | if (transportCode < 0) |
| 1 | 214 | | { |
| 1 | 215 | | throw new FormatException( |
| 1 | 216 | | $"The value for parameter 't' is out of range in server address: '{serverAddress}'."); |
| | 217 | | } |
| 6 | 218 | | break; |
| | 219 | |
|
| | 220 | | case "v": |
| | 221 | | try |
| 8 | 222 | | { |
| 8 | 223 | | bytes = Convert.FromBase64String(value); |
| 7 | 224 | | } |
| 1 | 225 | | catch (FormatException exception) |
| 1 | 226 | | { |
| 1 | 227 | | throw new FormatException( |
| 1 | 228 | | $"Invalid Base64 value in server address: '{serverAddress}'.", |
| 1 | 229 | | exception); |
| | 230 | | } |
| 7 | 231 | | break; |
| | 232 | |
|
| | 233 | | default: |
| 1 | 234 | | throw new FormatException($"Unknown parameter '{name}' in server address: '{serverAddress}'."); |
| | 235 | | } |
| 16 | 236 | | } |
| | 237 | |
|
| 6 | 238 | | if (transportCode == -1) |
| 1 | 239 | | { |
| 1 | 240 | | throw new FormatException($"Missing 't' parameter in server address: '{serverAddress}'."); |
| | 241 | | } |
| 5 | 242 | | else if (bytes.Length == 0) |
| 1 | 243 | | { |
| 1 | 244 | | throw new FormatException($"Missing 'v' parameter in server address: '{serverAddress}'."); |
| | 245 | | } |
| | 246 | |
|
| 4 | 247 | | return (transportCode, encodingMajor, encodingMinor, bytes); |
| 4 | 248 | | } |
| | 249 | | } |