Esempio n. 1
0
    static void Main()
    {
        var file = @"c:\temp\test.bin";
        IEnumerable <Yours> t = new Yours[3];

        File.WriteAllBytes(file, ByteSerializer.SerializeMessage(t));
        using (var stream = File.OpenRead(file))
        {
            Console.WriteLine("file size: " + stream.Length);
        }
    }
        /// <summary>Read the given number of bytes from the stream and deserialze it into the instance.</summary>
        public static Yours.Nullables.MyMessageV2 DeserializeLength(Stream stream, int length, Yours.Nullables.MyMessageV2 instance)
        {
            var br = new BinaryReader(stream);
            instance.FieldA = -1;
            instance.FieldB = 4.5;
            instance.FieldC = 5.4f;
            instance.FieldD = -2;
            instance.FieldE = -3;
            instance.FieldF = 4;
            instance.FieldG = 5;
            instance.FieldH = -6;
            instance.FieldI = -7;
            instance.FieldJ = 8;
            instance.FieldK = 9;
            instance.FieldL = -10;
            instance.FieldM = -11;
            instance.FieldN = false;
            instance.FieldO = "test";
            instance.FieldR = Yours.Nullables.MyMessageV2.MyEnum.ETest2;
#pragma warning disable 612
#pragma warning restore 612
            if (instance.FieldT == null)
                instance.FieldT = new List<uint>();
            if (instance.FieldS == null)
                instance.FieldS = new List<uint>();
            if (instance.FieldV == null)
                instance.FieldV = new List<Theirs.Nullables.TheirMessage>();
            long limit = stream.Position + length;
            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 Varint
                    case 8:
                        instance.FieldA = (int)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    // Field 2 Fixed64
                    case 17:
                        instance.FieldB = br.ReadDouble();
                        continue;
                    // Field 3 Fixed32
                    case 29:
                        instance.FieldC = br.ReadSingle();
                        continue;
                    // Field 4 Varint
                    case 32:
                        instance.FieldD = (int)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    // Field 5 Varint
                    case 40:
                        instance.FieldE = (long)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    // Field 6 Varint
                    case 48:
                        instance.FieldF = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream);
                        continue;
                    // Field 7 Varint
                    case 56:
                        instance.FieldG = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    // Field 8 Varint
                    case 64:
                        instance.FieldH = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadZInt32(stream);
                        continue;
                    // Field 9 Varint
                    case 72:
                        instance.FieldI = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadZInt64(stream);
                        continue;
                    // Field 10 Fixed32
                    case 85:
                        instance.FieldJ = br.ReadUInt32();
                        continue;
                    // Field 11 Fixed64
                    case 89:
                        instance.FieldK = br.ReadUInt64();
                        continue;
                    // Field 12 Fixed32
                    case 101:
                        instance.FieldL = br.ReadInt32();
                        continue;
                    // Field 13 Fixed64
                    case 105:
                        instance.FieldM = br.ReadInt64();
                        continue;
                    // Field 14 Varint
                    case 112:
                        instance.FieldN = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadBool(stream);
                        continue;
                    // Field 15 LengthDelimited
                    case 122:
                        instance.FieldO = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadString(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");
                    case 16:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.LengthDelimited)
                            break;
                        instance.FieldP = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadBytes(stream);
                        continue;
                    case 17:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.Varint)
                            break;
                        instance.FieldQ = (Yours.Nullables.MyMessageV2.MyEnum)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    case 18:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.Varint)
                            break;
                        instance.FieldR = (Yours.Nullables.MyMessageV2.MyEnum)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    case 19:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.LengthDelimited)
                            break;
#pragma warning disable 612
                        instance.Dummy = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadString(stream);
                        continue;
