Пример #1
0
        /// <summary>
        /// Create a message with test data
        /// </summary>
        public static MyMessageV2 TestInstance()
        {
            MyMessageV2 mm = new MyMessageV2();

            mm.FieldA = 1;
            mm.FieldB = 2.2;
            mm.FieldC = 3.3f;
            mm.FieldD = -4;
            mm.FieldE = new TimeSpan(3, 6, 3, 1).Ticks;
            mm.FieldF = 6;
            mm.FieldG = 7;
            mm.FieldH = -8;
            mm.FieldI = -9;
            mm.FieldJ = 10;
            mm.FieldK = 11;
            mm.FieldL = -12;
            mm.FieldM = -13;
            mm.FieldN = true;
            mm.FieldO = "test1";
            #pragma warning disable 612
            mm.FieldP = new byte[] { 0, 1, 2, 3, 4 };
            #pragma warning restore 612
            mm.FieldQ = MyMessageV2.MyEnum.ETest1;
            mm.FieldR = MyMessageV2.MyEnum.ETest3;
            mm.FieldS = new List <uint>();
            mm.FieldS.Add(20);
            mm.FieldS.Add(120);
            mm.FieldS.Add(1120);
            mm.FieldT = new List <uint>();
            mm.FieldT.Add(21);
            mm.FieldT.Add(121);
            mm.FieldT.Add(1121);
            mm.FieldU = new TheirMessage()
            {
                FieldA = 22
            };
            mm.FieldV = new List <TheirMessage>();
            mm.FieldV.Add(new TheirMessage()
            {
                FieldA = 23
            });
            mm.FieldV.Add(new TheirMessage()
            {
                FieldA = 123
            });
            mm.FieldV.Add(new TheirMessage()
            {
                FieldA = 1123
            });
            return(mm);
        }
Пример #2
0
 static void TestDefault(MyMessageV2 m)
 {
     Assert.AreEqual(-1, m.FieldA);
     Assert.AreEqual(4.5, m.FieldB);
     Assert.AreEqual(5.4f, m.FieldC);
     Assert.AreEqual(-2, m.FieldD);
     Assert.AreEqual(-3, m.FieldE);
     Assert.AreEqual(4u, m.FieldF);
     Assert.AreEqual(5u, m.FieldG);
     Assert.AreEqual(-6, m.FieldH);
     Assert.AreEqual(-7, m.FieldI);
     Assert.AreEqual(8u, m.FieldJ);
     Assert.AreEqual(9u, m.FieldK);
     Assert.AreEqual(-10, m.FieldL);
     Assert.AreEqual(-11, m.FieldM);
     Assert.AreEqual(false, m.FieldN);
     Assert.AreEqual("test", m.FieldO);
 }
Пример #3
0
        public override bool Equals(object obj)
        {
            MyMessageV2 mm = this;
            MyMessageV2 mo = obj as MyMessageV2;

            Test("FieldA", mm.FieldA == mo.FieldA);
            Test("FieldB", mm.FieldB == mo.FieldB);
            Test("FieldC", mm.FieldC == mo.FieldC);
            Test("FieldD", mm.FieldD == mo.FieldD);
            Test("FieldE", mm.FieldE == mo.FieldE);
            Test("FieldF", mm.FieldF == mo.FieldF);
            Test("FieldG", mm.FieldG == mo.FieldG);
            Test("FieldH", mm.FieldH == mo.FieldH);
            Test("FieldI", mm.FieldI == mo.FieldI);
            Test("FieldJ", mm.FieldJ == mo.FieldJ);
            Test("FieldK", mm.FieldK == mo.FieldK);
            Test("FieldL", mm.FieldL == mo.FieldL);
            Test("FieldM", mm.FieldM == mo.FieldM);
            Test("FieldN", mm.FieldN == mo.FieldN);
            Test("FieldO", mm.FieldO == mo.FieldO);
            #pragma warning disable 612
            Test("FieldP.Length", mm.FieldP.Length == mo.FieldP.Length);
            for (int n = 0; n < mm.FieldP.Length; n++)
            {
                Test("FieldP[" + n + "]", mm.FieldP[n] == mo.FieldP[n]);
            }
            #pragma warning restore 612
            Test("FieldQ", mm.FieldQ == mo.FieldQ);
            Test("FieldR", mm.FieldR == mo.FieldR);
            Test("FieldS.Count", mm.FieldS.Count == mo.FieldS.Count);
            Test("FieldS 0", mm.FieldS[0] == mo.FieldS[0]);
            Test("FieldS 1", mm.FieldS[1] == mo.FieldS[1]);
            Test("FieldS 2", mm.FieldS[2] == mo.FieldS[2]);
            Test("FieldT.Count", mm.FieldT.Count == mo.FieldT.Count);
            Test("FieldT 0", mm.FieldT[0] == mo.FieldT[0]);
            Test("FieldT 1", mm.FieldT[1] == mo.FieldT[1]);
            Test("FieldT 2", mm.FieldT[2] == mo.FieldT[2]);
            Test("FieldU", mm.FieldU.FieldA == mo.FieldU.FieldA);
            Test("FieldV.Count", mm.FieldV.Count == mo.FieldV.Count);
            Test("FieldV 0", mm.FieldV[0].FieldA == mo.FieldV[0].FieldA);
            Test("FieldV 1", mm.FieldV[1].FieldA == mo.FieldV[1].FieldA);
            Test("FieldV 2", mm.FieldV[2].FieldA == mo.FieldV[2].FieldA);
            return(true);
        }
