Example #1
0
        /// <summary>
        /// Applies a protocol-buffer stream to an existing instance (or null), using length-prefixed
        /// data - useful with network IO.
        /// </summary>
        /// <param name="type">The type being merged.</param>
        /// <param name="value">The existing instance to be modified (can be null).</param>
        /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
        /// <param name="style">How to encode the length prefix.</param>
        /// <param name="expectedField">The tag used as a prefix to each record (only used with base-128 style prefixes).</param>
        /// <param name="resolver">Used to resolve types on a per-field basis.</param>
        /// <param name="bytesRead">Returns the number of bytes consumed by this operation (includes length-prefix overheads and any skipped data).</param>
        /// <returns>The updated instance; this may be different to the instance argument if
        /// either the original instance was null, or the stream defines a known sub-type of the
        /// original instance.</returns>
        public object DeserializeWithLengthPrefix(Stream source, object value, Type type, PrefixStyle style, int expectedField, Serializer.TypeResolver resolver, out int bytesRead)
        {
            bool skip;
            int  len;
            int  tmpBytesRead;

            bytesRead = 0;
            do
            {
                int  actualField;
                bool expectPrefix = expectedField > 0 || resolver != null;
                len        = ProtoReader.ReadLengthPrefix(source, expectPrefix, style, out actualField, out tmpBytesRead);
                bytesRead += tmpBytesRead;
                if (len < 0)
                {
                    return(value);
                }

                if (expectedField == 0 && type == null && resolver != null)
                {
                    type = resolver(actualField);
                    skip = type == null;
                }
                else
                {
                    skip = expectedField != actualField;
                }

                if (skip)
                {
                    if (len == int.MaxValue)
                    {
                        throw new InvalidOperationException();
                    }
                    ProtoReader.Seek(source, len, null);
                    bytesRead += len;
                }
            } while (skip);

            int key = GetKey(ref type);

            if (key < 0)
            {
                throw new InvalidOperationException();
            }
            using (ProtoReader reader = new ProtoReader(source, this, len))
            {
                object result = Deserialize(key, value, reader);
                bytesRead += reader.Position;
                return(result);
            }
        }
Example #2
0
        private object DeserializeWithLengthPrefix(Stream source, object value, Type type, PrefixStyle style, int expectedField, Serializer.TypeResolver resolver, out int bytesRead, out bool haveObject, SerializationContext context)
        {
            haveObject = false;
            bytesRead  = 0;
            if (type == null && (style != PrefixStyle.Base128 || resolver == null))
            {
                throw new InvalidOperationException("A type must be provided unless base-128 prefixing is being used in combination with a resolver");
            }
            while (true)
            {
                bool expectPrefix = expectedField > 0 || resolver != null;
                int  actualField;
                int  tmpBytesRead;
                int  len = ProtoReader.ReadLengthPrefix(source, expectPrefix, style, out actualField, out tmpBytesRead);
                if (tmpBytesRead == 0)
                {
                    break;
                }
                bytesRead += tmpBytesRead;
                if (len < 0)
                {
                    return(value);
                }
                bool skip;
                if (style == PrefixStyle.Base128)
                {
                    if (expectPrefix && expectedField == 0 && type == null && resolver != null)
                    {
                        type = resolver(actualField);
                        skip = (type == null);
                    }
                    else
                    {
                        skip = (expectedField != actualField);
                    }
                }
                else
                {
                    skip = false;
                }
                if (skip)
                {
                    if (len == 2147483647)
                    {
                        goto Block_12;
                    }
                    ProtoReader.Seek(source, len, null);
                    bytesRead += len;
                }
                if (!skip)
                {
                    goto Block_13;
                }
            }
            return(value);

Block_12:
            throw new InvalidOperationException();
Block_13:
            ProtoReader reader = null;
            object result;

            try
            {
                int len;
                reader = ProtoReader.Create(source, this, context, len);
                int key = this.GetKey(ref type);
                if (key >= 0 && !Helpers.IsEnum(type))
                {
                    value = this.Deserialize(key, value, reader);
                }
                else
                {
                    if (!this.TryDeserializeAuxiliaryType(reader, DataFormat.Default, 1, type, ref value, true, false, true, false) && len != 0)
                    {
                        TypeModel.ThrowUnexpectedType(type);
                    }
                }
                bytesRead += reader.Position;
                haveObject = true;
                result     = value;
            }
            finally
            {
                ProtoReader.Recycle(reader);
            }
            return(result);
        }
