Пример #1
0
        void Test_DOM()
        {
            Console.WriteLine("\r\n------------------------ DOM");

             EmberContainer container1;
             EmberContainer frame = new EmberFrame();

             container1 = new EmberSet(new BerTag(DefaultClass, 0));
             container1.Insert(new BerTag(DefaultClass, 0), -1);
             container1.Insert(new BerTag(DefaultClass, 1), 128);
             container1.Insert(new BerTag(DefaultClass, 2), -128);
             container1.Insert(new BerTag(DefaultClass, 3), 255);
             container1.Insert(new BerTag(DefaultClass, 4), -255);
             container1.Insert(new BerTag(DefaultClass, 5), 12345);
             container1.Insert(new BerTag(DefaultClass, 6), -12345);
             container1.Insert(new BerTag(DefaultClass, 7), 16384);
             container1.Insert(new BerTag(DefaultClass, 8), -16384);
             container1.Insert(new BerTag(DefaultClass, 9), 65535);
             container1.Insert(new BerTag(DefaultClass, 10), -65535);
             container1.Insert(new BerTag(DefaultClass, 11), 0);
             container1.Insert(new BerTag(DefaultClass, 12), 127);
             container1.Insert(new BerTag(DefaultClass, 13), -127);
             container1.Insert(new BerTag(DefaultClass, 1111222), 0xFFFFFFFF);
             container1.InsertOid(new BerTag(DefaultClass, 14), new int[] { 1, 3, 6, 0 });
             container1.InsertOid(new BerTag(DefaultClass, 15), new int[] { 1 });
             container1.InsertRelativeOid(new BerTag(DefaultClass, 16), new int[] { 1, 2, 3, 4, 5, 6 });
             frame.Insert(container1);

             container1 = new EmberSequence(new BerTag(DefaultClass, 1));
             container1.Insert(new BerTag(DefaultClass, 3), -0.54321);
             container1.Insert(new BerTag(DefaultClass, 5), "Wuppdich");

             var appDefined = EmberApplicationInterface.CreateApplicationDefinedSequence(new BerTag(BerClass.Application, 889), 2, container1);
             appDefined.Insert(new BerTag(DefaultClass, 100), true);

             frame.Insert(container1);

             var xml1 = GetXml(frame);

             var output = new BerMemoryOutput();
             frame.Encode(output);

             var memory = output.ToArray();
             using(var stream = new FileStream(@"N:\Temp\test.ber", FileMode.Create, FileAccess.Write))
            stream.Write(memory, 0, memory.Length);

             var input = new BerMemoryInput(memory);

             var stopwatch = new Stopwatch();
             stopwatch.Start();

             var asyncReader = new AsyncFrameReader(this);
             asyncReader.ReadBytes(output.Memory);
             var loadedFrame = asyncReader.DetachRoot();

             stopwatch.Stop();
             Console.WriteLine("load tree: {0}ms", stopwatch.ElapsedMilliseconds);

             var xml2 = GetXml(loadedFrame);

             Console.WriteLine(xml1);
             Console.WriteLine(xml2);

             Debug.Assert(xml1 == xml2);
        }
        /// <summary>
        /// Creates a new EmberNode from the TLV the passed BER reader is currently positioned on.
        /// </summary>
        /// <param name="reader">The BER reader to create the node from.</param>
        /// <param name="application">The application interface responsible for creating nodes
        /// with application-defined types.</param>
        /// <returns>A new instance of EmberNode representing the read TLV or null
        /// if the TLV could not be decoded into an EmberNode.
        /// This happens if the TLV has an unsupported type.</returns>
        internal static EmberNode FromReader(BerReaderBase reader, EmberApplicationInterface application)
        {
            var node = null as EmberNode;
            var type = reader.Type;
            var tag  = reader.Tag;

            if (reader.IsContainer)
            {
                switch (type)
                {
#pragma warning disable 618 // EmberFrame is obsolete
                case BerType.Sequence:
                    if (tag == Legacy.EmberFrame.FrameTag)
                    {
                        Debug.WriteLine($"Warning: EmberNode / FromReader / EmberFrame is obsolete");
                        node = new Legacy.EmberFrame();
                    }
                    else
                    {
                        node = new EmberSequence(tag, null);
                    }
                    break;
#pragma warning restore 618

                case BerType.Set:
                    node = new EmberSet(tag);
                    break;

                default:
                {
                    if (application != null)
                    {
                        node = application.CreateNodeFromReader(type, reader);
                    }

                    if (node == null)
                    {
                        Debug.WriteLine($"Warning: Unknown BER container type: {type}");

                        node = new EmberContainer(tag, null, type);
                    }
                    break;
                }
                }
            }
            else
            {
                switch (type)
                {
                case BerType.Boolean:
                    node = new BooleanEmberLeaf(tag, reader.GetBoolean());
                    break;

                case BerType.Integer:
                    if (reader.Length > 4)
                    {
                        node = new LongEmberLeaf(tag, reader.GetLong());
                    }
                    else
                    {
                        node = new IntegerEmberLeaf(tag, reader.GetInteger());
                    }
                    break;

                case BerType.Real:
                    node = new RealEmberLeaf(tag, reader.GetReal());
                    break;

                case BerType.UTF8String:
                    node = new StringEmberLeaf(tag, reader.GetString());
                    break;

                case BerType.OctetString:
                    node = new OctetStringEmberLeaf(tag, reader.GetOctetString());
                    break;

                case BerType.RelativeOid:
                    node = new RelativeOidEmberLeaf(tag, reader.GetRelativeOid());
                    break;

                case BerType.ObjectIdentifier:
                    node = new ObjectIdentifierEmberLeaf(tag, reader.GetObjectIdentifier());
                    break;

                case BerType.Null:
                    node = new NullEmberLeaf(tag);
                    break;

                default:
                    Debug.WriteLine($"Unknown BER value type: {type}");
                    break;
                }
            }

            return(node);
        }