Пример #4
0
 /// <summary>
 /// Create a message with test data
 /// </summary>
 public static MyMessageV2 TestInstance()
 {
     MyMessageV2 mm = new MyMessageV2();
     mm.FieldA = 1;
     mm.FieldB = 2.2;
     mm.FieldC = 3.3f;
     mm.FieldD = -4;
     mm.FieldE = new TimeSpan(3, 6, 3, 1).Ticks;
     mm.FieldF = 6;
     mm.FieldG = 7;
     mm.FieldH = -8;
     mm.FieldI = -9;
     mm.FieldJ = 10;
     mm.FieldK = 11;
     mm.FieldL = -12;
     mm.FieldM = -13;
     mm.FieldN = true;
     mm.FieldO = "test1";
     #pragma warning disable 612
     mm.FieldP = new byte[] { 0, 1, 2, 3, 4 };
     #pragma warning restore 612
     mm.FieldQ = MyMessageV2.MyEnum.ETest1;
     mm.FieldR = MyMessageV2.MyEnum.ETest3;
     mm.FieldS = new List<uint>();
     mm.FieldS.Add(20);
     mm.FieldS.Add(120);
     mm.FieldS.Add(1120);
     mm.FieldT = new List<uint>();
     mm.FieldT.Add(21);
     mm.FieldT.Add(121);
     mm.FieldT.Add(1121);
     mm.FieldU = new TheirMessage() { FieldA = 22 };
     mm.FieldV = new List<TheirMessage>();
     mm.FieldV.Add(new TheirMessage() { FieldA = 23 });
     mm.FieldV.Add(new TheirMessage() { FieldA = 123 });
     mm.FieldV.Add(new TheirMessage() { FieldA = 1123 });
     return mm;
 }
Пример #5
0
        public void RunDefaultTest()
        {
            //Test constuctor values
            var m = new MyMessageV2();
            TestDefault(m);

            //Test deserialization
            m.FieldA = 0;
            m.FieldB = 0;
            m.FieldC = 0;
            m.FieldD = 0;
            m.FieldE = 0;
            m.FieldF = 0;
            m.FieldG = 0;
            m.FieldI = 0;
            m.FieldJ = 0;
            m.FieldK = 0;
            m.FieldL = 0;
            m.FieldM = 0;
            m.FieldN = true;
            m.FieldO = null;
            MyMessageV2.Deserialize(new byte[0], m);
            TestDefault(m);
        }
 /// <summary>Helper: Serialize with a varint length prefix</summary>
 public static void SerializeLengthDelimited(Stream stream, MyMessageV2 instance)
 {
     var data = SerializeToBytes(instance);
     global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length);
     stream.Write(data, 0, data.Length);
 }
 /// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary>
 public static byte[] SerializeToBytes(MyMessageV2 instance)
 {
     using (var ms = new MemoryStream())
     {
         Serialize(ms, instance);
         return ms.ToArray();
     }
 }
        /// <summary>Serialize the instance into the stream</summary>
        public static void Serialize(Stream stream, MyMessageV2 instance)
        {
            var bw = new BinaryWriter(stream);
            var msField = global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Pop();
            // Key for field: 1, Varint
            stream.WriteByte(8);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldA);
            // Key for field: 2, Fixed64
            stream.WriteByte(17);
            bw.Write(instance.FieldB);
            // Key for field: 3, Fixed32
            stream.WriteByte(29);
            bw.Write(instance.FieldC);
            // Key for field: 4, Varint
            stream.WriteByte(32);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldD);
            // Key for field: 5, Varint
            stream.WriteByte(40);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldE);
            // Key for field: 6, Varint
            stream.WriteByte(48);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, instance.FieldF);
            // Key for field: 7, Varint
            stream.WriteByte(56);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream, instance.FieldG);
            // Key for field: 8, Varint
            stream.WriteByte(64);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteZInt32(stream, instance.FieldH);
            // Key for field: 9, Varint
            stream.WriteByte(72);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteZInt64(stream, instance.FieldI);
            // Key for field: 10, Fixed32
            stream.WriteByte(85);
            bw.Write(instance.FieldJ);
            // Key for field: 11, Fixed64
            stream.WriteByte(89);
            bw.Write(instance.FieldK);
            // Key for field: 12, Fixed32
            stream.WriteByte(101);
            bw.Write(instance.FieldL);
            // Key for field: 13, Fixed64
            stream.WriteByte(105);
            bw.Write(instance.FieldM);
            // Key for field: 14, Varint
            stream.WriteByte(112);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBool(stream, instance.FieldN);
            if (instance.FieldO == null)
                throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException("FieldO is required by the proto specification.");
            // Key for field: 15, LengthDelimited
            stream.WriteByte(122);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.FieldO));
