示例#1
0
        ValueTuple IFieldCodec <ValueTuple> .ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType != WireType.VarInt)
            {
                ThrowUnsupportedWireTypeException();
            }

            ReferenceCodec.MarkValueField(reader.Session);
            _ = reader.ReadVarUInt64();

            return(default);
示例#2
0
        /// <inheritdoc/>
        void IFieldCodec <KeyValuePair <TKey, TValue> > .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer,
                                                                                    uint fieldIdDelta,
                                                                                    Type expectedType,
                                                                                    KeyValuePair <TKey, TValue> value)
        {
            ReferenceCodec.MarkValueField(writer.Session);
            writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited);

            _keyCodec.WriteField(ref writer, 0, CodecKeyType, value.Key);
            _valueCodec.WriteField(ref writer, 1, CodecValueType, value.Value);

            writer.WriteEndObject();
        }
示例#3
0
        public static void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, decimal value) where TBufferWriter : IBufferWriter <byte>
        {
            ReferenceCodec.MarkValueField(writer.Session);
            writer.WriteFieldHeader(fieldIdDelta, expectedType, CodecFieldType, WireType.LengthPrefixed);
            writer.WriteVarUInt32(Width);
            var holder = new DecimalConverter
            {
                Value = value
            };

            writer.WriteUInt64(holder.First);
            writer.WriteUInt64(holder.Second);
        }
示例#4
0
        public static void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, T value) where TBufferWriter : IBufferWriter <byte>
        {
            ReferenceCodec.MarkValueField(writer.Session);
            writer.WriteFieldHeader(fieldIdDelta, expectedType, CodecFieldType, WireType.Fixed32);
            var holder = new HolderStruct
            {
                Value = value
            };

            var intValue = Unsafe.As <T, int>(ref holder.Value);

            writer.WriteInt32(intValue);
        }
示例#5
0
        public static T ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            ReferenceCodec.MarkValueField(reader.Session);
            if (field.WireType != WireType.Fixed32)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            var intValue = reader.ReadInt32();
            var result   = Unsafe.As <int, T>(ref intValue);

            return(result);
        }
示例#6
0
 /// <summary>
 /// Writes a field.
 /// </summary>
 /// <typeparam name="TBufferWriter">The buffer writer type.</typeparam>
 /// <param name="writer">The writer.</param>
 /// <param name="fieldIdDelta">The field identifier delta.</param>
 /// <param name="expectedType">The expected type.</param>
 /// <param name="value">The value.</param>
 /// <param name="actualType">The actual type.</param>
 public static void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, uint value, Type actualType) where TBufferWriter : IBufferWriter <byte>
 {
     ReferenceCodec.MarkValueField(writer.Session);
     if (value > 1 << 20)
     {
         writer.WriteFieldHeader(fieldIdDelta, expectedType, actualType, WireType.Fixed32);
         writer.WriteUInt32(value);
     }
     else
     {
         writer.WriteFieldHeader(fieldIdDelta, expectedType, actualType, WireType.VarInt);
         writer.WriteVarUInt32(value);
     }
 }
示例#7
0
        public static void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, Guid value) where TBufferWriter : IBufferWriter <byte>
        {
            ReferenceCodec.MarkValueField(writer.Session);
            writer.WriteFieldHeader(fieldIdDelta, expectedType, typeof(Guid), WireType.LengthPrefixed);
            writer.WriteVarUInt32(Width);
#if NETCOREAPP3_1_OR_GREATER
            writer.EnsureContiguous(Width);
            if (value.TryWriteBytes(writer.WritableSpan))
            {
                writer.AdvanceSpan(Width);
                return;
            }
#endif
            writer.Write(value.ToByteArray());
        }
