Ejemplo n.º 1
0
        public static IPAddress ReadValue <TInput>(ref Buffers.Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return((IPAddress)ReferenceCodec.ReadReference(ref reader, field, CodecFieldType));
            }

            var       length = reader.ReadVarUInt32();
            IPAddress result;

#if NET5_0_OR_GREATER
            if (reader.TryReadBytes((int)length, out var bytes))
            {
                result = new IPAddress(bytes);
            }
            else
            {
#endif
            var addressBytes = reader.ReadBytes(length);
            result = new IPAddress(addressBytes);
#if NET5_0_OR_GREATER
        }
#endif

            ReferenceCodec.RecordObject(reader.Session, result);
            return(result);
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public TField ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <TField, TInput>(ref reader, field));
            }

            var placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            var fieldType = field.FieldType;

            if (fieldType is null || fieldType == CodecFieldType)
            {
                TSurrogate surrogate = default;
                _surrogateSerializer.Deserialize(ref reader, ref surrogate);
                var result = ConvertFromSurrogate(ref surrogate);
                ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
                return(result);
            }

            // The type is a descendant, not an exact match, so get the specific serializer for it.
            var specificSerializer = reader.Session.CodecProvider.GetCodec(fieldType);

            if (specificSerializer != null)
            {
                return((TField)specificSerializer.ReadValue(ref reader, field));
            }

            ThrowSerializerNotFoundException(fieldType);
            return(null);
        }
Ejemplo n.º 3
0
        public static string ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <string, TInput>(ref reader, field));
            }

            if (field.WireType != WireType.LengthPrefixed)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            var length = reader.ReadVarUInt32();

            string result;

#if NETCOREAPP3_1_OR_GREATER
            if (reader.TryReadBytes((int)length, out var span))
            {
                result = Encoding.UTF8.GetString(span);
            }
            else
#endif
            {
                var bytes = reader.ReadBytes(length);
                result = Encoding.UTF8.GetString(bytes);
            }

            ReferenceCodec.RecordObject(reader.Session, result);
            return(result);
        }
Ejemplo n.º 4
0
        public Dictionary <TKey, TValue> ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <Dictionary <TKey, TValue>, TInput>(ref reader, field));
            }

            if (field.WireType != WireType.TagDelimited)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            var placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            int length = 0;
            Dictionary <TKey, TValue> result   = null;
            IEqualityComparer <TKey>  comparer = null;
            uint fieldId = 0;

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

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

                case 1:
                    length = Int32Codec.ReadValue(ref reader, header);
                    if (length > 10240 && length > reader.Length)
                    {
                        ThrowInvalidSizeException(length);
                    }

                    break;

                case 2:
                    result ??= CreateInstance(length, comparer, reader.Session, placeholderReferenceId);
                    var pair = _pairCodec.ReadValue(ref reader, header);
                    result.Add(pair.Key, pair.Value);
                    break;

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

            result ??= CreateInstance(length, comparer, reader.Session, placeholderReferenceId);
            return(result);
        }
Ejemplo n.º 5
0
        /// <inheritdoc />
        object IFieldCodec <object> .ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType != WireType.Reference)
            {
                ThrowInvalidWireType(field);
            }

            return(ReferenceCodec.ReadReference <object, TInput>(ref reader, field));
        }
Ejemplo n.º 6
0
        /// <inheritdoc/>
        Queue <T> IFieldCodec <Queue <T> > .ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <Queue <T>, TInput>(ref reader, field));
            }

            if (field.WireType != WireType.TagDelimited)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            var       placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            Queue <T> result  = null;
            uint      fieldId = 0;
            var       length  = 0;
            var       index   = 0;

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

                fieldId += header.FieldIdDelta;
                switch (fieldId)
                {
                case 0:
                    length = Int32Codec.ReadValue(ref reader, header);
                    result = new Queue <T>(length);
                    ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
                    break;

                case 1:
                    if (result is null)
                    {
                        ThrowLengthFieldMissing();
                    }

                    if (index >= length)
                    {
                        ThrowIndexOutOfRangeException(length);
                    }
                    result.Enqueue(_fieldCodec.ReadValue(ref reader, header));
                    ++index;
                    break;

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

            return(result);
        }
Ejemplo n.º 7
0
        /// <inheritdoc/>
        T? IFieldCodec <T?> .ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            // This will only be true if the value is null.
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <T?, TInput>(ref reader, field));
            }

            // Read the non-null value.
            return(_fieldCodec.ReadValue(ref reader, field));
        }