#pragma warning disable 612
            if (instance.FieldP == null)
                throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException("FieldP is required by the proto specification.");
            // Key for field: 16, LengthDelimited
            stream.WriteByte(130);
            stream.WriteByte(1);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, instance.FieldP);
#pragma warning restore 612
            // Key for field: 17, Varint
            stream.WriteByte(136);
            stream.WriteByte(1);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldQ);
            if (instance.FieldR != Yours.MyMessageV2.MyEnum.ETest2)
            {
                // Key for field: 18, Varint
                stream.WriteByte(144);
                stream.WriteByte(1);
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldR);
            }
#pragma warning disable 612
            if (instance.Dummy != null)
            {
                // Key for field: 19, LengthDelimited
                stream.WriteByte(154);
                stream.WriteByte(1);
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Dummy));
            }
#pragma warning restore 612
            if (instance.FieldT != null)
            {
                // Key for field: 20, LengthDelimited
                stream.WriteByte(162);
                stream.WriteByte(1);
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, 4u * (uint)instance.FieldT.Count);
                foreach (var i20 in instance.FieldT)
                {
                    bw.Write(i20);
                }
            }
            if (instance.FieldS != null)
            {
                foreach (var i21 in instance.FieldS)
                {
                    // Key for field: 21, Varint
                    stream.WriteByte(168);
                    stream.WriteByte(1);
                    global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, i21);
                }
            }
            if (instance.FieldU != null)
            {
                // Key for field: 22, LengthDelimited
                stream.WriteByte(178);
                stream.WriteByte(1);
                msField.SetLength(0);
                Theirs.TheirMessage.Serialize(msField, instance.FieldU);
                // Length delimited byte array
                uint length22 = (uint)msField.Length;
                global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length22);
                msField.WriteTo(stream);

            }
            if (instance.FieldV != null)
            {
                foreach (var i23 in instance.FieldV)
                {
                    // Key for field: 23, LengthDelimited
                    stream.WriteByte(186);
                    stream.WriteByte(1);
                    msField.SetLength(0);
                    Theirs.TheirMessage.Serialize(msField, i23);
                    // Length delimited byte array
                    uint length23 = (uint)msField.Length;
                    global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length23);
                    msField.WriteTo(stream);

                }
            }
            // Key for field: 24, Varint
            stream.WriteByte(192);
            stream.WriteByte(1);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.NullableInt);
            // Key for field: 25, Varint
            stream.WriteByte(200);
            stream.WriteByte(1);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.NullableEnum);
            global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Push(msField);
        }
 /// <summary>Helper: put the buffer into a MemoryStream and create a new instance to deserializing into</summary>
 public static MyMessageV2 Deserialize(byte[] buffer)
 {
     var instance = new MyMessageV2();
     using (var ms = new MemoryStream(buffer))
         Deserialize(ms, instance);
     return instance;
 }
 /// <summary>Helper: create a new instance to deserializing into</summary>
 public static MyMessageV2 DeserializeLength(Stream stream, int length)
 {
     var instance = new MyMessageV2();
     DeserializeLength(stream, length, instance);
     return instance;
 }
 /// <summary>Helper: create a new instance to deserializing into</summary>
 public static MyMessageV2 DeserializeLengthDelimited(Stream stream)
 {
     var instance = new MyMessageV2();
     DeserializeLengthDelimited(stream, instance);
     return instance;
 }
 /// <summary>Helper: create a new instance to deserializing into</summary>
 public static MyMessageV2 Deserialize(Stream stream)
 {
     var instance = new MyMessageV2();
     Deserialize(stream, instance);
     return instance;
 }