示例#8
0
 /// <inheritdoc/>
 void IFieldCodec <int> .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer,
                                                    uint fieldIdDelta,
                                                    Type expectedType,
                                                    int value)
 {
     ReferenceCodec.MarkValueField(writer.Session);
     if (value > 1 << 20 || -value > 1 << 20)
     {
         writer.WriteFieldHeader(fieldIdDelta, expectedType, CodecFieldType, WireType.Fixed32);
         writer.WriteInt32(value);
     }
     else
     {
         writer.WriteFieldHeader(fieldIdDelta, expectedType, CodecFieldType, WireType.VarInt);
         writer.WriteVarInt32(value);
     }
 }
示例#9
0
        public static double ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            ReferenceCodec.MarkValueField(reader.Session);
            switch (field.WireType)
            {
            case WireType.Fixed32:
                return(FloatCodec.ReadFloatRaw(ref reader));

            case WireType.Fixed64:
                return(ReadDoubleRaw(ref reader));

            case WireType.LengthPrefixed:
                return((double)DecimalCodec.ReadDecimalRaw(ref reader));

            default:
                ThrowWireTypeOutOfRange(field.WireType);
                return(0);
            }
        }
示例#10
0
        /// <inheritdoc/>
        public KeyValuePair <TKey, TValue> ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType != WireType.TagDelimited)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            ReferenceCodec.MarkValueField(reader.Session);
            var  key     = default(TKey);
            var  value   = default(TValue);
            uint fieldId = 0;

            while (true)
            {
                var header = reader.ReadFieldHeader();
                if (header.IsEndBaseOrEndObject)
                {
                    break;
                }

                fieldId += header.FieldIdDelta;
                switch (fieldId)
                {
                case 0:
                    key = _keyCodec.ReadValue(ref reader, header);
                    break;

                case 1:
                    value = _valueCodec.ReadValue(ref reader, header);
                    break;

                default:
                    reader.ConsumeUnknownField(header);
                    break;
                }
            }

            return(new KeyValuePair <TKey, TValue>(key, value));
        }
示例#11
0
        public static DateTimeOffset ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            ReferenceCodec.MarkValueField(reader.Session);
            if (field.WireType != WireType.TagDelimited)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            uint     fieldId  = 0;
            TimeSpan offset   = default;
            DateTime dateTime = default;

            while (true)
            {
                var header = reader.ReadFieldHeader();
                if (header.IsEndBaseOrEndObject)
                {
                    break;
                }

                fieldId += header.FieldIdDelta;
                switch (fieldId)
                {
                case 0:
                    dateTime = DateTimeCodec.ReadValue(ref reader, header);
                    break;

                case 1:
                    offset = TimeSpanCodec.ReadValue(ref reader, header);
                    break;

                default:
                    reader.ConsumeUnknownField(header);
                    break;
                }
            }

            return(new DateTimeOffset(dateTime, offset));
        }
示例#12
0
 public static ulong ReadValue <TInput>(ref Reader <TInput> reader, Field field)
 {
     ReferenceCodec.MarkValueField(reader.Session);
     return(reader.ReadUInt64(field.WireType));
 }
