diff --git a/Assets/Scripts/Multiplayer/MultiplayerSceneSync.cs b/Assets/Scripts/Multiplayer/MultiplayerSceneSync.cs index e4a9c4ea7..9c4518556 100644 --- a/Assets/Scripts/Multiplayer/MultiplayerSceneSync.cs +++ b/Assets/Scripts/Multiplayer/MultiplayerSceneSync.cs @@ -14,6 +14,7 @@ using System; using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -48,6 +49,11 @@ void Start() onLargeDataReceived += OnLargeDataReceived; } + private void Update() + { + ProcessQueue(); + } + void OnDestroy() { onLargeDataReceived -= OnLargeDataReceived; @@ -206,24 +212,12 @@ public async void StartSyncProgressDisplayForSrokes(int TargetPlayerId, LinkedLi foreach (var stroke in strokes) { - int retryCount = 0; - const int maxRetries = 3; - bool acknowledged = false; - while (retryCount < maxRetries) + while (await MultiplayerManager.m_Instance.CheckStrokeReception(stroke, TargetPlayerId)) { - if (await MultiplayerManager.m_Instance.CheckStrokeReception(stroke, TargetPlayerId)) - { - acknowledged = true; - break; - } - retryCount++; await Task.Delay(200); } - if (!acknowledged) - ControllerConsoleScript.m_Instance.AddNewLine($"Stroke {stroke.m_Guid} failed to synchronize after {maxRetries} retries."); - sentStrokes++; SynchHistoryPercentage(TargetPlayerId, strokes.Count, sentStrokes); } @@ -269,27 +263,54 @@ private void SynchHistoryComplete(int id) #region Local infoCard commands - private InfoCardAnimation infoCard; private readonly object infoCardLock = new object(); + private ConcurrentQueue messageQueue = new ConcurrentQueue(); + private InfoCardAnimation infoCard; + private void EnqueueMessage(string message) + { + messageQueue.Enqueue(message); + } - public void DisplaySynchInfo(string text = "Sync Started!") + private void ProcessQueue() //once per frame { - lock (infoCardLock) + if (messageQueue.TryDequeue(out string message)) { if (infoCard == null) { - OutputWindowScript.m_Instance.CreateInfoCardAtController( - InputManager.ControllerName.Brush, - text, - fPopScalar: 1.0f - ); - infoCard = RetrieveInfoCard(); + DisplaySynchInfo(message); + } + else + { + UpdateInfoCard(message); } } } - public InfoCardAnimation RetrieveInfoCard() + private void DisplaySynchInfo(string text) + { + if (infoCard == null) + { + OutputWindowScript.m_Instance.CreateInfoCardAtController( + InputManager.ControllerName.Brush, + text, + fPopScalar: 1.0f + ); + infoCard = RetrieveInfoCard(); + } + else + { + UpdateInfoCard(text); + } + } + + private void UpdateInfoCard(string text) + { + infoCard.GetComponentInChildren().text = text; + infoCard.UpdateHoldingDuration(5f); + } + + private InfoCardAnimation RetrieveInfoCard() { InfoCardAnimation[] allInfoCards = FindObjectsOfType(); foreach (var card in allInfoCards) @@ -303,41 +324,21 @@ public InfoCardAnimation RetrieveInfoCard() return null; } + public void StartSynchInfo() + { + EnqueueMessage("Sync Started!"); + } public void SynchInfoPercentageUpdate() { - lock (infoCardLock) - { - int percentage = (int)((float)SketchMemoryScript.AllStrokesCount() / numberOfCommandsExpected * 100); - string text = $"Sync {percentage}%"; - - if (infoCard == null) - { - DisplaySynchInfo(text); - return; - } - - infoCard.GetComponentInChildren().text = text; - infoCard.UpdateHoldingDuration(5f); - } + int percentage = (int)((float)SketchMemoryScript.AllStrokesCount() / numberOfCommandsExpected * 100); + EnqueueMessage($"Sync {percentage}%"); } public void HideSynchInfo() { - lock (infoCardLock) - { - if (infoCard == null) - { - DisplaySynchInfo("Sync Ended!"); - return; - } - - infoCard.GetComponentInChildren().text = "Sync Ended!"; - infoCard.UpdateHoldingDuration(3.0f); - ControllerConsoleScript.m_Instance.AddNewLine("Sync Ended!"); - } + EnqueueMessage("Sync Ended!"); } - #endregion } diff --git a/Assets/Scripts/SketchControlsScript.cs b/Assets/Scripts/SketchControlsScript.cs index 0def9b8fb..cfdab9e63 100644 --- a/Assets/Scripts/SketchControlsScript.cs +++ b/Assets/Scripts/SketchControlsScript.cs @@ -4862,7 +4862,7 @@ public void IssueGlobalCommand(GlobalCommands rEnum, int iParam1 = -1, SketchSurfacePanel.m_Instance.EatToolsInput(); break; case GlobalCommands.DisplaySynchInfo: - MultiplayerSceneSync.m_Instance.DisplaySynchInfo(); + MultiplayerSceneSync.m_Instance.StartSynchInfo(); break; case GlobalCommands.SynchInfoPercentageUpdate: MultiplayerSceneSync.m_Instance.SynchInfoPercentageUpdate();