/* * tup的例子 */ public void TestTup(TestInfo ti, ref byte[] buffer) { UniPacket client = new UniPacket(); client.SetVersion(3); client.ServantName = "ServantName"; client.FuncName = "test"; short tests = -100; int testi = int.MaxValue; long testl = -1000; float testf = -100.032f; string teststr = "teststring"; client.Put("mystruct", ti); client.Put("tests", tests); client.Put("testi", testi); client.Put("testl", testl); client.Put("testf", testf); client.Put("teststr", teststr); // 编码tup buffer = client.Encode(); Console.WriteLine("buffer.size: " + buffer.Length); // 解码tup UniPacket de = new UniPacket(); de.Decode(buffer); 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(); }
public void testbyNetWork() { try { tcpClient client = new tcpClient(); //建立socket client.createSocket(); //连接服务端 client.connect("127.0.0.1", 8080); TestInfo tiSend = new TestInfo(); assignTestInfo(ref tiSend); //-------------------------encode----------------------- UniPacket encodePack = new UniPacket(); //设置tup版本号,默认是PACKET_TYPE_TUP encodePack.ServantName = "TestInfo"; encodePack.FuncName = "TestInfo"; short tests = -100; int testi = int.MaxValue; long testl = -1000; float testf = -100.032f; string teststr = "teststring"; encodePack.Put("tests", tests); encodePack.Put("testi", testi); encodePack.Put("testl", testl); encodePack.Put("testf", testf); encodePack.Put("teststr", teststr); encodePack.Put <TestInfo>("TestInfo", tiSend); byte[] bufferS = encodePack.Encode(); Console.WriteLine("打印发送 buffer"); int ret = client.send(bufferS); StringBuilder sb = new StringBuilder(); tiSend.Display(sb, 0); Console.WriteLine(sb.ToString()); Console.WriteLine("发送大小: " + ret); byte[] bufferR = new byte[8192]; ret = client.receive(bufferR); Console.WriteLine("打印接收 buffer"); Console.WriteLine("接收大小:" + ret); //----------------------decode---------------------------- if (ret > sizeof(int)) { UniPacket decodePack = new UniPacket(); decodePack.Decode(bufferR); TestInfo tiRecv = new TestInfo(); short dtests = 0; int dtesti = 0; long dtestl = 0; float dtestf = 0.0f; string dteststr = ""; //使用新的Get<T>(string Name, T DefaultObj) dtests = decodePack.Get <short>("tests", dtests); dtesti = decodePack.Get <int>("testi", dtesti); dtestl = decodePack.Get <long>("testl", dtestl); dtestf = decodePack.Get <float>("testf", dtestf); dteststr = decodePack.Get <string>("teststr", dteststr); tiRecv = decodePack.Get <TestInfo>("TestInfo", tiRecv); sb.Clear(); tiRecv.Display(sb, 0); Console.WriteLine("end:" + sb.ToString()); Console.WriteLine("dtests:" + dtests); Console.WriteLine("dtesti:" + dtesti); Console.WriteLine("dtestl:" + dtestl); Console.WriteLine("dtestf:" + dtestf); Console.WriteLine("dteststr:" + dteststr); Console.ReadLine(); } client.close(); } catch (Exception e) { Console.WriteLine("testbyNetWork: " + e.Message); } }
public void TestMapList() { UniPacket client = new UniPacket(); client.ServantName = "ServantName"; client.FuncName = "FuncName"; client.RequestId = 0; Dictionary <string, int> dict1 = new Dictionary <string, int>(); dict1.Add("s1", 2341); List <int> list1 = new List <int>(); for (int il = 0; il < 10; il++) { list1.Add(il); } Dictionary <string, List <int> > dictList = new Dictionary <string, List <int> >(); for (int idl = 0; idl < 3; idl++) { List <int> listdict = new List <int>(); for (int il = 0; il < 10; il++) { listdict.Add(il + idl); } dictList.Add("key " + idl, listdict); } Dictionary <string, Dictionary <string, int> > dictdict = new Dictionary <string, Dictionary <string, int> >(); for (int idl = 0; idl < 3; idl++) { Dictionary <string, int> dd = new Dictionary <string, int>(); for (int il = 0; il < 10; il++) { dd.Add("sub key " + il + idl, il + idl); } dictdict.Add("key " + idl, dd); } byte[] abtye = new byte[10]; for (byte ib = 1; ib < abtye.Length; ib++) { abtye[ib] = ib; } client.Put("array", abtye); client.Put("list", list1); client.Put("emptylist", new List <byte>()); client.Put("emptylistint", new List <int>()); client.Put("emptylistTars", new List <TestFriendlInfo>()); client.Put("emptyMapintstring", new Dictionary <int, string>()); client.Put("emptyMapintTars", new Dictionary <int, TestFriendlInfo>()); client.Put("map", dict1); client.Put("maplist", dictList); client.Put("mapmap", dictdict); byte[] buffer = client.Encode(); UniPacket de = new UniPacket(); de.Decode(buffer); abtye = de.Get <byte[]>("array"); Dictionary <string, int> dict10 = new Dictionary <string, int>(); dict10 = de.Get <Dictionary <string, int> >("map"); QTrace.Trace(dict10); List <int> list10 = new List <int>(); list10 = de.Get <List <int> >("list"); QTrace.Trace(list10); List <byte> emptylist = new List <byte>(); emptylist = de.Get <List <byte> >("emptylist"); QTrace.Trace(emptylist); List <int> emptylistint = new List <int>(); emptylistint = de.Get <List <int> >("emptylistint"); QTrace.Trace(emptylistint); List <TestFriendlInfo> emptylistTars = de.Get <List <TestFriendlInfo> >("emptylistTars"); QTrace.Trace(emptylistTars); Dictionary <int, string> emptyMapintstring = de.Get <Dictionary <int, string> >("emptyMapintstring"); QTrace.Trace(emptyMapintstring); Dictionary <int, TestFriendlInfo> emptyMapintTars = de.Get <Dictionary <int, TestFriendlInfo> >("emptyMapintTars"); QTrace.Trace(emptyMapintTars); dictList = (Dictionary <string, List <int> >)de.Get <Dictionary <string, List <int> > >("maplist"); foreach (string key in dictList.Keys) { QTrace.Trace(key); QTrace.Trace(dictList[key]); } dictdict = (Dictionary <string, Dictionary <string, int> >)de.Get <Dictionary <string, Dictionary <string, int> > >("mapmap"); QTrace.Trace("Map Map"); foreach (string key in dictdict.Keys) { QTrace.Trace(key); Dictionary <string, int> subdict = dictdict[key]; QTrace.Trace(subdict); } }