#pragma warning restore 612
                    case 20:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.LengthDelimited)
                            break;
                        // repeated packed
                        long end20 = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream);
                        end20 += stream.Position;
                        while (stream.Position < end20)
                        {
                            instance.FieldT.Add(br.ReadUInt32());
                        }
                        if (stream.Position != end20)
                            throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException("Read too many bytes in packed data");
                        continue;
                    case 21:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.Varint)
                            break;
                        // repeated
                        instance.FieldS.Add(global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream));
                        continue;
                    case 22:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.LengthDelimited)
                            break;
                        if (instance.FieldU == null)
                            instance.FieldU = Theirs.Nullables.TheirMessage.DeserializeLengthDelimited(stream);
                        else
                            Theirs.Nullables.TheirMessage.DeserializeLengthDelimited(stream, instance.FieldU);
                        continue;
                    case 23:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.LengthDelimited)
                            break;
                        // repeated
                        instance.FieldV.Add(Theirs.Nullables.TheirMessage.DeserializeLengthDelimited(stream));
                        continue;
                    case 24:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.Varint)
                            break;
                        instance.NullableInt = (int)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    case 25:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.Varint)
                            break;
                        instance.NullableEnum = (Yours.Nullables.MyMessageV2.AliasedEnum)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    default:
                        global::SilentOrbit.ProtocolBuffers.ProtocolParser.SkipKey(stream, key);
                        break;
                }
            }

            return instance;
        }
 /// <summary>Helper: put the buffer into a MemoryStream before deserializing</summary>
 public static Yours.Nullables.MyMessageV2 Deserialize(byte[] buffer, Yours.Nullables.MyMessageV2 instance)
 {
     using (var ms = new MemoryStream(buffer))
         Deserialize(ms, instance);
     return instance;
 }
        /// <summary>Read the VarInt length prefix and the given number of bytes from the stream and deserialze it into the instance.</summary>
        public static Yours.MyMessageV2 DeserializeLengthDelimited(Stream stream, Yours.MyMessageV2 instance)
        {
            BinaryReader br = new BinaryReader(stream);
            instance.FieldR = Yours.MyMessageV2.MyEnum.ETest2;
            if (instance.FieldT == null)
                instance.FieldT = new List<uint>();
            if (instance.FieldS == null)
                instance.FieldS = new List<uint>();
            if (instance.FieldV == null)
                instance.FieldV = new List<Theirs.TheirMessage>();
            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 InvalidOperationException("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 Varint
                    case 8:
                        instance.FieldA = (int)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    // Field 2 Fixed64
                    case 17:
                        instance.FieldB = br.ReadDouble();
                        continue;
                    // Field 3 Fixed32
                    case 29:
                        instance.FieldC = br.ReadSingle();
                        continue;
                    // Field 4 Varint
                    case 32:
                        instance.FieldD = (int)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    // Field 5 Varint
                    case 40:
                        instance.FieldE = (long)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    // Field 6 Varint
                    case 48:
                        instance.FieldF = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream);
                        continue;
                    // Field 7 Varint
                    case 56:
                        instance.FieldG = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    // Field 8 Varint
                    case 64:
                        instance.FieldH = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadZInt32(stream);
                        continue;
                    // Field 9 Varint
                    case 72:
                        instance.FieldI = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadZInt64(stream);
                        continue;
                    // Field 10 Fixed32
                    case 85:
                        instance.FieldJ = br.ReadUInt32();
                        continue;
                    // Field 11 Fixed64
                    case 89:
                        instance.FieldK = br.ReadUInt64();
                        continue;
                    // Field 12 Fixed32
                    case 101:
                        instance.FieldL = br.ReadInt32();
                        continue;
                    // Field 13 Fixed64
                    case 105:
                        instance.FieldM = br.ReadInt64();
                        continue;
                    // Field 14 Varint
                    case 112:
                        instance.FieldN = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadBool(stream);
                        continue;
                    // Field 15 LengthDelimited
                    case 122:
                        instance.FieldO = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadString(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 InvalidDataException("Invalid field id: 0, something went wrong in the stream");
                    case 16:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.LengthDelimited)
                            break;
                        instance.FieldP = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadBytes(stream);
                        continue;
                    case 17:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.Varint)
                            break;
                        instance.FieldQ = (Yours.MyMessageV2.MyEnum)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    case 18:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.Varint)
                            break;
                        instance.FieldR = (Yours.MyMessageV2.MyEnum)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream);
                        continue;
                    case 19:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.LengthDelimited)
                            break;
                        instance.Dummy = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadString(stream);
                        continue;
                    case 20:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.LengthDelimited)
                            break;
                        // repeated packed
                        using (var ms20 = new MemoryStream(global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadBytes(stream)))
                        {
                            BinaryReader br20 = new BinaryReader(ms20);
                            while (ms20.Position < ms20.Length)
                            {
                                instance.FieldT.Add(br20.ReadUInt32());
                            }
                        }
                        continue;
                    case 21:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.Varint)
                            break;
                        // repeated
                        instance.FieldS.Add(global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream));
                        continue;
                    case 22:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.LengthDelimited)
                            break;
                        if (instance.FieldU == null)
                            instance.FieldU = Theirs.TheirMessage.DeserializeLengthDelimited(stream);
                        else
                            Theirs.TheirMessage.DeserializeLengthDelimited(stream, instance.FieldU);
                        continue;
                    case 23:
                        if(key.WireType != global::SilentOrbit.ProtocolBuffers.Wire.LengthDelimited)
                            break;
                        // repeated
                        instance.FieldV.Add(Theirs.TheirMessage.DeserializeLengthDelimited(stream));
                        continue;
                    default:
                        global::SilentOrbit.ProtocolBuffers.ProtocolParser.SkipKey(stream, key);
                        break;
                }
            }

            return instance;
        }