Example #3
0
        private object DeserializeWithLengthPrefix(Stream source, object value, Type type, PrefixStyle style, int expectedField, Serializer.TypeResolver resolver, out int bytesRead, out bool haveObject, SerializationContext context)
        {
            haveObject = false;
            bytesRead  = 0;
            if (type == null && (style != PrefixStyle.Base128 || resolver == null))
            {
                throw new InvalidOperationException("A type must be provided unless base-128 prefixing is being used in combination with a resolver");
            }
            int  num;
            bool flag2;

            do
            {
                bool flag = expectedField > 0 || resolver != null;
                num = ProtoReader.ReadLengthPrefix(source, flag, style, out int fieldNumber, out int bytesRead2);
                if (bytesRead2 == 0)
                {
                    return(value);
                }
                bytesRead += bytesRead2;
                if (num < 0)
                {
                    return(value);
                }
                if (style == PrefixStyle.Base128)
                {
                    if (flag && expectedField == 0 && type == null && resolver != null)
                    {
                        type  = resolver(fieldNumber);
                        flag2 = (type == null);
                    }
                    else
                    {
                        flag2 = (expectedField != fieldNumber);
                    }
                }
                else
                {
                    flag2 = false;
                }
                if (flag2)
                {
                    if (num == int.MaxValue)
                    {
                        throw new InvalidOperationException();
                    }
                    ProtoReader.Seek(source, num, null);
                    bytesRead += num;
                }
            }while (flag2);
            ProtoReader protoReader = null;

            try
            {
                protoReader = ProtoReader.Create(source, this, context, num);
                int key = GetKey(ref type);
                if (key >= 0 && !Helpers.IsEnum(type))
                {
                    value = Deserialize(key, value, protoReader);
                }
                else if (!TryDeserializeAuxiliaryType(protoReader, DataFormat.Default, 1, type, ref value, skipOtherFields: true, asListItem: false, autoCreate: true, insideList: false) && num != 0)
                {
                    ThrowUnexpectedType(type);
                }
                bytesRead += protoReader.Position;
                haveObject = true;
                return(value);
            }
            finally
            {
                ProtoReader.Recycle(protoReader);
            }
        }
Example #4
0
        private object DeserializeWithLengthPrefix(Stream source, object value, Type type, PrefixStyle style, int expectedField, ProtobufSerializer.TypeResolver resolver, out int bytesRead, out bool haveObject, SerializationContext context)
        {
            bool flag;
            int  num;

            haveObject = false;
            bytesRead  = 0;
            if ((type == null) && ((style != PrefixStyle.Base128) || (resolver == null)))
            {
                throw new InvalidOperationException("A type must be provided unless base-128 prefixing is being used in combination with a resolver");
            }
            do
            {
                int  num2;
                int  num3;
                bool expectHeader = (expectedField > 0) || (resolver != null);
                num = ProtoReader.ReadLengthPrefix(source, expectHeader, style, out num3, out num2);
                if (num2 == 0)
                {
                    return(value);
                }
                bytesRead += num2;
                if (num < 0)
                {
                    return(value);
                }
                if (style == PrefixStyle.Base128)
                {
                    if ((expectHeader && (expectedField == 0)) && ((type == null) && (resolver != null)))
                    {
                        type = resolver(num3);
                        flag = type == null;
                    }
                    else
                    {
                        flag = expectedField != num3;
                    }
                }
                else
                {
                    flag = false;
                }
                if (flag)
                {
                    if (num == 0x7fffffff)
                    {
                        throw new InvalidOperationException();
                    }
                    ProtoReader.Seek(source, num, null);
                    bytesRead += num;
                }
            }while (flag);
            using (ProtoReader reader = new ProtoReader(source, this, context, num))
            {
                int key = this.GetKey(ref type);
                if ((key >= 0) && !Helpers.IsEnum(type))
                {
                    value = this.Deserialize(key, value, reader);
                }
                else if (!this.TryDeserializeAuxiliaryType(reader, DataFormat.Default, 1, type, ref value, true, false, true, false) && (num != 0))
                {
                    ThrowUnexpectedType(type);
                }
                bytesRead += reader.Position;
                haveObject = true;
                return(value);
            }
        }