public void decodeCommandInByteArray(byte[] data) { byte[] allData = concatWithRemainingBuffer(data); PostItCommandType commandType = classifyCommand(allData); PostItCommand command = null; switch (commandType) { case PostItCommandType.Add: command = decodeAddCommand(allData); break; case PostItCommandType.Update: command = decodeUpdateCommand(allData); break; case PostItCommandType.Delete: command = decodeDeleteCommand(allData); break; } if (command != null) { if (commandDecodedEventHandler != null) { commandDecodedEventHandler(command.CommandType, command.CommandData); } buffer = null; } else { buffer = allData; } }
PostItCommand decodeAddCommand(byte[] data) { string commndStr = Encoding.UTF8.GetString(data); if (!commndStr.Contains(Encoding.UTF8.GetString(PostItCommand.ADD_PREFIX)) || !commndStr.Contains(Encoding.UTF8.GetString(PostItCommand.ADD_POSTFIX))) { return(null); } if (!commndStr.StartsWith(Encoding.UTF8.GetString(PostItCommand.ADD_PREFIX))) { return(null); } //start to extract part of the message; //structure of an Add command //<ADD> ID X Y Orientation Size DataType Data </ADD> PostItCommand command = new PostItCommand(); command.CommandType = PostItCommandType.Add; int index = PostItCommand.ADD_PREFIX.Length; PostItNote note = new PostItNote(); //ID byte[] buffer = new byte[4]; Array.Copy(data, index, buffer, 0, 4); note.Id = Utilities.UtilitiesLib.Bytes2Int(buffer); index += 4; //X Array.Copy(data, index, buffer, 0, 4); note.CenterX = Utilities.UtilitiesLib.Bytes2Float(buffer); index += 4; //Y Array.Copy(data, index, buffer, 0, 4); note.CenterY = Utilities.UtilitiesLib.Bytes2Float(buffer); index += 4; //skip 4 bytes of orientation, can be used in the future index += 4; //get size of the content Array.Copy(data, index, buffer, 0, 4); int contentSize = Utilities.UtilitiesLib.Bytes2Int(buffer); index += 4; //get content data type Array.Copy(data, index, buffer, 0, 4); note.DataType = PostItCommand.GetPostItContentType(buffer); index += 4; //start getting content buffer = new byte[contentSize]; Array.Copy(data, index, buffer, 0, buffer.Length); note.ParseContentFromBytes(note.DataType, buffer); command.CommandData = note; return(command); }
PostItCommand decodeUpdateCommand(byte[] data) { string commndStr = Encoding.UTF8.GetString(data); if (!commndStr.Contains(Encoding.UTF8.GetString(PostItCommand.UPDATE_PREFIX)) || !commndStr.Contains(Encoding.UTF8.GetString(PostItCommand.UPDATE_POSTFIX))) { return(null); } if (!commndStr.StartsWith(Encoding.UTF8.GetString(PostItCommand.UPDATE_PREFIX))) { return(null); } //start to extract part of the message; //structure of an Add command //<UPD> ID X Y Orientation </UPD> PostItCommand command = new PostItCommand(); command.CommandType = PostItCommandType.Update; int index = PostItCommand.UPDATE_PREFIX.Length; PostItNote note = new PostItNote(); //ID byte[] buffer = new byte[4]; Array.Copy(data, index, buffer, 0, 4); note.Id = Utilities.UtilitiesLib.Bytes2Int(buffer); index += 4; //X Array.Copy(data, index, buffer, 0, 4); note.CenterX = Utilities.UtilitiesLib.Bytes2Float(buffer); index += 4; //Y Array.Copy(data, index, buffer, 0, 4); note.CenterY = Utilities.UtilitiesLib.Bytes2Float(buffer); //at this moment, do not read the Orientation command.CommandData = note; return(command); }
PostItCommand decodeDeleteCommand(byte[] data) { string commndStr = Encoding.UTF8.GetString(data); if (!commndStr.Contains(Encoding.UTF8.GetString(PostItCommand.DEL_PREFIX)) || !commndStr.Contains(Encoding.UTF8.GetString(PostItCommand.DEL_POSTFIX))) { return(null); } int correctMsgLength = PostItCommand.DEL_PREFIX.Length + PostItCommand.DEL_POSTFIX.Length + sizeof(Int32); if (data.Length != correctMsgLength) { return(null); } PostItCommand command = new PostItCommand(); command.CommandType = PostItCommandType.Delete; byte[] cmdData = new byte[sizeof(Int32)]; Array.Copy(data, PostItCommand.DEL_PREFIX.Length, cmdData, 0, cmdData.Length); command.CommandData = Utilities.UtilitiesLib.Bytes2Int(cmdData); return(command); }