BACnet.IP.Bvlc.BvlcHeader.Deserialize C# (CSharp) Method

Deserialize() public method

Deserializes the message from a buffer
public Deserialize ( byte buffer, int offset ) : int
buffer byte The buffer to deserialize from
offset int The offset to begin deserializing
return int
        public int Deserialize(byte[] buffer, int offset)
        {
            if (buffer.ReadUInt8(offset) != Magic)
            {
                throw new Exception("Buffer does not contain a bvlc message");
            }

            this.Function = (FunctionCode)buffer.ReadUInt8(offset + 1);
            this.Length = buffer.ReadUInt16(offset + 2);
            return offset + 4;
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Called whenever a datagram is received
        /// </summary>
        /// <param name="ep">The IPEndPoint of the sending device</param>
        /// <param name="buffer">The buffer containing the datagram</param>
        /// <param name="length">The length of the received datagram</param>
        /// <returns>The inbound netgram to propagate, if any</returns>
        private void _onDatagramReceived(IPEndPoint ep, byte[] buffer, int length)
        {
            int offset = 0;
            BvlcHeader header = null;
            IBvlcMessage message = null;
            NetgramReceivedMessage netgram = null;
            Mac mac = IPUtils.IPEndPointToMac(ep);

            try
            {
                if (length < 4)
                    throw new Exception("Received datagram under 4 bytes long");

                header = new BvlcHeader();
                offset = header.Deserialize(buffer, offset);

                if (header.Length != length)
                    throw new Exception("Received bvlc datagram with non-matching lengths");

                message = _createMessage(header.Function);
                offset = message.Deserialize(buffer, offset);
                lock (_lock)
                {
                    netgram = _processMessage(mac, message, buffer, offset, length);
                }

                if (netgram != null && Session != null)
                    Session.QueueMessage(netgram);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }