public void CommonTest()
        {
            PooledByteBufferAllocator pbba = new PooledByteBufferAllocator();
            IByteBuffer bf = pbba.CompositeBuffer();

            bf.WriteUnsignedShort(20);
            bf.WriteBoolean(true);
            bf.WriteInt(32);
            bf.WriteInt(1);
            bf.WriteInt(2);
            bf.WriteInt(3);
            bf.WriteInt(4);
            bf.WriteInt(5);
            bf.WriteInt(6);
            bf.WriteByte(0xff);

            SimplDataType sdt = ByteStreamToObjectConverter.Deserialize <SimplDataType>(bf);


            Assert.AreEqual(20, sdt.head);
            Assert.AreEqual(true, sdt.result);
            Assert.AreEqual(32, sdt.length);
            Assert.AreEqual(0xff, sdt.XOR);

            //System.Console.ReadLine();
        }
        public void Test_Package_simple_Type()
        {
            PooledByteBufferAllocator pbba = new PooledByteBufferAllocator();
            IByteBuffer bf = pbba.CompositeBuffer();

            bf.WriteByte(0xef);
            bf.WriteByte(0xef);                                     // 超始标识
            bf.SetUnsignedInt(bf.WriterIndex, 2000);                // 数据包ID
            bf.SetWriterIndex(bf.WriterIndex + sizeof(UInt32));
            bf.WriteUnsignedShort((ushort)CommandIdentifier.Hello); // 命令标识
            bf.WriteInt(6);                                         // 数据长度
            bf.WriteUnsignedShort(2);                               // 内容标识
            bf.WriteInt(0);                                         // 内容长度
            bf.WriteUnsignedShort(20);

            TRPackage ar = ByteStreamToObjectConverter.Deserialize <TRPackage>(bf);

            Assert.AreEqual(ar.delimiter, 0xefef);
            Assert.AreEqual(ar.packageid, 2000);
            Assert.AreEqual(ar.identifier, (ushort)CommandIdentifier.Hello);
            Assert.AreEqual(ar.data_len, 6);
            Assert.AreEqual(ar.content_identifier, 2);
            Assert.AreEqual(ar.content_len, 0);
            Assert.AreEqual(ar.content_obj, null);
            //System.Console.ReadLine();
        }
