Exemplo n.º 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;
        }
Exemplo n.º 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;
        }
Exemplo n.º 3
0
 public static void ParsePacket <T>(byte[] packet, out byte command, out T payload)
 {
     byte[] payload_bytes;
     ParsePacket(packet, out command, out payload_bytes);
     payload = StructInterop.ByteArrayToStruct <T>(payload_bytes);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Wrapper for TransferPacket(byte command, byte[] payload)
 /// </summary>
 /// <remarks>
 /// Command doesn't expect a payload, but returns a response.
 /// </remarks>
 /// <typeparam name="R">Struct type for response</typeparam>
 /// <param name="command">Command to send</param>
 /// <returns>Response struct</returns>
 protected R TransferPacket <R>(byte command)
 {
     byte[] response_bytes = TransferPacket(command);
     return(StructInterop.ByteArrayToStruct <R>(response_bytes));
 }
Exemplo n.º 5
0
 /// <summary>
 /// Wrapper for TransferPacket(byte command, byte[] payload)
 /// </summary>
 ///
 /// <typeparam name="T">Struct type for payload</typeparam>
 /// <typeparam name="R">Struct type for response</typeparam>
 /// <param name="command">Command to send</param>
 /// <param name="payload">Payload struct</param>
 /// <returns>Response struct</returns>
 protected R TransferPacket <T, R>(byte command, T payload)
 {
     byte[] payload_bytes  = StructInterop.StructToByteArray <T>(payload);
     byte[] response_bytes = TransferPacket(command, payload_bytes);
     return(StructInterop.ByteArrayToStruct <R>(response_bytes));
 }