コード例 #1
0
        /// <summary>Read the VarInt length prefix and the given number of bytes from the stream and deserialze it into the instance.</summary>
        public static Example.Person DeserializeLengthDelimited(Stream stream, Example.Person instance)
        {
            long limit = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream);

            limit += stream.Position;
            while (true)
            {
                if (stream.Position >= limit)
                {
                    if (stream.Position == limit)
                    {
                        break;
                    }
                    else
                    {
                        throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException("Read past max limit");
                    }
                }
                int keyByte = stream.ReadByte();
                if (keyByte == -1)
                {
                    throw new System.IO.EndOfStreamException();
                }
                // Optimized reading of known fields with field ID < 16
                switch (keyByte)
                {
                // Field 1 LengthDelimited
                case 10:
                    instance.FirstName = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadString(stream);
                    continue;

                // Field 2 LengthDelimited
                case 18:
                    instance.LastName = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadString(stream);
                    continue;

                // Field 3 Varint
                case 24:
                    instance.Age = (int)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                    continue;
                }

                var key = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadKey((byte)keyByte, stream);

                // Reading field ID > 16 and unknown field ID/wire type combinations
                switch (key.Field)
                {
                case 0:
                    throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException("Invalid field id: 0, something went wrong in the stream");

                default:
                    global::SilentOrbit.ProtocolBuffers.ProtocolParser.SkipKey(stream, key);
                    break;
                }
            }

            return(instance);
        }
コード例 #2
0
        /// <summary>Takes the remaining content of the stream and deserialze it into the instance.</summary>
        public static Example.Person Deserialize(Stream stream, Example.Person instance)
        {
            if (instance.Phone == null)
            {
                instance.Phone = new List <Example.Person.PhoneNumber>();
            }
            while (true)
            {
                int keyByte = stream.ReadByte();
                if (keyByte == -1)
                {
                    break;
                }
                // Optimized reading of known fields with field ID < 16
                switch (keyByte)
                {
                // Field 1 LengthDelimited
                case 10:
                    instance.Name = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadString(stream);
                    continue;

                // Field 2 Varint
                case 16:
                    instance.Id = (int)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                    continue;

                // Field 3 LengthDelimited
                case 26:
                    instance.Email = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadString(stream);
                    continue;

                // Field 4 LengthDelimited
                case 34:
                    // repeated
                    instance.Phone.Add(Example.Person.PhoneNumber.DeserializeLengthDelimited(stream));
                    continue;
                }

                var key = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadKey((byte)keyByte, stream);

                // Reading field ID > 16 and unknown field ID/wire type combinations
                switch (key.Field)
                {
                case 0:
                    throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException("Invalid field id: 0, something went wrong in the stream");

                default:
                    global::SilentOrbit.ProtocolBuffers.ProtocolParser.SkipKey(stream, key);
                    break;
                }
            }

            return(instance);
        }
コード例 #3
0
 /// <summary>Helper: put the buffer into a MemoryStream before deserializing</summary>
 public static Example.Person Deserialize(byte[] buffer, Example.Person instance)
 {
     using (var ms = new MemoryStream(buffer))
         Deserialize(ms, instance);
     return(instance);
 }