示例#13
0
 public static void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, BitVector32 value) where TBufferWriter : IBufferWriter <byte>
 {
     ReferenceCodec.MarkValueField(writer.Session);
     writer.WriteFieldHeader(fieldIdDelta, expectedType, typeof(BitVector32), WireType.Fixed32);
     writer.WriteInt32(value.Data);  // BitVector32.Data gets the value of the BitVector32 as an Int32
 }
        /// <inheritdoc />
        public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, object value) where TBufferWriter : IBufferWriter <byte>
        {
            uint           type;
            CompareOptions compareOptions = default;
            CompareInfo    compareInfo    = default;

            if (value is null)
            {
                type = 0;
            }
            else
            {
#if NET6_0_OR_GREATER
                var comparer = (IEqualityComparer <string>)value;
                if (StringComparer.IsWellKnownOrdinalComparer(comparer, out var ignoreCase))
                {
                    // Ordinal. This also handles EqualityComparer<string>.Default.
                    type = 1;
                    if (ignoreCase)
                    {
                        compareOptions = CompareOptions.IgnoreCase;
                    }
                }
                else if (StringComparer.IsWellKnownCultureAwareComparer(comparer, out compareInfo, out compareOptions))
                {
                    type = 2;
                }
                else
                {
                    ThrowNotSupported(value.GetType());
                    return;
                }
#else
                var isOrdinal           = _ordinalComparer.Equals(value) || _defaultEqualityComparer.Equals(value);
                var isOrdinalIgnoreCase = _ordinalIgnoreCaseComparer.Equals(value);
                if (isOrdinal)
                {
                    type = 1;
                }
                else if (isOrdinalIgnoreCase)
                {
                    type           = 1;
                    compareOptions = CompareOptions.IgnoreCase;
                }
                else if (TryGetWellKnownCultureAwareComparerInfo(value, out compareInfo, out compareOptions, out var ignoreCase))
                {
                    type = 2;
                    if (ignoreCase)
                    {
                        compareOptions |= CompareOptions.IgnoreCase;
                    }
                }
                else
                {
                    ThrowNotSupported(value.GetType());
                    return;
                }
#endif
            }

            ReferenceCodec.MarkValueField(writer.Session);
            writer.WriteFieldHeader(fieldIdDelta, expectedType, typeof(WellKnownStringComparerCodec), WireType.TagDelimited);

            UInt32Codec.WriteField(ref writer, 0, typeof(int), type);
            UInt64Codec.WriteField(ref writer, 1, typeof(ulong), (ulong)compareOptions);

            if (compareInfo is not null)
            {
                Int32Codec.WriteField(ref writer, 1, typeof(int), compareInfo.LCID);
            }

            writer.WriteEndObject();
        }
示例#15
0
 /// <summary>
 /// Writes a field.
 /// </summary>
 /// <typeparam name="TBufferWriter">The buffer writer type.</typeparam>
 /// <param name="writer">The writer.</param>
 /// <param name="fieldIdDelta">The field identifier delta.</param>
 /// <param name="expectedType">The expected type.</param>
 /// <param name="value">The value.</param>
 public static void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, TimeSpan value) where TBufferWriter : IBufferWriter <byte>
 {
     ReferenceCodec.MarkValueField(writer.Session);
     writer.WriteFieldHeader(fieldIdDelta, expectedType, CodecFieldType, WireType.Fixed64);
     writer.WriteInt64(value.Ticks);
 }
示例#16
0
        /// <summary>
        /// Reads a value.
        /// </summary>
        /// <typeparam name="TInput">The reader input type.</typeparam>
        /// <param name="reader">The reader.</param>
        /// <param name="field">The field.</param>
        /// <returns>The value.</returns>
        public static Type ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <Type, TInput>(ref reader, field));
            }

            var  placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            uint fieldId    = 0;
            var  schemaType = default(SchemaType);
            uint id         = 0;
            Type result     = null;

            while (true)
            {
                var header = reader.ReadFieldHeader();
                if (header.IsEndBaseOrEndObject)
                {
                    break;
                }

                ReferenceCodec.MarkValueField(reader.Session);
                fieldId += header.FieldIdDelta;
                switch (fieldId)
                {
                case 0:
                    schemaType = (SchemaType)reader.ReadVarUInt32();
                    break;

                case 1:
                    result = reader.Session.TypeCodec.ReadLengthPrefixed(ref reader);
                    break;

                case 2:
                    id = reader.ReadVarUInt32();
                    break;

                default:
                    reader.ConsumeUnknownField(header);
                    break;
                }
            }

            switch (schemaType)
            {
            case SchemaType.Referenced:
                if (reader.Session.ReferencedTypes.TryGetReferencedType(id, out result))
                {
                    break;
                }

                return(ThrowUnknownReferencedType(id));

            case SchemaType.WellKnown:
                if (reader.Session.WellKnownTypes.TryGetWellKnownType(id, out result))
                {
                    break;
                }

                return(ThrowUnknownWellKnownType(id));

            case SchemaType.Encoded:
                if (result is not null)
                {
                    break;
                }

                return(ThrowMissingType());

            default:
                return(ThrowInvalidSchemaType(schemaType));
            }

            ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
            return(result);
        }
        /// <inheritdoc />
        public object ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            ReferenceCodec.MarkValueField(reader.Session);
            uint           type    = default;
            CompareOptions options = default;
            int            lcid    = default;
            uint           fieldId = 0;

            while (true)
            {
                var header = reader.ReadFieldHeader();
                if (header.IsEndBaseOrEndObject)
                {
                    break;
                }

                fieldId += header.FieldIdDelta;
                switch (fieldId)
                {
                case 0:
                    type = UInt32Codec.ReadValue(ref reader, header);
                    break;

                case 1:
                    options = (CompareOptions)UInt64Codec.ReadValue(ref reader, header);
                    break;

                case 2:
                    lcid = Int32Codec.ReadValue(ref reader, header);
                    break;

                default:
                    reader.ConsumeUnknownField(header);
                    break;
                }
            }

            if (type == 0)
            {
                return(null);
            }
            else if (type == 1)
            {
                if (options.HasFlag(CompareOptions.IgnoreCase))
                {
                    return(StringComparer.OrdinalIgnoreCase);
                }
                else
                {
                    return(StringComparer.Ordinal);
                }
            }
            else if (type == 2)
            {
                if (lcid == CultureInfo.InvariantCulture.LCID)
                {
                    if (options == CompareOptions.None)
                    {
                        return(StringComparer.InvariantCulture);
                    }
                    else if (options == CompareOptions.IgnoreCase)
                    {
                        return(StringComparer.InvariantCultureIgnoreCase);
                    }

                    // Otherwise, perhaps some other options were specified, in which case we fall-through to create a new comparer.
                }

                var cultureInfo = CultureInfo.GetCultureInfo(lcid);
                var result      = StringComparer.Create(cultureInfo, options);
                return(result);
            }

            ThrowNotSupported(field, type);
            return(null);
        }
示例#18
0
 /// <summary>
 /// Writes a field.
 /// </summary>
 /// <typeparam name="TBufferWriter">The buffer writer type.</typeparam>
 /// <param name="writer">The writer.</param>
 /// <param name="fieldIdDelta">The field identifier delta.</param>
 /// <param name="expectedType">The expected type.</param>
 /// <param name="value">The value.</param>
 public static void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, char value) where TBufferWriter : IBufferWriter <byte>
 {
     ReferenceCodec.MarkValueField(writer.Session);
     writer.WriteFieldHeader(fieldIdDelta, expectedType, CodecFieldType, WireType.VarInt);
     writer.WriteVarUInt32(value);
 }
示例#19
0
 void IFieldCodec <ValueTuple> .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, ValueTuple value)
 {
     ReferenceCodec.MarkValueField(writer.Session);
     writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.VarInt);
     writer.WriteVarUInt32(0);
 }
示例#20
0
 public static char ReadValue <TInput>(ref Reader <TInput> reader, Field field)
 {
     ReferenceCodec.MarkValueField(reader.Session);
     return((char)reader.ReadUInt16(field.WireType));
 }
示例#21
0
 /// <summary>
 /// Writes a field.
 /// </summary>
 /// <typeparam name="TBufferWriter">The buffer writer type.</typeparam>
 /// <param name="writer">The writer.</param>
 /// <param name="fieldIdDelta">The field identifier delta.</param>
 /// <param name="expectedType">The expected type.</param>
 /// <param name="value">The value.</param>
 /// <param name="actualType">The actual type.</param>
 public static void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, short value, Type actualType) where TBufferWriter : IBufferWriter <byte>
 {
     ReferenceCodec.MarkValueField(writer.Session);
     writer.WriteFieldHeader(fieldIdDelta, expectedType, actualType, WireType.VarInt);
     writer.WriteVarInt16(value);
 }