Exemple #1
0
        public static void ParsePacket(byte[] packet, out byte command, out byte[] payload)
        {
            var          headerBytes = new ArraySegment <byte>(packet, 0, HEADER_SIZE).ToArray();
            HeaderStruct header      = StructInterop.ByteArrayToStruct <HeaderStruct>(headerBytes);

            // Payload
            if (header.length > 0)
            {
                payload = new ArraySegment <byte>(packet, HEADER_SIZE, header.length).ToArray();
            }
            else
            {
                payload = null;
            }

            var          footerBytes = new ArraySegment <byte>(packet, HEADER_SIZE + header.length, FOOTER_SIZE).ToArray();
            FooterStruct footer      = StructInterop.ByteArrayToStruct <FooterStruct>(footerBytes);

            // Detect protocol errors
            if (header.command == CMD_ERROR)
            {
                if (payload != null && payload.Length > 0)
                {
                    throw HLDCProtocolException.FromCode(payload[0]);
                }
                else
                {
                    throw new HLDCException("Protocol malformed error message");
                }
            }

            command = header.command;
        }
Exemple #2
0
        public static void ParsePacket(byte[] packet, out byte command, out byte[] payload)
        {
            // NOTE: Assumes packet has a complete HLDC frame (excluding the SOF/EOF bytes)
            // Use the HLDCFramer helper class to receive a complete HLDC frame

            var          headerBytes = new ArraySegment <byte>(packet, 0, HEADER_SIZE).ToArray();
            HeaderStruct header      = StructInterop.ByteArrayToStruct <HeaderStruct>(headerBytes);

            // Payload
            if (header.length > 0)
            {
                payload = new ArraySegment <byte>(packet, HEADER_SIZE, header.length).ToArray();
            }
            else
            {
                payload = null;
            }

            var          footerBytes = new ArraySegment <byte>(packet, HEADER_SIZE + header.length, FOOTER_SIZE).ToArray();
            FooterStruct footer      = StructInterop.ByteArrayToStruct <FooterStruct>(footerBytes);

            // Detect protocol errors
            if (header.command == CMD_ERROR)
            {
                if (payload != null && payload.Length > 0)
                {
                    throw HLDCProtocolException.FromCode(payload[0]);
                }
                else
                {
                    throw new HLDCException("HLDC Malformed Error Message");
                }
            }

            command = header.command;
        }