public void DecodeRequestContent(Request req)
 {
     if (req.ParameterTypes.Length > 0)
     {
         if (req.Version == TarsCodecsConstant.VERSION)
         {
             if (req.Parameters == null)
                 req.Parameters = new object[req.ParameterTypes.Length];
             //todo buffer to byte[]
             TarsInputStream jis = new TarsInputStream((byte[])req.Buffer);
             //todo charset
             for (int i = 0; i < req.ParameterTypes.Length; i++)
             {
                 Type type = TarsHelper.GetSourceType(req.ParameterTypes[i].ParameterType);
                 req.Parameters[i] = jis.Read(type, i, false);
             }
         }
         else if (req.Version == TarsCodecsConstant.VERSION3 || req.Version == TarsCodecsConstant.VERSION2)
         {
             if (req.Parameters == null)
                 throw new ArgumentException("params name should not be empty");
             UniAttribute unaIn = new UniAttribute(req.Version);
             unaIn.Decode((byte[])req.Buffer);
             for (int i = 0; i < req.ParameterTypes.Length; i++)
             {
                 req.Parameters[i] = unaIn.GetByType(req.ParameterTypes[i].Name, TarsHelper.GetSourceType(req.ParameterTypes[i].ParameterType));
             }
         }
         else
         {
             throw new TarsException(RpcStatusCode.ServerDecodeErr, "un supported protocol, version = " + req.Version);
         }
         req.Buffer = null;
     }
 }
Esempio n. 2
0
        public void TestArray()
        {
            UniAttribute client = new UniAttribute();

            byte[] aByte = new byte[]
            {
                1, 2, 3, 4, 5, 6
            };
            client.Put("aByte", aByte);
            client.Put("aEmpty", new byte[0]);
            client.Put("aint", new int[] {
                11, 22, 33, 44, 55, 66, 77, 88, 99, 10, 1111
            });


            byte[] buffer = client.Encode();

            UniAttribute de = new UniAttribute();

            de.Decode(buffer);

            aByte = de.Get <byte[]>("aByte");
            byte[] aEmpty = de.Get <byte[]>("aEmpty");
            int[]  aint   = de.Get <int[]>("aint");
        }
Esempio n. 3
0
        private byte[] EncodeTupResponseContent(Response message, string charsetName)
        {
            UniAttribute uni = new UniAttribute(charsetName, message.Version);

            if (message.ResultStatusCode == RpcStatusCode.ServerSuccess)
            {
                var type = message.ReturnValueType.ParameterType;
                if (type != typeof(void) && type != typeof(Task))
                {
                    if (type.BaseType == typeof(Task))
                    {
                        uni.Put(TarsCodecsConstant.STAMP_STRING, message.ReturnValue);
                    }
                    else
                    {
                        uni.Put(TarsCodecsConstant.STAMP_STRING, type.GetProperty("Result").GetReflector().GetValue(message.ReturnValue));
                    }
                }
                for (int i = 0; i < message.ReturnParameterTypes.Length; i++)
                {
                    uni.Put(message.ReturnParameterTypes[i].Name, message.ReturnParameters[i]);
                }
            }
            return(uni.Encode());
        }
