public static bool unserialize(byte[] received, ref CommandHeader header, ref string command, ref string data) { if (CommandHeader.getHeader(received, ref header)) { if (header.command_length + header.data_length + CommandHeader.HEADER_SIZE == received.Length) { command = Encoding.ASCII.GetString(received, CommandHeader.HEADER_SIZE, header.command_length); data = Encoding.ASCII.GetString(received, CommandHeader.HEADER_SIZE + header.command_length, (int)header.data_length); return(true); } } return(false); }
public static byte[] serialize(ref CommandHeader header_s, string command_str, string data_str) { byte[] command = Encoding.ASCII.GetBytes(command_str); byte[] data = Encoding.ASCII.GetBytes(data_str); header_s.command_length = (byte)command.Length; header_s.data_length = (Int32)data.Length; byte[] header = header_s.toBytes(); byte[] msg = new byte[header.Length + command.Length + data.Length]; Array.Copy(header, 0, msg, 0, header.Length); Array.Copy(command, 0, msg, header.Length, command.Length); Array.Copy(data, 0, msg, header.Length + command.Length, data.Length); return(msg); }
public static bool getHeader(byte[] received, ref CommandHeader header) { if (received.Length < CommandHeader.HEADER_SIZE) { return false; } else { header.command_length = received[0]; header.last_part = received[CommandHeader.HEADER_SIZE - 1]; if (BitConverter.IsLittleEndian) { byte[] copy = new byte[CommandHeader.HEADER_SIZE]; Array.Copy(received, copy, CommandHeader.HEADER_SIZE); Array.Reverse(copy, 1, 4); header.id_num = BitConverter.ToInt32(copy, 1); Array.Reverse(copy, 5, 4); header.port = BitConverter.ToInt32(copy, 5); Array.Reverse(copy, 9, 4); header.data_length = BitConverter.ToInt32(copy, 9); Array.Reverse(copy, 13, 4); header.part_num = BitConverter.ToInt32(copy, 13); } else { header.id_num = BitConverter.ToInt32(received, 1); header.port = BitConverter.ToInt32(received, 5); header.data_length = BitConverter.ToInt32(received, 9); header.part_num = BitConverter.ToInt32(received, 13); } return true; } }