コード例 #1
0
        /// <summary>Takes the remaining content of the stream and deserialze it into the instance.</summary>
        public static Example.KVMessage Deserialize(Stream stream, Example.KVMessage instance)
        {
            while (true)
            {
                int keyByte = stream.ReadByte();
                if (keyByte == -1)
                {
                    break;
                }
                // Optimized reading of known fields with field ID < 16
                switch (keyByte)
                {
                // Field 1 Varint
                case 8:
                    instance.Id = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream);
                    continue;

                // Field 2 Varint
                case 16:
                    instance.Len = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream);
                    continue;

                // Field 3 LengthDelimited
                case 26:
                    if (instance.Request == null)
                    {
                        instance.Request = Example.KVRequest.DeserializeLengthDelimited(stream);
                    }
                    else
                    {
                        Example.KVRequest.DeserializeLengthDelimited(stream, instance.Request);
                    }
                    continue;

                // Field 4 LengthDelimited
                case 34:
                    if (instance.Response == null)
                    {
                        instance.Response = Example.KVResponse.DeserializeLengthDelimited(stream);
                    }
                    else
                    {
                        Example.KVResponse.DeserializeLengthDelimited(stream, instance.Response);
                    }
                    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 Exception("Invalid field id: 0, something went wrong in the stream");

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

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