Esempio n. 4
0
        /*
         * 测试 UniAttributte
         */
        public void TestUnipack(TestInfo ti)
        {
            byte[]       buffer  = new byte[8192];
            UniAttribute unipack = new UniAttribute();

            short  tests   = -100;
            int    testi   = int.MaxValue;
            long   testl   = -1000;
            float  testf   = -100.032f;
            string teststr = "teststring";

            // 设置 tup 版本号,默认是 tup2,非精简版.
            unipack.Version = Tup.Const.TUP_VERSION_2;

            unipack.Put("mystruct", ti);
            unipack.Put("tests", tests);
            unipack.Put("testi", testi);
            unipack.Put("testl", testl);
            unipack.Put("testf", testf);
            unipack.Put("teststr", teststr);

            buffer = unipack.Encode();
            Console.WriteLine("buffer.size: " + buffer.Length);

            UniAttribute    de  = new UniAttribute();
            TarsInputStream _is = new TarsInputStream(buffer);

            de.ReadFrom(_is);

            TestInfo st       = new TestInfo();
            short    dtests   = 0;
            int      dtesti   = 0;
            long     dtestl   = 0;
            float    dtestf   = 0.0f;
            string   dteststr = "";

            // 使用新的Get<T>(string Name, T DefaultObj)
            dtests   = de.Get <short>("tests", dtests);
            dtesti   = de.Get <int>("testi", dtesti);
            dtestl   = de.Get <long>("testl", dtestl);
            dtestf   = de.Get <float>("testf", dtestf);
            dteststr = de.Get <string>("teststr", dteststr);
            st       = de.Get <TestInfo>("mystruct", st);

            Console.WriteLine("dtests: " + dtests);
            Console.WriteLine("dtesti: " + dtesti);
            Console.WriteLine("dtestl: " + dtestl);
            Console.WriteLine("dtestf: " + dtestf);
            Console.WriteLine("dteststr: " + dteststr);
            Console.WriteLine("\nTestInfo get struct:  st->ibegin=" + st.ibegin + " st->li=" + st.li);

            Console_PressAnyKey();
        }
Esempio n. 5
0
        public void TestBasicType()
        {
            UniAttribute client = new UniAttribute();

            client.Put("short", (short)-123);
            client.Put("int", -1);
            client.Put("long", -1L);
            client.Put("ushort", (ushort)1234);
            client.Put("uint", (uint)1324234);
            client.Put("ulong", (ulong)123432455);
            client.Put("float", 1.23f);
            client.Put("double", (double)3.1415926);

            byte[] buffer = client.Encode();

            UniAttribute de = new UniAttribute();

            de.Decode(buffer);
            short sh = 0;

            sh = de.Get <short>("short");

            int i = 0;

            i = de.Get <int>("int");

            long l = 0;

            l = de.Get <long>("long");

            ushort ush = 0;

            ush = (ushort)de.Get <ushort>("ushort");

            uint ui = 0;

            ui = de.Get <uint>("uint");

            ulong ul = 0;

            ul = de.Get <ulong>("ulong");

            float f = 0.0f;

            f = de.Get <float>("float");

            double d = 0.0;

            d = de.Get <double>("double");
        }
Esempio n. 6
0
        public void TestTars()
        {
            UniAttribute client = new UniAttribute();

            TestFriendlInfo tfi = new TestFriendlInfo();

            tfi.dictTest = new Dictionary <int, string>();
            for (int i = 0; i < 3; i++)
            {
                tfi.dictTest.Add(i, "v" + (i + 1));
            }
            tfi.friend_count        = 3;
            tfi.getfriendCount      = 4;
            tfi.getgroupCount       = 5;
            tfi.group_count         = 6;
            tfi.groupid             = 7;
            tfi.groupstartIndex     = 13;
            tfi.ifGetGroupInfo      = 0;
            tfi.ifReflush           = 1;
            tfi.reqtype             = 4321;
            tfi.startIndex          = 32;
            tfi.totoal_friend_count = 3;
            tfi.totoal_group_count  = 43;
            tfi.uin           = 3214123;
            tfi.vecFriendInfo = new List <FriendInfo>();
            for (int j = 0; j < 3; j++)
            {
                FriendInfo fi = new FriendInfo();
                fi.faceId    = (byte)j;
                fi.friendUin = j + 100;
                fi.groupId   = (byte)(j + 13);
                fi.remark    = "hello " + j;
                fi.sqqtype   = (byte)j;
                fi.status    = (byte)j;
                tfi.vecFriendInfo.Add(fi);
            }

            client.Put("TestFriendlInfo", tfi);

            byte[] buffer = client.Encode();

            UniAttribute de = new UniAttribute();

            de.Decode(buffer);

            TestFriendlInfo detfi = de.Get <TestFriendlInfo>("TestFriendlInfo");
        }