/// <summary> /// Construct message from bytestream. /// </summary> /// <returns>Returns null on failure.</returns> public static Message Deserialize(byte[] data) { eMessageType type = (eMessageType)data[0]; Message message; switch (type) { case eMessageType.LAYOUT: message = new LayoutMessage(); break; case eMessageType.DATA: message = new DataMessage(); break; case eMessageType.PATH: message = new PathMessage(); break; default: message = null; break; } bool isOk = false; if (message != null) { isOk = message.DeserializeSelf(data); } return(isOk ? message : null); }
/// <summary> /// Add or remove paths. Also handles explicit path modification messages, not just data appends. /// </summary> private void ProcessMessage(PathMessage msg) { string error = null; try { Path path; switch (msg.action) { case PathMessage.eAction.ADD_POINT: lock (lockObject) { if (target != null) { path = pathIdMapping[msg.path]; target.Invoke(new Action(() => { path.Add(msg.point); target.Refresh(); })); } } break; case PathMessage.eAction.UPDATE_POINT: lock (lockObject) { if (target != null) { path = pathIdMapping[msg.path]; target.Invoke(new Action(() => path[(int)msg.index] = msg.point)); } } break; case PathMessage.eAction.CLEAR_PATH: lock (lockObject) { if (target != null) { path = pathIdMapping[msg.path]; target.Invoke(new Action(() => path.Clear())); } } break; case PathMessage.eAction.ADD_PATH: bool containsPath; lock (lockObject) { containsPath = pathIdMapping.ContainsKey(msg.path); if (target == null) { // do nothing } else if (!containsPath) { target.Invoke(new Action(() => { path = new Path(); path.Name = msg.name != null ? msg.name : "unnamed"; target.Paths.Add(path); pathIdMapping.Add(msg.path, path); })); } else { throw new InvalidOperationException("Path already exists, cannot add it."); } } break; case PathMessage.eAction.REMOVE_PATH: lock (lockObject) { if (target != null) { path = pathIdMapping[msg.path]; target.Invoke(new Action(() => { target.Paths.Remove(path); pathIdMapping.Remove(msg.path); })); } } break; case PathMessage.eAction.CLEAR_MAP: lock (lockObject) { if (target != null) { target.Invoke(new Action(() => target.Paths.Clear())); pathIdMapping.Clear(); } } break; default: break; } } catch (KeyNotFoundException e) { error = "Specified path does not exist. Add it first."; } catch (InvalidOperationException e) { error = e.Message; } catch (ArgumentOutOfRangeException e) { error = "Invalid path index accessed on target form. Have you assigned multiple MessageProcessors that conflict?"; } }
public static bool Test() { LayoutMessage layoutMsg = new LayoutMessage(); layoutMsg.Count = 3; layoutMsg[0].Name = "GPS"; layoutMsg[0].Id = 0; layoutMsg[0].AppendPathId = 1; layoutMsg[0].UpdateMap = true; layoutMsg[0].AppendPathEnabled = false; layoutMsg[0].Dimension = 3; layoutMsg[0][0] = "Latitude"; layoutMsg[0][1] = "Longitude"; layoutMsg[0][2] = "Altitude"; layoutMsg[1].Name = "Velocity"; layoutMsg[1].Id = 1; layoutMsg[1].AppendPathId = 16; layoutMsg[1].UpdateMap = true; layoutMsg[1].AppendPathEnabled = true; layoutMsg[1].Dimension = 3; layoutMsg[1][0] = "X"; layoutMsg[1][1] = "Y"; layoutMsg[1][2] = "Z"; layoutMsg[2].Name = "Temperature"; layoutMsg[2].Id = 2; layoutMsg[2].AppendPathId = 0; layoutMsg[2].UpdateMap = false; layoutMsg[2].AppendPathEnabled = true; layoutMsg[2].Dimension = 1; layoutMsg[2][0] = "Value"; byte[] layoutMsgBytes = layoutMsg.Serialize(); LayoutMessage layoutMsgRestored = (LayoutMessage)Message.Deserialize(layoutMsgBytes); PathMessage pathMsg = new PathMessage(); pathMsg.action = PathMessage.eAction.UPDATE_POINT; pathMsg.index = 5; pathMsg.path = 9; pathMsg.point = new GeoPoint(47.5, 19, 120); byte[] pathMsgBytes = pathMsg.Serialize(); PathMessage pathMsgRestore = (PathMessage)Message.Deserialize(pathMsgBytes); DataMessage dataMsg = new DataMessage(); dataMsg.Dimension = 3; dataMsg.Id = 12345; dataMsg[0] = 3.1415926f; dataMsg[1] = 2.7182818f; dataMsg[2] = 0.5772156f; byte[] dataMsgBytes = dataMsg.Serialize(); DataMessage dataMsgRestored = (DataMessage)Message.Deserialize(dataMsgBytes); return(true); }