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