private void GetContent() { while (true) { ServerStream_priv = ClientSocket.GetStream(); byte[] inStream = new byte[10025]; int n = 0; n = ServerStream_priv.Read(inStream, 0, 10025); if (n != 0) { byte[] data = inStream; BinaryFormatter bf = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream(data)) { object obj = bf.Deserialize(ms); if (obj.GetType() == typeof(Undo_Redo.UndoRedoWrapper)) { this.Undo_command = (Undo_Redo.UndoRedoWrapper)obj; if (Undo_command.Command.Username != Username) { Undo_command = Manager.SaveCommand(Undo_command.Command, Undo_command.Command.IsUndo, Undo_command.Command.IsRedo); } if (Manager.Counter > 1) { if (Counter > 2) { CmdLargeRepair cmdLargeRepair = new CmdLargeRepair(); cmdLargeRepair.isLargeRepair = true; cmdLargeRepair.ID_Command = Guid.NewGuid(); cmdLargeRepair.username = Username; BinaryFormatter bf1 = new BinaryFormatter(); using (MemoryStream data1 = new MemoryStream()) { bf1.Serialize(data1, cmdLargeRepair); ServerStream_priv.Write(data1.ToArray(), 0, data1.ToArray().Length); ServerStream_priv.Flush(); } } else if (Manager.Commands[Manager.Counter - 1].ID_prevCommand != Undo_command.ID_prevCommand) { Manager.Swap(Manager.Commands[Manager.Counter - 2], Manager.Commands[Manager.Counter - 1], Manager.Commands); Counter++; } } this.Rect = Undo_command.Command; if (Rect.EventName == "Rectangle Red") { this.Rect_Red = new RectangleRed(Rect.X, Rect.Y, Rect.Selected, Rect.Deleted, Rect.Moved, Rect.Username, Rect.MovingHelperX, Rect.MovingHelperY, Rect.IsUndo, Rect.IsRedo); Rect_Red.HierarchyID = Rect.HierarchyID; Rect_Red.EventList = Rect.EventList; if (Rect_Red.IsUndo && Rect_Red.Deleted) { DeleteRectangleRed_Invoker_Inverse(this.Rect_Red); } else if (Rect_Red.IsUndo) { DrawRectangleRed_Invoker_Inverse(this.Rect_Red); } else if (Rect_Red.Moved) { MoveRectangleRed_Invoker(this.Rect_Red); } else if (Rect_Red.Deleted) { DeleteRectangleRed_Invoker(this.Rect_Red); } else if (Rect_Red.Selected) { SelectRectangleRed(this.Rect_Red); } else { DrawRectRed(this.Rect_Red); } } else if (Rect.EventName == "Rectangle Blue") { this.Rect_Blue = new RectangleBlue(Rect.X, Rect.Y, Rect.Selected, Rect.Deleted, Rect.Moved, Rect.Username, Rect.MovingHelperX, Rect.MovingHelperY, Rect.IsUndo, Rect.IsRedo); Rect_Blue.HierarchyID = Rect.HierarchyID; Rect_Blue.EventList = Rect.EventList; if (Rect_Blue.IsUndo && Rect_Blue.Deleted) { DeleteRectangleBlue_Invoker_Inverse(this.Rect_Blue); } else if (Rect_Blue.IsUndo) { DrawRectangleBlue_Invoker_Inverse(this.Rect_Blue); } else if (Rect_Blue.Moved) { MoveRectangleBlue_Invoker(this.Rect_Blue); } else if (Rect_Blue.Deleted) { DeleteRectangleBlue_Invoker(this.Rect_Blue); } else if (Rect_Blue.Selected) { SelectRectangleBlue(this.Rect_Blue); } else { DrawRectBlue(this.Rect_Blue); } } else if (Rect.EventName == "Relation") { this.Rel = new Relation(Rect.Start, Rect.End, Rect.Selected, Rect.Deleted, Rect.Moved, Rect.Username, Rect.IsUndo, Rect.IsRedo); Rel.EventList.Add(Rect.EventList[0]); Rel.EventList.Add(Rect.EventList[1]); Rel.HierarchyID = Rect.HierarchyID; if (Rel.IsUndo && Rel.Deleted) { DeleteRelation_Invoker_Inverse(Rel); } else if (Rel.IsUndo) { DrawRelation_Invoker_Inverse(Rel); } else if (Rel.Deleted) { DeleteRelation_Invoker(Rel); } else if (Rel.Moved) { MoveRelation_Invoker(Rel); } else { DrawRelation(Rel); } } else if (Rect.EventName == "Label") { this.Lab = new Objects.Label(Rect.X, Rect.Y, Rect.Selected, Rect.Deleted, Rect.Moved, Rect.Username, Rect.MovingHelperX, Rect.MovingHelperY, Rect.IsUndo, Rect.IsRedo); Lab.HierarchyID = Rect.HierarchyID; Lab.EventList = Rect.EventList; if (Lab.IsUndo && Lab.Deleted) { DeleteLabel_Invoker_Inverse(this.Lab); } else if (Lab.IsUndo) { DrawLabel_Invoker_Inverse(this.Lab); } else if (Lab.Deleted) { DeleteLabel_Invoker(this.Lab); } else if (Lab.Selected) { SelectLabelEvent(this.Lab); } else { DrawLabel(this.Lab); } } } else { Console.WriteLine("This is large repair!"); Manager.Commands = (Undo_Redo.UndoRedoWrapper[])obj; } } } else { break; } } }
public void DoCollaborative() { int RequestCount = 0; byte[] BytesFrom = new byte[10025]; while (true) { try { RequestCount = RequestCount + 1; NetworkStream NetworkStream = ClientSocket.GetStream(); NetworkStream.Read(BytesFrom, 0, 10025); Console.WriteLine("Client - " + ClNo + " - has just done some action!"); BinaryFormatter Bf = new BinaryFormatter(); using (MemoryStream Ms = new MemoryStream(BytesFrom)) { object Obj = Bf.Deserialize(Ms); Model = (Command_Interface.ICommand)Obj; UndoRedoWrapper manager_helper = new UndoRedoWrapper(Model); int Position = 0; if (Model.IsLargeRepair) { BinaryFormatter Bf2 = new BinaryFormatter(); using (MemoryStream Data = new MemoryStream()) { Bf2.Serialize(Data, Program.Undo_Manager.Commands); Program.Broadcast(Data.ToArray(), ClNo, true); } continue; } else if (Model.IsUndo) { Position = Program.Undo_Manager.GetCommand(Model.Username); if (Program.Undo_Manager.ConflictAll(Position)) { Console.WriteLine("There was some error acquired!"); } else { ResultCommand = Program.Undo_Manager.Inverse(manager_helper); ResultCommand = Program.Undo_Manager.SaveCommand(Model, Model.IsUndo, Model.IsRedo); } } else { ResultCommand = Program.Undo_Manager.SaveCommand(Model, Model.IsUndo, Model.IsRedo); } using (MemoryStream data = new MemoryStream()) { Bf.Serialize(data, ResultCommand); Program.Broadcast(data.ToArray(), ClNo, true); } } } catch (Exception ex) { ClientSocket.Close(); Console.WriteLine(ex.ToString()); } } }
public UndoRedoWrapper SaveCommand(Command_Interface.ICommand command, bool isUndo, bool isRedo) { UndoRedoWrapper tempCommand = new UndoRedoWrapper(command); if (GetCommand(command.Username) != -1) { tempCommand.PrevCommand = GetCommand(command.Username); if (isUndo) { tempCommand.IsUndo = true; if (Commands[tempCommand.PrevCommand].IsUndo) { if (Counter == 0) { tempCommand.ID_prevCommand = tempCommand.ID_Command; } else { tempCommand.ID_prevCommand = Commands[Counter - 1].ID_Command; } tempCommand.UndoCommand = Commands[tempCommand.PrevCommand].PrevCommand; tempCommand.PrevCommand = Commands[tempCommand.UndoCommand].PrevCommand; tempCommand.Command.HierarchyID = Commands[tempCommand.UndoCommand].Command.HierarchyID; tempCommand.Command.EventList = Commands[tempCommand.UndoCommand].Command.EventList; Commands[Counter] = tempCommand; Counter++; UndoCounter--; tempCommand.Username = command.Username; SetCommand(tempCommand); return(tempCommand); } else { if (Counter == 0) { tempCommand.ID_prevCommand = tempCommand.ID_Command; } else { tempCommand.ID_prevCommand = Commands[Counter - 1].ID_Command; } tempCommand.UndoCommand = tempCommand.PrevCommand; tempCommand.PrevCommand = Commands[tempCommand.PrevCommand].PrevCommand; tempCommand.Command.HierarchyID = Commands[tempCommand.UndoCommand].Command.HierarchyID; tempCommand.Command.EventList = Commands[tempCommand.UndoCommand].Command.EventList; Commands[Counter] = tempCommand; Counter++; UndoCounter--; tempCommand.Username = command.Username; SetCommand(tempCommand); return(tempCommand); } } else if (isRedo) { tempCommand.IsRedo = true; if (Counter == 0) { tempCommand.ID_prevCommand = tempCommand.ID_Command; } else { tempCommand.ID_prevCommand = Commands[Counter - 1].ID_Command; } tempCommand.IsRedo = true; Commands[tempCommand.PrevCommand].RedoCommand = Counter; tempCommand.Command.HierarchyID = Commands[tempCommand.PrevCommand].Command.HierarchyID; tempCommand.Command.EventList = Commands[tempCommand.PrevCommand].Command.EventList; tempCommand.PrevCommand = Commands[tempCommand.PrevCommand].PrevCommand; Commands[Counter] = tempCommand; Counter++; UndoCounter++; tempCommand.Username = command.Username; SetCommand(tempCommand); return(tempCommand); } else { if (Commands[tempCommand.PrevCommand].IsUndo || Commands[tempCommand.PrevCommand].IsRedo) { tempCommand.PrevCommand = Commands[tempCommand.PrevCommand].PrevCommand; if (Counter == 0) { tempCommand.ID_prevCommand = tempCommand.ID_Command; } else { tempCommand.ID_prevCommand = Commands[Counter - 1].ID_Command; } Commands[Counter] = tempCommand; command.PrevCommand = tempCommand.PrevCommand; Counter++; UndoCounter++; tempCommand.Username = command.Username; SetCommand(tempCommand); return(tempCommand); } else { if (Counter == 0) { tempCommand.ID_prevCommand = tempCommand.ID_Command; } else { tempCommand.ID_prevCommand = Commands[Counter - 1].ID_Command; } Commands[Counter] = tempCommand; command.PrevCommand = tempCommand.PrevCommand; Counter++; UndoCounter++; tempCommand.Username = command.Username; SetCommand(tempCommand); return(tempCommand); } } } else { if (Counter == 0) { tempCommand.ID_prevCommand = tempCommand.ID_Command; } else { tempCommand.ID_prevCommand = Commands[Counter - 1].ID_Command; } tempCommand.PrevCommand = Counter; tempCommand.Command.HierarchyID = command.HierarchyID; command.PrevCommand = tempCommand.PrevCommand; Commands[Counter] = tempCommand; tempCommand.Username = command.Username; SetCommand(tempCommand); Counter++; UndoCounter++; return(tempCommand); } }