コード例 #1
0
        private Object decodeData(byte[] data, Object proxy)
        {
            _is.wrap(data);
            _is.setServerEncoding(_encodeName);
            Object o = _is.read(proxy, 0, true);

            return(o);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: liangzai90/Tencent_Tars
        /*
         * 结构体赋值并序列化的例子
         */
        public void TestTars(ref TestInfo ti, ref byte[] buffer)
        {
            //给结构TestInfo赋值
            ti.ibegin = 1111;
            ti.ii     = 54321;
            ti.li     = -1000;
            ti.iend   = 9999;
            ti.f      = 0.999f;
            ti.d      = 123.456f;
            ti.s      = "teststring0";
            ti.uii    = 123456789;

            //test vector<byte>
            ti.vb = new List <byte> ();

            //////////////////////////////////////////////
            //vector<int> 类型的成员赋值
            ti.vi = new List <int>();
            for (int il = 0; il < 10; il++)
            {
                ti.vi.Add(10000 + il);
            }

            //////////////////////////////////////////////
            //map<int, string> 类型的成员赋值
            ti.mi = new Dictionary <int, string>();
            for (int idl = 0; idl < 10; idl++)
            {
                ti.mi.Add(20000 + idl, "testmap" + idl);
            }


            //////////////////////////////////////////////
            //结构类型的成员赋值
            ti.aa     = new A();
            ti.aa.a   = 200;
            ti.aa.b   = new B();
            ti.aa.b.a = 300;
            ti.aa.b.f = 0.300f;


            //////////////////////////////////////////////
            // vector<struct>类型的成员赋值
            ti.vi2 = new List <A>();
            for (int j = 0; j < 10; j++)
            {
                A a = new A();
                a.a   = 200000 + j;
                a.b   = new B();
                a.b.a = 300;
                a.b.f = 0.300f;
                ti.vi2.Add(a);
            }
            ///List<float>类型成员赋值
            ti.vf = new List <float>();
            float ff = 1.23f;

            for (int il = 0; il < 5; il++)
            {
                ti.vf.Add(ff);
            }
            //////////////////////////////////////////////
            // map<int, struct>类型的成员赋值
            ti.mi2 = new Dictionary <int, A>();
            for (int il = 0; il < 10; il++)
            {
                A a = new A();
                a.a   = 200 + il;
                a.b   = new B();
                a.b.a = 300;
                a.b.f = 0.300f;
                ti.mi2.Add(300000 + il, a);
            }


            //////////////////////////////////////////////
            // map<string, vector<struct>>类型的成员赋值
            ti.msv = new Dictionary <string, List <A> >();
            for (int il = 0; il < 10; il++)
            {
                List <A> listA = new List <A>();
                for (int j = 0; j < 2; j++)
                {
                    A a = new A();
                    a.a   = 200 + il;
                    a.b   = new B();
                    a.b.a = 300;
                    a.b.f = 0.300f;

                    listA.Add(a);
                }
                ti.msv.Add("key" + 400000 + il, listA);
            }
            //结构体赋值结束

            TarsOutputStream os = new TarsOutputStream(0);

            //把结构序列化
            ti.WriteTo(os);

            buffer = os.toByteArray();

            System.IO.File.WriteAllBytes(@"tarsbuffer.txt", buffer);

            TarsInputStream _is = new TarsInputStream();

            _is.wrap(buffer, 0);
            TestInfo stTest = new TestInfo();

            stTest.ReadFrom(_is);

            Console.WriteLine("stTest.ibegin:" + stTest.ibegin);
            Console.WriteLine("stTest.li:" + stTest.li);
            Console.WriteLine("stTest.s:" + stTest.s);
            Console.ReadLine();
        }