Пример #13
0
        /// <summary>
        /// This is a simple test to trigger most functionality of the generated code.
        /// </summary>
        static void TestStandardFeatures()
        {
            MyMessageV2 mm = new MyMessageV2 ();
            mm.FieldA = 1;
            mm.FieldB = 2.2;
            mm.FieldC = 3.3f;
            mm.FieldD = -4;
            mm.FieldE = new TimeSpan (3, 6, 3, 1).Ticks;
            mm.FieldF = 6;
            mm.FieldG = 7;
            mm.FieldH = -8;
            mm.FieldI = -9;
            mm.FieldJ = 10;
            mm.FieldK = 11;
            mm.FieldL = -12;
            mm.FieldM = -13;
            mm.FieldN = true;
            mm.FieldO = "test1";
            mm.FieldP = new byte[] { 0, 1, 2, 3, 4 };
            mm.FieldQ = MyMessageV2.MyEnum.ETest1;
            mm.FieldR = MyMessageV2.MyEnum.ETest3;
            mm.FieldS = new List<uint> ();
            mm.FieldS.Add (20);
            mm.FieldS.Add (120);
            mm.FieldS.Add (1120);
            mm.FieldT = new List<uint> ();
            mm.FieldT.Add (21);
            mm.FieldT.Add (121);
            mm.FieldT.Add (1121);
            mm.FieldU = new TheirMessage (){ FieldA = 22};
            mm.FieldV = new List<TheirMessage> ();
            mm.FieldV.Add (new TheirMessage (){ FieldA = 23});
            mm.FieldV.Add (new TheirMessage (){ FieldA = 123});
            mm.FieldV.Add (new TheirMessage (){ FieldA = 1123});

            //Write
            MemoryStream ms = new MemoryStream ();
            MyMessageV2.Serialize (ms, mm);

            Console.WriteLine ("Wire bytes: " + ms.Length);

            //Read
            MemoryStream msread = new MemoryStream (ms.ToArray ());
            MyMessageV2 mo = MyMessageV2.Deserialize (msread);

            //Verify
            Test ("FieldA", mm.FieldA == mo.FieldA);
            Test ("FieldB", mm.FieldB == mo.FieldB);
            Test ("FieldC", mm.FieldC == mo.FieldC);
            Test ("FieldD", mm.FieldD == mo.FieldD);
            Test ("FieldE", mm.FieldE == mo.FieldE);
            Test ("FieldF", mm.FieldF == mo.FieldF);
            Test ("FieldG", mm.FieldG == mo.FieldG);
            Test ("FieldH", mm.FieldH == mo.FieldH);
            Test ("FieldI", mm.FieldI == mo.FieldI);
            Test ("FieldJ", mm.FieldJ == mo.FieldJ);
            Test ("FieldK", mm.FieldK == mo.FieldK);
            Test ("FieldL", mm.FieldL == mo.FieldL);
            Test ("FieldM", mm.FieldM == mo.FieldM);
            Test ("FieldN", mm.FieldN == mo.FieldN);
            Test ("FieldO", mm.FieldO == mo.FieldO);
            Test ("FieldP.Length", mm.FieldP.Length == mo.FieldP.Length);
            for (int n = 0; n < mm.FieldP.Length; n++)
                Test ("FieldP[" + n + "]", mm.FieldP [n] == mo.FieldP [n]);
            Test ("FieldQ", mm.FieldQ == mo.FieldQ);
            Test ("FieldR", mm.FieldR == mo.FieldR);
            Test ("FieldS.Count", mm.FieldS.Count == mo.FieldS.Count);
            Test ("FieldS 0", mm.FieldS [0] == mo.FieldS [0]);
            Test ("FieldS 1", mm.FieldS [1] == mo.FieldS [1]);
            Test ("FieldS 2", mm.FieldS [2] == mo.FieldS [2]);
            Test ("FieldT.Count", mm.FieldT.Count == mo.FieldT.Count);
            Test ("FieldT 0", mm.FieldT [0] == mo.FieldT [0]);
            Test ("FieldT 1", mm.FieldT [1] == mo.FieldT [1]);
            Test ("FieldT 2", mm.FieldT [2] == mo.FieldT [2]);
            Test ("FieldU", mm.FieldU.FieldA == mo.FieldU.FieldA);
            Test ("FieldV.Count", mm.FieldV.Count == mo.FieldV.Count);
            Test ("FieldV 0", mm.FieldV [0].FieldA == mo.FieldV [0].FieldA);
            Test ("FieldV 1", mm.FieldV [1].FieldA == mo.FieldV [1].FieldA);
            Test ("FieldV 2", mm.FieldV [2].FieldA == mo.FieldV [2].FieldA);

            //Read by older version
            MemoryStream msread1 = new MemoryStream (ms.ToArray ());
            MyMessageV1 m1 = MyMessageV1.Deserialize (msread1);

            Console.WriteLine ("Version 1");
            Test ("FieldA", mm.FieldA == m1.FieldA);
        }