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"); }
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()); }
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"); }
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"); }
/* * 测试 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(); }