Esempio n. 1
0
        private void SendJsonData(int packet_id, JsonData json)
        {
            Packet pkt = PacketManager.Instance.AllocatePacket();

            pkt.SetPacketID(packet_id);
            pkt.AddJsonData(json);

            SendPacket(pkt);
        }
Esempio n. 2
0
        private void Test_Share_Net_Packet_Json()
        {
            byte[] buffer = new byte[Packet.DEFAULT_PACKET_BUF_SIZE];

            Packet pkt = new Packet(buffer);

            pkt.Initialize();

            CAssert.AreEqual(buffer, pkt.Buf);
            CAssert.AreEqual((int)pkt.Size, Packet.PACKET_HEAD_LENGTH);

            pkt.SetPacketID(Protocol.CLI_GW_ENTER_TEST);
            CAssert.AreEqual(pkt.GetPacketID(), Protocol.CLI_GW_ENTER_TEST);

            string Key_Name  = "Name";
            string Key_Price = "Price";
            string Key_List  = "TL";

            string Value_Name   = "Apple";
            int    Value_Price  = 100;
            string List_Value_1 = "1";
            string List_Value_2 = "2";
            string List_Value_3 = "3";

            JsonData test_json = new JsonData();

            test_json[Key_Name]  = Value_Name;
            test_json[Key_Price] = Value_Price;

            JsonArray test_array = new JsonArray();

            test_array.Add(List_Value_1);
            test_array.Add(List_Value_2);
            test_array.Add(List_Value_3);
            test_json[Key_List] = test_array.Root;

            pkt.AddJsonData(test_json);

            int total_size = Packet.PACKET_HEAD_LENGTH + sizeof(short) + test_json.ToString().Length;

            CAssert.AreEqual(total_size, (int)pkt.Size);

            pkt.ResetBufferIndex();

            JsonData get_json = pkt.GetJsonData();

            CAssert.AreEqual(get_json[Key_Name], test_json[Key_Name]);
            CAssert.AreEqual(get_json[Key_Price], test_json[Key_Price]);
            CAssert.AreEqual(JTokenType.Array, get_json[Key_List].Type);

            JsonArray get_array = new JsonArray(get_json[Key_List]);

            CAssert.AreEqual((string)get_array[0], List_Value_1);
            CAssert.AreEqual((string)get_array[1], List_Value_2);
            CAssert.AreEqual((string)get_array[2], List_Value_3);

            pkt.Release();
            CAssert.IsNull(pkt.Buf);

            buffer = null;
        }