Ejemplo n.º 8
0
        public TField ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <TField, TInput>(ref reader, field));
            }

            var        placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            TSurrogate surrogate = default;

            _surrogateSerializer.Deserialize(ref reader, ref surrogate);
            var result = ConvertFromSurrogate(ref surrogate);

            ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
            return(result);
        }
Ejemplo n.º 9
0
        /// <inheritdoc/>
        public CompareInfo ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <CompareInfo, TInput>(ref reader, field));
            }

            if (field.WireType != WireType.TagDelimited)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            var    placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            uint   fieldId = 0;
            string name    = null;

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

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

                case 1:
                    break;

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

            var result = CompareInfo.GetCompareInfo(name);

            ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
            return(result);
        }
Ejemplo n.º 10
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 byte[] ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <byte[], TInput>(ref reader, field));
            }

            if (field.WireType != WireType.LengthPrefixed)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            var length = reader.ReadVarUInt32();
            var result = reader.ReadBytes(length);

            ReferenceCodec.RecordObject(reader.Session, result);
            return(result);
        }
Ejemplo n.º 11
0
        public static object ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <object, TInput>(ref reader, field));
            }

            if (field.FieldType == ObjectType || field.FieldType is null)
            {
                _ = reader.ReadVarUInt32();
                var result = new object();
                ReferenceCodec.RecordObject(reader.Session, result);
                return(result);
            }

            var specificSerializer = reader.Session.CodecProvider.GetCodec(field.FieldType);

            return(specificSerializer.ReadValue(ref reader, field));
        }
Ejemplo n.º 12
0
        public static IPEndPoint ReadValue <TInput>(ref Buffers.Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return((IPEndPoint)ReferenceCodec.ReadReference(ref reader, field, CodecFieldType));
            }

            var       referencePlaceholder = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            IPAddress address = default;
            ushort    port    = 0;
            int       id      = 0;
            Field     header  = default;

            while (true)
            {
                id = OrleansGeneratedCodeHelper.ReadHeader(ref reader, ref header, id);
                if (id == 1)
                {
                    address = IPAddressCodec.ReadValue(ref reader, header);
                    id      = OrleansGeneratedCodeHelper.ReadHeader(ref reader, ref header, id);
                }

                if (id == 2)
                {
                    port = UInt16Codec.ReadValue(ref reader, header);
                    id   = OrleansGeneratedCodeHelper.ReadHeaderExpectingEndBaseOrEndObject(ref reader, ref header, id);
                }

                if (id != -1)
                {
                    reader.ConsumeUnknownField(header);
                }
                else
                {
                    break;
                }
            }

            var result = new IPEndPoint(address, port);

            ReferenceCodec.RecordObject(reader.Session, result, referencePlaceholder);
            return(result);
        }
Ejemplo n.º 13
0
        Tuple <T> IFieldCodec <Tuple <T> > .ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <Tuple <T>, TInput>(ref reader, field));
            }

            if (field.WireType != WireType.TagDelimited)
            {
                ThrowUnsupportedWireTypeException();
            }

            var  placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            var  item1   = default(T);
            uint fieldId = 0;

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

                fieldId += header.FieldIdDelta;
                switch (fieldId)
                {
                case 1:
                    item1 = _valueCodec.ReadValue(ref reader, header);
                    break;

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

            var result = new Tuple <T>(item1);

            ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
            return(result);
        }
Ejemplo n.º 14
0
        /// <inheritdoc/>
        object IFieldCodec <object> .ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <T[], TInput>(ref reader, field));
            }

            if (field.WireType != WireType.TagDelimited)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            var   placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            Array result  = null;
            uint  fieldId = 0;

            int[] lengths = null;
            int[] indices = null;
            var   rank    = 0;

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

                fieldId += header.FieldIdDelta;
                switch (fieldId)
                {
                case 0:
                {
                    lengths = _intArrayCodec.ReadValue(ref reader, header);
                    rank    = lengths.Length;

                    // Multi-dimensional arrays must be indexed using indexing arrays, so create one now.
                    indices = new int[rank];
                    result  = Array.CreateInstance(CodecElementType, lengths);
                    ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
                    break;
                }

                case 1:
                {
                    if (result is null)
                    {
                        return(ThrowLengthsFieldMissing());
                    }

                    var element = _elementCodec.ReadValue(ref reader, header);
                    result.SetValue(element, indices);

                    // Increment the indices array by 1.
                    var idx = rank - 1;
                    while (idx >= 0 && ++indices[idx] >= lengths[idx])
                    {
                        indices[idx] = 0;
                        --idx;
                    }

                    break;
                }

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

            return(result);
        }
Ejemplo n.º 15
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);
        }