/// <summary> /// Reads a known DescribedType object from the buffer given the already read descriptor. /// </summary> /// <param name="buffer"></param> /// <param name="descriptor"></param> public static object DecodeKnownDescribedType(ByteBuffer buffer, Descriptor descriptor) { Func <object> ctor; if (DescribedTypeCodec.TryGetKnownDescribedConstructor(descriptor.Code, out ctor)) { var instance = ctor() as DescribedType; instance.Decode(buffer); return(instance); } else { throw new AmqpException(ErrorCode.DecodeError, $"Missing Constructor For Known Described Type {descriptor.ToString()}"); } }
/// <summary> /// Reads a Descriptor and DescribedType object from the buffer. /// </summary> /// <param name="buffer"></param> public static object DecodeDescribedType(ByteBuffer buffer, byte formatCode) { if (formatCode != FormatCode.Described) { throw new ArgumentException(nameof(formatCode), "Format code must be described (0x00)"); } var descriptor = DecodeDescriptor(buffer); if (DescribedTypeCodec.IsKnownDescribedType(descriptor)) { return(DecodeKnownDescribedType(buffer, descriptor)); } object value = DecodeBoxedObject(buffer); // TODO: performance. boxing return(DescribedTypeCodec.GetDescribedTypeConstructor(value.GetType())(descriptor, value)); }
/// <summary> /// Reads a described type descriptor from the buffer. It may return a known static Descriptor. /// </summary> /// <param name="buffer"></param> public static Descriptor DecodeDescriptor(ByteBuffer buffer) { var descriptorFormatCode = DecodeFormatCode(buffer); if (descriptorFormatCode == FormatCode.ULong || descriptorFormatCode == FormatCode.SmallULong) { ulong code = Encoder.ReadULong(buffer, descriptorFormatCode); Descriptor descriptor = null; if (DescribedTypeCodec.TryGetKnownDescribedType(code, out descriptor)) { return(descriptor); } return(new Descriptor(code)); } if (descriptorFormatCode == FormatCode.Symbol8 || descriptorFormatCode == FormatCode.Symbol32) { string symbol = Encoder.ReadSymbol(buffer, descriptorFormatCode); return(new Descriptor(symbol)); } throw new AmqpException(ErrorCode.FramingError, $"Invalid Descriptor Format Code{descriptorFormatCode.ToHex()}"); }