コード例 #1
0
ファイル: HexProtocol.cs プロジェクト: dyg540/hexlight-gui
        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;
        }
コード例 #2
0
        public static byte[] CreatePacket(byte command, byte[] payload = null)
        {
            List <byte> packetBuilder = new List <byte>();

            if (payload != null && payload.Length > MAX_PAYLOAD_LEN)
            {
                throw new Exception("HLDC payload too large");
            }

            packetBuilder.Add(HLDC_FRAME_DELIMITER);

            // Build the packet header
            HeaderStruct header = new HeaderStruct()
            {
                command = command,
                length  = (byte)((payload != null) ? payload.Length : 0)
            };

            packetBuilder.AddRange(EscapeBytes(StructInterop.StructToByteArray <HeaderStruct>(header)));

            // Build the packet payload
            UInt16 crc = 0;

            if (payload != null)
            {
                byte[] escapedPayload = EscapeBytes(payload);
                packetBuilder.AddRange(escapedPayload);
                crc = CalculateCrc(escapedPayload);
            }

            // BUild the packet footer
            FooterStruct footer = new FooterStruct()
            {
                crc = crc,
            };

            packetBuilder.AddRange(EscapeBytes(StructInterop.StructToByteArray <FooterStruct>(footer)));

            packetBuilder.Add(HLDC_FRAME_DELIMITER);

            if (packetBuilder.Count > MAX_PACKET_LEN)
            {
                throw new Exception("HLDC packet too large");
            }

            return(packetBuilder.ToArray());
        }
コード例 #3
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;
        }
コード例 #4
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);
 }
コード例 #5
0
 public static byte[] CreatePacket <T>(byte command, T struc)
 {
     byte[] payload = StructInterop.StructToByteArray <T>(struc);
     return(CreatePacket(command, payload));
 }
コード例 #6
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));
 }
コード例 #7
0
 /// <summary>
 /// Wrapper for TransferPacket(byte command, byte[] payload)
 /// </summary>
 /// <remarks>
 /// Doesn't expect a response. Any response data is discarded,
 /// but will still block until a response is received.
 /// </remarks>
 /// <typeparam name="T">Struct type for payload</typeparam>
 /// <param name="command">Command to send</param>
 /// <param name="payload">Payload struct</param>
 protected void TransferPacket <T>(byte command, T payload)
 {
     byte[] payload_bytes = StructInterop.StructToByteArray <T>(payload);
     TransferPacket(command, payload_bytes);
 }
コード例 #8
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));
 }