Esempio n. 1
0
        /// <summary>
        /// Parse byte[] into TLVs
        /// </summary>
        public void ParseByteArrayIntoTlvs(byte[] bytes, int offset)
        {
            log.DebugFormat("bytes.Length {0}, offset {1}", bytes.Length, offset);

            int position = 0;

            TlvCollection.Clear();

            while (position < bytes.Length)
            {
                // The payload type
                var byteArraySegment = new ByteArraySegment(bytes, offset + position, TLVTypeLength.TypeLengthLength);
                var typeLength       = new TLVTypeLength(byteArraySegment);

                // create a TLV based on the type and
                // add it to the collection
                TLV currentTlv = TLVFactory(bytes, offset + position, typeLength.Type);
                if (currentTlv == null)
                {
                    log.Debug("currentTlv == null");
                    break;
                }

                log.DebugFormat("Adding tlv {0}, Type {1}",
                                currentTlv.GetType(), currentTlv.Type);
                TlvCollection.Add(currentTlv);

                // stop at the first end tlv we run into
                if (currentTlv is EndOfLLDPDU)
                {
                    break;
                }

                // Increment the position to seek the next TLV
                position += (currentTlv.TotalLength);
            }

            log.DebugFormat("Done, position {0}", position);
        }
Esempio n. 2
0
        public override string ToColoredString(bool colored)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append('[');
            if (colored)
            {
                builder.Append(AnsiEscapeSequences.Blue);
            }
            builder.Append("LLDPPacket");
            if (colored)
            {
                builder.Append(AnsiEscapeSequences.Reset);
            }
            builder.Append(":");
            IEnumerator <TLV> enumerator = this.TlvCollection.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    TLV   current = enumerator.Current;
                    Match match   = new Regex(@"[^(\.)]([^\.]*)$").Match(current.GetType().ToString());
                    builder.Append(string.Concat(new object[] { " [", match.Groups[0].Value, " length:", current.Length, "]" }));
                }
            }
            finally
            {
                if (enumerator == null)
                {
                }
                enumerator.Dispose();
            }
            builder.Append(']');
            return(builder.ToString());
        }