示例#3
0
        /// <summary>
        /// Serialize目前只支持TR, 因为时间问题,不再过多研究
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static IByteBuffer Serialize <T>(object obj)
        {
            PooledByteBufferAllocator pbba = new PooledByteBufferAllocator();
            IByteBuffer bf = pbba.CompositeBuffer();

            try
            {
                SerializeInner(obj, bf);
            }
            catch (Exception ex)
            {
                bf.Release();
                return(null);
            }


            return(bf);
        }
        public void CommonTest()
        {
            PooledByteBufferAllocator pbba = new PooledByteBufferAllocator();
            IByteBuffer bf = pbba.CompositeBuffer();

            bf.WriteUnsignedShort(20);
            bf.WriteInt(21);
            bf.WriteBoolean(false);
            bf.WriteByte(0xff);

            SimplDataType sdt = ByteStreamToObjectConverter.Deserialize <SimplDataType>(bf);


            Assert.AreEqual(20, sdt.type);
            Assert.AreEqual(21, sdt.id);
            Assert.AreEqual(false, sdt.gender);
            Assert.AreEqual(0xff, sdt.hash);

            //System.Console.ReadLine();
        }
        public void CommonTest()
        {
            PooledByteBufferAllocator pbba = new PooledByteBufferAllocator();
            IByteBuffer bf = pbba.CompositeBuffer();

            bf.WriteUnsignedShort(20);

            bf.WriteBoolean(true);
            bf.WriteUnsignedShort(2);
            bf.WriteInt(21); bf.WriteInt(22);
            bf.WriteInt(23); bf.WriteInt(24);

            bf.WriteUnsignedShort(10);

            bf.WriteInt(25); bf.WriteInt(28); bf.WriteInt(25);
            bf.WriteInt(26); bf.WriteInt(29); bf.WriteInt(26);
            bf.WriteInt(27); bf.WriteInt(30); bf.WriteInt(27);
            bf.WriteInt(28); bf.WriteInt(25); bf.WriteInt(28);
            bf.WriteInt(29); bf.WriteInt(26); bf.WriteInt(29);
            bf.WriteInt(30); bf.WriteInt(27); bf.WriteInt(30);
            bf.WriteInt(25); bf.WriteInt(28); bf.WriteInt(25);
            bf.WriteInt(26); bf.WriteInt(29); bf.WriteInt(26);
            bf.WriteInt(27); bf.WriteInt(30); bf.WriteInt(27);
            bf.WriteInt(28); bf.WriteInt(29); bf.WriteInt(30);


            bf.WriteByte(0xff);

            SimplDataType sdt = ByteStreamToObjectConverter.Deserialize <SimplDataType>(bf);


            Assert.AreEqual(20, sdt.type);
            Assert.AreEqual(true, sdt.three_D);
            Assert.AreEqual(0xff, sdt.hash);

            //System.Console.ReadLine();
        }
        public void Test_content_maintaince_string()
        {
            PooledByteBufferAllocator pbba = new PooledByteBufferAllocator();
            IByteBuffer bf = pbba.CompositeBuffer();

            bf.WriteByte(0xef);                                               // 1
            bf.WriteByte(0xef);                                               // 超始标识 1
            bf.SetUnsignedInt(bf.WriterIndex, 2000);                          // 数据包ID 4
            bf.SetWriterIndex(bf.WriterIndex + sizeof(UInt32));
            bf.WriteUnsignedShort((ushort)CommandIdentifier.Report);          // 命令标识 2
            bf.WriteInt(6);                                                   // 数据长度 4
            bf.WriteUnsignedShort((ushort)PackageDataIdnetifier.Maintenance); // 内容标识 2
            bf.WriteInt(100);                                                 // 内容长度 4
            ////////////////写入内容/////////////////////
            bf.WriteByte(0x01);                                               // 1

            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms, encoding: Encoding.ASCII);
            string       bstring;

            bstring = "codes";
            bw.Write(bstring);
            ms.Seek(0, SeekOrigin.Begin);

            // Read the string as raw bytes using FileStream...
            // The first series of bytes is the UTF7 encoded length of the
            // string. In this case, however, it is just the first two bytes.
            int len = ms.ReadByte() & 0x7f;

            len += ms.ReadByte() & 0x80;

            byte[] code_str = new byte[len];
            ms.Read(code_str, 0, len);

            ms.Seek(0, SeekOrigin.Begin);
            bf.WriteByte(ms.ReadByte());
            bf.WriteByte(ms.ReadByte());



            string convertred = Encoding.ASCII.GetString(code_str);


            bf.WriteBytes(code_str);


            ms.Seek(0, SeekOrigin.Begin);
            bf.WriteByte(ms.ReadByte());
            bf.WriteByte(ms.ReadByte());
            bf.WriteBytes(code_str);

            ms.Close();
            ////////////////写入内容/////////////////////
            bf.WriteUnsignedShort(20); //2

            TRPackage ar = ByteStreamToObjectConverter.Deserialize <TRPackage>(bf);

            Assert.AreEqual(ar.delimiter, 0xefef);
            Assert.AreEqual(ar.packageid, 2000);
            Assert.AreEqual(ar.identifier, (ushort)CommandIdentifier.Report);
            Assert.AreEqual(ar.data_len, 6);
            Assert.AreEqual(ar.content_identifier, (ushort)PackageDataIdnetifier.Maintenance);
            Assert.AreEqual(ar.content_len, 100);
            Assert.AreEqual(ar.content_obj.GetType(), typeof(TRCloudMaintenance));
            Assert.AreEqual(20, ar.xor_check_code);
            //System.Console.ReadLine();
        }
        public void Test_content_analysis_result_list()
        {
            PooledByteBufferAllocator pbba = new PooledByteBufferAllocator();
            IByteBuffer bf = pbba.CompositeBuffer();

            bf.WriteByte(0xef);                                                  // 1
            bf.WriteByte(0xef);                                                  // 超始标识 1
            bf.SetUnsignedInt(bf.WriterIndex, 2000);                             // 数据包ID 4
            bf.SetWriterIndex(bf.WriterIndex + sizeof(UInt32));
            bf.WriteUnsignedShort((ushort)CommandIdentifier.Report);             // 命令标识 2
            bf.WriteInt(6);                                                      // 数据长度 4
            bf.WriteUnsignedShort((ushort)PackageDataIdnetifier.AnalyseResults); // 内容标识 2
            bf.WriteInt(100);                                                    // 内容长度 4
            ////////////////写入内容/////////////////////
            bf.WriteInt(23);                                                     // recordid
            bf.WriteInt(24);                                                     // type
            bf.WriteLong(25);                                                    // time
            bf.WriteInt(26);                                                     // analysiscount
            bf.WriteInt(27);                                                     // alarmcount
            bf.WriteBoolean(true);                                               // isalarm


            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms, encoding: Encoding.ASCII);
            string       bstring;

            bstring = "codes";
            bw.Write(bstring);
            ms.Seek(0, SeekOrigin.Begin);

            // Read the string as raw bytes using FileStream...
            // The first series of bytes is the UTF7 encoded length of the
            // string. In this case, however, it is just the first two bytes.
            int len = ms.ReadByte() & 0x7f;

            len += ms.ReadByte() & 0x80;

            byte[] code_str = new byte[len];
            ms.Read(code_str, 0, len);

            ms.Seek(0, SeekOrigin.Begin);
            bf.WriteByte(ms.ReadByte());
            bf.WriteByte(ms.ReadByte());
            bf.WriteBytes(code_str); // softversion


            ms.Seek(0, SeekOrigin.Begin);
            bf.WriteByte(ms.ReadByte());
            bf.WriteByte(ms.ReadByte());
            bf.WriteBytes(code_str);// algversion

            ms.Seek(0, SeekOrigin.Begin);
            bf.WriteByte(ms.ReadByte());
            bf.WriteByte(ms.ReadByte());
            bf.WriteBytes(code_str);// libversion


            ms.Seek(0, SeekOrigin.Begin);
            bf.WriteByte(ms.ReadByte());
            bf.WriteByte(ms.ReadByte());
            bf.WriteBytes(code_str);// username


            // alarm result
            bf.WriteInt(1); // count

            // alarm info
            bf.WriteInt(28);// masstype


            ms.Seek(0, SeekOrigin.Begin);
            bf.WriteByte(ms.ReadByte());
            bf.WriteByte(ms.ReadByte());
            bf.WriteBytes(code_str); //  massname
            bf.WriteFloat(29);       // identensity

            bf.WriteInt(30);

            bf.WriteFloat(31);
            bf.WriteFloat(32);
            bf.WriteFloat(33);
            bf.WriteFloat(34);
            bf.WriteFloat(35);


            bf.WriteFloat(36);
            bf.WriteFloat(37);
            bf.WriteFloat(38);
            bf.WriteFloat(39);
            bf.WriteFloat(40);



            ms.Close();
            ////////////////写入内容/////////////////////
            bf.WriteUnsignedShort(20); //2

            TRPackage ar = ByteStreamToObjectConverter.Deserialize <TRPackage>(bf);

            Assert.AreEqual(ar.delimiter, 0xefef);
            Assert.AreEqual(ar.packageid, 2000);
            Assert.AreEqual(ar.identifier, (ushort)CommandIdentifier.Report);
            Assert.AreEqual(ar.data_len, 6);
            Assert.AreEqual(ar.content_identifier, (ushort)PackageDataIdnetifier.AnalyseResults);
            Assert.AreEqual(ar.content_len, 100);
            Assert.AreEqual(ar.content_obj.GetType(), typeof(TRCloudAnalyseResult));
            Assert.AreEqual(20, ar.xor_check_code);
            //System.Console.ReadLine();
        }