| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Collections.Immutable; |
| | | 4 | | using System.ComponentModel; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using static IceRpc.Ice.Codec.Internal.IceEncodingDefinitions; |
| | | 8 | | |
| | | 9 | | namespace IceRpc.Ice.Codec; |
| | | 10 | | |
| | | 11 | | /// <summary>Provides methods to encode data with Ice.</summary> |
| | | 12 | | public ref partial struct IceEncoder |
| | | 13 | | { |
| | | 14 | | /// <summary>Encodes a class instance, or <see langword="null" />.</summary> |
| | | 15 | | /// <param name="v">The class instance to encode, or <see langword="null" />.</param> |
| | | 16 | | public void EncodeClass(IceClass? v) |
| | 544 | 17 | | { |
| | 544 | 18 | | if (v is null) |
| | 256 | 19 | | { |
| | 256 | 20 | | EncodeSize(0); |
| | 256 | 21 | | } |
| | | 22 | | else |
| | 288 | 23 | | { |
| | 288 | 24 | | if (_classContext.Current.InstanceType != InstanceType.None && |
| | 288 | 25 | | _classContext.ClassFormat == ClassFormat.Sliced) |
| | 31 | 26 | | { |
| | | 27 | | // If encoding an instance within a slice and using the sliced format, encode an index of that |
| | | 28 | | // slice's indirection table. |
| | 31 | 29 | | if (_classContext.Current.IndirectionMap is not null && |
| | 31 | 30 | | _classContext.Current.IndirectionMap.TryGetValue(v, out int index)) |
| | 1 | 31 | | { |
| | | 32 | | // Found, index is position in indirection table + 1 |
| | 1 | 33 | | Debug.Assert(index > 0); |
| | 1 | 34 | | } |
| | | 35 | | else |
| | 30 | 36 | | { |
| | 30 | 37 | | _classContext.Current.IndirectionTable ??= new List<IceClass>(); |
| | 30 | 38 | | _classContext.Current.IndirectionMap ??= new Dictionary<IceClass, int>(); |
| | 30 | 39 | | _classContext.Current.IndirectionTable.Add(v); |
| | 30 | 40 | | index = _classContext.Current.IndirectionTable.Count; // Position + 1 (0 is reserved for null) |
| | 30 | 41 | | _classContext.Current.IndirectionMap.Add(v, index); |
| | 30 | 42 | | } |
| | 31 | 43 | | EncodeSize(index); |
| | 31 | 44 | | } |
| | | 45 | | else |
| | 257 | 46 | | { |
| | 257 | 47 | | EncodeInstance(v); // Encodes the instance or a reference if already encoded. |
| | 257 | 48 | | } |
| | 288 | 49 | | } |
| | 544 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary>Marks the end of the encoding of a class or exception slice.</summary> |
| | | 53 | | /// <param name="lastSlice">Whether this is the last slice or not.</param> |
| | | 54 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 55 | | public void EndSlice(bool lastSlice) |
| | 491 | 56 | | { |
| | 491 | 57 | | Debug.Assert(_classContext.Current.InstanceType != InstanceType.None); |
| | | 58 | | |
| | 491 | 59 | | if (lastSlice) |
| | 305 | 60 | | { |
| | 305 | 61 | | _classContext.Current.SliceFlags |= SliceFlags.IsLastSlice; |
| | 305 | 62 | | } |
| | | 63 | | |
| | | 64 | | // Encodes the tagged end marker if some tagged fields were encoded. Note that tagged fields are encoded before |
| | | 65 | | // the indirection table and are included in the slice size. |
| | 491 | 66 | | if ((_classContext.Current.SliceFlags & SliceFlags.HasTaggedFields) != 0) |
| | 25 | 67 | | { |
| | 25 | 68 | | EncodeByte(TagEndMarker); |
| | 25 | 69 | | } |
| | | 70 | | |
| | | 71 | | // Encodes the slice size if necessary. |
| | 491 | 72 | | if ((_classContext.Current.SliceFlags & SliceFlags.HasSliceSize) != 0) |
| | 151 | 73 | | { |
| | | 74 | | // Size includes the size length. |
| | 151 | 75 | | EncodeInt( |
| | 151 | 76 | | EncodedByteCount - _classContext.Current.SliceSizeStartPos, |
| | 151 | 77 | | _classContext.Current.SliceSizePlaceholder.Span); |
| | 151 | 78 | | } |
| | | 79 | | |
| | 491 | 80 | | if (_classContext.Current.IndirectionTable?.Count > 0) |
| | 36 | 81 | | { |
| | 36 | 82 | | Debug.Assert(_classContext.ClassFormat == ClassFormat.Sliced); |
| | 36 | 83 | | _classContext.Current.SliceFlags |= SliceFlags.HasIndirectionTable; |
| | | 84 | | |
| | 36 | 85 | | EncodeSize(_classContext.Current.IndirectionTable.Count); |
| | 184 | 86 | | foreach (IceClass v in _classContext.Current.IndirectionTable) |
| | 38 | 87 | | { |
| | 38 | 88 | | EncodeInstance(v); |
| | 38 | 89 | | } |
| | 36 | 90 | | _classContext.Current.IndirectionTable.Clear(); |
| | 36 | 91 | | _classContext.Current.IndirectionMap?.Clear(); // IndirectionMap is null when encoding unknown slices. |
| | 36 | 92 | | } |
| | | 93 | | |
| | | 94 | | // Update SliceFlags in case they were updated. |
| | 491 | 95 | | _classContext.Current.SliceFlagsPlaceholder.Span[0] = (byte)_classContext.Current.SliceFlags; |
| | | 96 | | |
| | | 97 | | // If this is the last slice in an exception, reset the current context. |
| | 491 | 98 | | if (lastSlice && _classContext.Current.InstanceType == InstanceType.Exception) |
| | 31 | 99 | | { |
| | 31 | 100 | | _classContext.Current = default; |
| | 31 | 101 | | } |
| | 491 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary>Marks the start of the encoding of a class or exception slice.</summary> |
| | | 105 | | /// <param name="typeId">The type ID of this slice.</param> |
| | | 106 | | /// <param name="compactId ">The compact ID of this slice, if any.</param> |
| | | 107 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 108 | | public void StartSlice(string typeId, int? compactId = null) |
| | 491 | 109 | | { |
| | | 110 | | // This will only be called with an InstanceType of 'None' when we're starting to encode the first slice |
| | | 111 | | // of an exception. |
| | 491 | 112 | | if (_classContext.Current.InstanceType == InstanceType.None) |
| | 31 | 113 | | { |
| | 31 | 114 | | _classContext.ClassFormat = ClassFormat.Sliced; // always encode exceptions in sliced format |
| | 31 | 115 | | _classContext.Current.InstanceType = InstanceType.Exception; |
| | 31 | 116 | | _classContext.Current.FirstSlice = true; |
| | 31 | 117 | | } |
| | | 118 | | |
| | 491 | 119 | | _classContext.Current.SliceFlags = default; |
| | 491 | 120 | | _classContext.Current.SliceFlagsPlaceholder = GetPlaceholderMemory(1); |
| | | 121 | | |
| | 491 | 122 | | if (_classContext.ClassFormat == ClassFormat.Sliced) |
| | 151 | 123 | | { |
| | 151 | 124 | | EncodeTypeId(typeId, compactId); |
| | | 125 | | // Encode the slice size if using the sliced format. |
| | 151 | 126 | | _classContext.Current.SliceFlags |= SliceFlags.HasSliceSize; |
| | 151 | 127 | | _classContext.Current.SliceSizeStartPos = EncodedByteCount; // size includes size-length |
| | 151 | 128 | | _classContext.Current.SliceSizePlaceholder = GetPlaceholderMemory(4); |
| | 151 | 129 | | } |
| | 340 | 130 | | else if (_classContext.Current.FirstSlice) |
| | 227 | 131 | | { |
| | 227 | 132 | | EncodeTypeId(typeId, compactId); |
| | 227 | 133 | | } |
| | | 134 | | |
| | 491 | 135 | | if (_classContext.Current.FirstSlice) |
| | 305 | 136 | | { |
| | 305 | 137 | | _classContext.Current.FirstSlice = false; |
| | 305 | 138 | | } |
| | 491 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary>Encodes this class instance inline if not previously encoded, otherwise just encode its instance |
| | | 142 | | /// ID.</summary> |
| | | 143 | | /// <param name="v">The class instance.</param> |
| | | 144 | | private void EncodeInstance(IceClass v) |
| | 295 | 145 | | { |
| | | 146 | | // If the instance was already encoded, just encode its instance ID. |
| | 295 | 147 | | if (_classContext.InstanceMap is not null && _classContext.InstanceMap.TryGetValue(v, out int instanceId)) |
| | 21 | 148 | | { |
| | 21 | 149 | | EncodeSize(instanceId); |
| | 21 | 150 | | } |
| | | 151 | | else |
| | 274 | 152 | | { |
| | 274 | 153 | | _classContext.InstanceMap ??= new Dictionary<IceClass, int>(); |
| | | 154 | | |
| | | 155 | | // We haven't seen this instance previously, so we create a new instance ID and insert the instance |
| | | 156 | | // and its ID in the encoded map, before encoding the instance inline. |
| | | 157 | | // The instance IDs start at 2 (0 means null and 1 means the instance is encoded immediately after). |
| | 274 | 158 | | instanceId = _classContext.InstanceMap.Count + 2; |
| | 274 | 159 | | _classContext.InstanceMap.Add(v, instanceId); |
| | | 160 | | |
| | 274 | 161 | | EncodeSize(1); // Class instance marker. |
| | | 162 | | |
| | | 163 | | // Save _current in case we're encoding a nested instance. |
| | 274 | 164 | | InstanceData previousCurrent = _classContext.Current; |
| | 274 | 165 | | _classContext.Current = default; |
| | 274 | 166 | | _classContext.Current.InstanceType = InstanceType.Class; |
| | 274 | 167 | | _classContext.Current.FirstSlice = true; |
| | | 168 | | |
| | 274 | 169 | | if (v.UnknownSlices.Count > 0 && _classContext.ClassFormat == ClassFormat.Sliced) |
| | 12 | 170 | | { |
| | 12 | 171 | | EncodeUnknownSlices(v.UnknownSlices, fullySliced: v is UnknownIceClass); |
| | 12 | 172 | | _classContext.Current.FirstSlice = false; |
| | 12 | 173 | | } |
| | 274 | 174 | | v.Encode(ref this); |
| | | 175 | | |
| | | 176 | | // Restore previous _current. |
| | 274 | 177 | | _classContext.Current = previousCurrent; |
| | 274 | 178 | | } |
| | 295 | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary>Encodes the type ID or compact ID immediately after the slice flags byte, and updates the slice |
| | | 182 | | /// flags byte as needed.</summary> |
| | | 183 | | /// <param name="typeId">The type ID of the current slice.</param> |
| | | 184 | | /// <param name="compactId">The compact ID of the current slice.</param> |
| | | 185 | | private void EncodeTypeId(string typeId, int? compactId) |
| | 378 | 186 | | { |
| | 378 | 187 | | Debug.Assert(_classContext.Current.InstanceType != InstanceType.None); |
| | | 188 | | |
| | 378 | 189 | | TypeIdKind typeIdKind = TypeIdKind.None; |
| | | 190 | | |
| | 378 | 191 | | if (_classContext.Current.InstanceType == InstanceType.Class) |
| | 339 | 192 | | { |
| | 339 | 193 | | if (compactId is int compactIdValue) |
| | 27 | 194 | | { |
| | 27 | 195 | | typeIdKind = TypeIdKind.CompactId; |
| | 27 | 196 | | EncodeSize(compactIdValue); |
| | 27 | 197 | | } |
| | | 198 | | else |
| | 312 | 199 | | { |
| | 312 | 200 | | int index = RegisterTypeId(typeId); |
| | 312 | 201 | | if (index < 0) |
| | 72 | 202 | | { |
| | 72 | 203 | | typeIdKind = TypeIdKind.String; |
| | 72 | 204 | | EncodeString(typeId); |
| | 72 | 205 | | } |
| | | 206 | | else |
| | 240 | 207 | | { |
| | 240 | 208 | | typeIdKind = TypeIdKind.Index; |
| | 240 | 209 | | EncodeSize(index); |
| | 240 | 210 | | } |
| | 312 | 211 | | } |
| | 339 | 212 | | } |
| | | 213 | | else |
| | 39 | 214 | | { |
| | 39 | 215 | | Debug.Assert(compactId is null); |
| | | 216 | | // We always encode a string and don't set a type ID kind in SliceFlags. |
| | 39 | 217 | | EncodeString(typeId); |
| | 39 | 218 | | } |
| | | 219 | | |
| | 378 | 220 | | _classContext.Current.SliceFlags |= (SliceFlags)typeIdKind; |
| | 378 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary>Encodes sliced-off slices.</summary> |
| | | 224 | | /// <param name="unknownSlices">The sliced-off slices to encode.</param> |
| | | 225 | | /// <param name="fullySliced">When <see langword="true" />, slicedData holds all the data of this instance.</param> |
| | | 226 | | private void EncodeUnknownSlices(ImmutableList<SliceInfo> unknownSlices, bool fullySliced) |
| | 12 | 227 | | { |
| | 12 | 228 | | Debug.Assert(_classContext.Current.InstanceType != InstanceType.None); |
| | | 229 | | |
| | | 230 | | // We only re-encode preserved slices if we are using the sliced format. Otherwise, we ignore the preserved |
| | | 231 | | // slices, which essentially "slices" the instance into the most-derived type known by the sender. |
| | 12 | 232 | | if (_classContext.ClassFormat != ClassFormat.Sliced) |
| | 0 | 233 | | { |
| | 0 | 234 | | throw new NotSupportedException( |
| | 0 | 235 | | $"Cannot encode sliced data into payload using {_classContext.ClassFormat} format."); |
| | | 236 | | } |
| | | 237 | | |
| | 72 | 238 | | for (int i = 0; i < unknownSlices.Count; ++i) |
| | 24 | 239 | | { |
| | 24 | 240 | | SliceInfo sliceInfo = unknownSlices[i]; |
| | | 241 | | |
| | | 242 | | // If type ID is a compact ID, extract it. |
| | 24 | 243 | | int? compactId = null; |
| | 24 | 244 | | if (!sliceInfo.TypeId.StartsWith("::", StringComparison.Ordinal)) |
| | 8 | 245 | | { |
| | | 246 | | try |
| | 8 | 247 | | { |
| | 8 | 248 | | compactId = int.Parse(sliceInfo.TypeId, CultureInfo.InvariantCulture); |
| | 8 | 249 | | } |
| | 0 | 250 | | catch (FormatException exception) |
| | 0 | 251 | | { |
| | 0 | 252 | | throw new InvalidDataException($"Received invalid type ID {sliceInfo.TypeId}.", exception); |
| | | 253 | | } |
| | 8 | 254 | | } |
| | | 255 | | |
| | 24 | 256 | | StartSlice(sliceInfo.TypeId, compactId); |
| | | 257 | | |
| | | 258 | | // Writes the bytes associated with this slice. |
| | 24 | 259 | | WriteByteSpan(sliceInfo.Bytes.Span); |
| | | 260 | | |
| | 24 | 261 | | if (sliceInfo.HasTaggedFields) |
| | 4 | 262 | | { |
| | 4 | 263 | | _classContext.Current.SliceFlags |= SliceFlags.HasTaggedFields; |
| | 4 | 264 | | } |
| | | 265 | | |
| | | 266 | | // Make sure to also encode the instance indirection table. |
| | | 267 | | // These instances will be encoded (and assigned instance IDs) in EndSlice. |
| | 24 | 268 | | if (sliceInfo.Instances.Count > 0) |
| | 8 | 269 | | { |
| | 8 | 270 | | _classContext.Current.IndirectionTable ??= new List<IceClass>(); |
| | 8 | 271 | | Debug.Assert(_classContext.Current.IndirectionTable.Count == 0); |
| | 8 | 272 | | _classContext.Current.IndirectionTable.AddRange(sliceInfo.Instances); |
| | 8 | 273 | | } |
| | 24 | 274 | | EndSlice(lastSlice: fullySliced && (i == unknownSlices.Count - 1)); |
| | 24 | 275 | | } |
| | 12 | 276 | | } |
| | | 277 | | |
| | | 278 | | /// <summary>Registers or looks up a type ID in the _typeIdMap.</summary> |
| | | 279 | | /// <param name="typeId">The type ID to register or lookup.</param> |
| | | 280 | | /// <returns>The index in _typeIdMap if this type ID was previously registered; otherwise, -1.</returns> |
| | | 281 | | private int RegisterTypeId(string typeId) |
| | 312 | 282 | | { |
| | 312 | 283 | | _classContext.TypeIdMap ??= new Dictionary<string, int>(); |
| | | 284 | | |
| | 312 | 285 | | if (_classContext.TypeIdMap.TryGetValue(typeId, out int index)) |
| | 240 | 286 | | { |
| | 240 | 287 | | return index; |
| | | 288 | | } |
| | | 289 | | else |
| | 72 | 290 | | { |
| | 72 | 291 | | index = _classContext.TypeIdMap.Count + 1; |
| | 72 | 292 | | _classContext.TypeIdMap.Add(typeId, index); |
| | 72 | 293 | | return -1; |
| | | 294 | | } |
| | 312 | 295 | | } |
| | | 296 | | |
| | | 297 | | private struct ClassContext |
| | | 298 | | { |
| | | 299 | | // The current class/exception format, can be either Compact or Iced. |
| | | 300 | | internal ClassFormat ClassFormat; |
| | | 301 | | |
| | | 302 | | // Data for the class or exception instance that is currently getting encoded. |
| | | 303 | | internal InstanceData Current; |
| | | 304 | | |
| | | 305 | | // Map of class instance to instance ID, where the instance IDs start at 2. |
| | | 306 | | // - Instance ID = 0 means null. |
| | | 307 | | // - Instance ID = 1 means the instance is encoded inline afterwards. |
| | | 308 | | // - Instance ID > 1 means a reference to a previously encoded instance, found in this map. |
| | | 309 | | internal Dictionary<IceClass, int>? InstanceMap; |
| | | 310 | | |
| | | 311 | | // Map of type ID string to type ID index. |
| | | 312 | | // We assign a type ID index (starting with 1) to each type ID we write, in order. |
| | | 313 | | internal Dictionary<string, int>? TypeIdMap; |
| | | 314 | | |
| | | 315 | | internal ClassContext(ClassFormat classFormat) |
| | 6460 | 316 | | : this() => ClassFormat = classFormat; |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | private struct InstanceData |
| | | 320 | | { |
| | | 321 | | // The following fields are used and reused for all the slices of a class or exception instance. |
| | | 322 | | |
| | | 323 | | internal InstanceType InstanceType; |
| | | 324 | | |
| | | 325 | | // The following fields are used for the current slice: |
| | | 326 | | |
| | | 327 | | internal bool FirstSlice; |
| | | 328 | | |
| | | 329 | | // The indirection map and indirection table are only used for the sliced format. |
| | | 330 | | internal Dictionary<IceClass, int>? IndirectionMap; |
| | | 331 | | internal List<IceClass>? IndirectionTable; |
| | | 332 | | |
| | | 333 | | internal SliceFlags SliceFlags; |
| | | 334 | | |
| | | 335 | | // The Ice flags byte. |
| | | 336 | | internal Memory<byte> SliceFlagsPlaceholder; |
| | | 337 | | |
| | | 338 | | // The place holder for the Slice size. Used only for the sliced format. |
| | | 339 | | internal Memory<byte> SliceSizePlaceholder; |
| | | 340 | | |
| | | 341 | | // The starting position for computing the size of the slice. It's just before the SliceSizePlaceholder as |
| | | 342 | | // the size includes the size length. |
| | | 343 | | internal int SliceSizeStartPos; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | private enum InstanceType : byte |
| | | 347 | | { |
| | | 348 | | None = 0, |
| | | 349 | | Class, |
| | | 350 | | Exception |
| | | 351 | | } |
| | | 352 | | } |