From 5f90563ddaba72a0c565c342c149f3a8a2c77042 Mon Sep 17 00:00:00 2001 From: Vladimir Sigalkin Date: Sun, 26 Jun 2022 21:58:39 +0300 Subject: [PATCH] feat(extOSC): added drown detection in receiver --- .../Editor/Editors/OSCReceiverEditor.cs | 16 ++++++++ .../Scripts/Editor/OSCDefinesManager.cs | 29 ++++++++++----- .../extOSC/Scripts/Editor/OSCMenuOptions.cs | 17 +++++++++ Assets/extOSC/Scripts/OSCReceiver.cs | 37 ++++++++++++++++++- 4 files changed, 89 insertions(+), 10 deletions(-) diff --git a/Assets/extOSC/Scripts/Editor/Editors/OSCReceiverEditor.cs b/Assets/extOSC/Scripts/Editor/Editors/OSCReceiverEditor.cs index dbe2f7a..556a767 100644 --- a/Assets/extOSC/Scripts/Editor/Editors/OSCReceiverEditor.cs +++ b/Assets/extOSC/Scripts/Editor/Editors/OSCReceiverEditor.cs @@ -32,7 +32,11 @@ public class OSCReceiverEditor : UnityEditor.Editor private static readonly GUIContent _closeOnPauseContent = new GUIContent("Close On Pause"); + private static readonly GUIContent _drownContent = new GUIContent("Receiver is drown!"); + private static string _advancedSettingsText = "\"Advanced settings\" are not available for UWP (WSA)."; + + private static string _drownText = "OSCReceiver is unable to process the current number of packets. Try reducing the number of packetss, or turn off optimizations: \"Tools/extOSC/\"."; private static MethodInfo _updateMethod; @@ -108,6 +112,18 @@ public override void OnInspectorGUI() // LOGO OSCEditorInterface.LogoLayout(); + // IS DROWN INDICATE + if (_receiver.IsDrown) + { + GUI.color = Color.red; + using (new GUILayout.VerticalScope(OSCEditorStyles.Box)) + { + GUILayout.Label(_drownContent, OSCEditorStyles.CenterBoldLabel); + GUI.color = _defaultColor; + EditorGUILayout.HelpBox(_drownText, MessageType.Error); + } + } + // INSPECTOR EditorGUILayout.LabelField("Active: " + _receiver.IsStarted, EditorStyles.boldLabel); using (new GUILayout.VerticalScope(OSCEditorStyles.Box)) diff --git a/Assets/extOSC/Scripts/Editor/OSCDefinesManager.cs b/Assets/extOSC/Scripts/Editor/OSCDefinesManager.cs index 77b020c..8414f40 100644 --- a/Assets/extOSC/Scripts/Editor/OSCDefinesManager.cs +++ b/Assets/extOSC/Scripts/Editor/OSCDefinesManager.cs @@ -3,6 +3,7 @@ using UnityEditor; using System; +using System.Linq; namespace extOSC.Editor { @@ -29,22 +30,29 @@ static OSCDefinesManager() public static void SetDefine(string define, bool active) { + // Get all defines groups. var buildTargets = (BuildTargetGroup[]) Enum.GetValues(typeof(BuildTargetGroup)); foreach (var targetGroup in buildTargets) { if (!CheckBuildTarget(targetGroup)) continue; - var scriptingDefines = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup); - if (!scriptingDefines.Contains(define) && active) + // Get all defines. + var definesString = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup); + var defines = definesString.Split(';').ToList(); + + // Setup defines. + if (active) { - scriptingDefines += ";" + define; + if (!defines.Contains(define)) + defines.Add(define); } - else if (!active) + else { - scriptingDefines = scriptingDefines.Replace(define, string.Empty); + defines.Remove(define); } - PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, scriptingDefines); + // Store new defines. + PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, string.Join(';', defines)); } } @@ -52,9 +60,12 @@ public static bool HasDefine(string define) { // Get current define group. var currentBuildTarget = EditorUserBuildSettings.selectedBuildTargetGroup; + + var definesString = PlayerSettings.GetScriptingDefineSymbolsForGroup(currentBuildTarget); + var defines = definesString.Split(';'); - // Check. - return PlayerSettings.GetScriptingDefineSymbolsForGroup(currentBuildTarget).Contains(define); + // Check contain defines. + return defines.Contains(define); } #endregion @@ -63,7 +74,7 @@ public static bool HasDefine(string define) private static bool CheckBuildTarget(BuildTargetGroup buildTarget) { - // Not available id Unknown. + // Not available in Unknown. if (buildTarget == BuildTargetGroup.Unknown) return false; diff --git a/Assets/extOSC/Scripts/Editor/OSCMenuOptions.cs b/Assets/extOSC/Scripts/Editor/OSCMenuOptions.cs index 28abb7f..39c103b 100644 --- a/Assets/extOSC/Scripts/Editor/OSCMenuOptions.cs +++ b/Assets/extOSC/Scripts/Editor/OSCMenuOptions.cs @@ -32,8 +32,12 @@ public static class OSCMenuOptions private const string _settingsUTF8 = _settingsEncoding + "UTF8"; + private const string _settingsDrown = _settingsRoot + "Detect Receiver Drown"; + private const string _encodingDefine = "EXTOSC_UTF8"; + private const string _drownDefine = "EXTOSC_DISABLE_DROWN"; + private const int _settingsIndex = _windowsIndex + 100; private const string _linksRoot = _toolsRoot + "Links/"; @@ -96,6 +100,19 @@ public static bool SettingsSwitchUTF8Validate() return true; } + [MenuItem(_settingsDrown, false, _settingsIndex + 2)] + public static void SettingsDrown() + { + OSCDefinesManager.SetDefine(_drownDefine, !OSCDefinesManager.HasDefine(_drownDefine)); + } + + [MenuItem(_settingsDrown, true)] + public static bool SettingsDrownValidate() + { + Menu.SetChecked(_settingsDrown, !OSCDefinesManager.HasDefine(_drownDefine)); + return true; + } + // LINKS [MenuItem(_linksRoot + "GitHub: Repository", false, _linksIndex)] public static void ShowRepository(MenuCommand menuCommand) diff --git a/Assets/extOSC/Scripts/OSCReceiver.cs b/Assets/extOSC/Scripts/OSCReceiver.cs index 9f1f965..ce6c79a 100644 --- a/Assets/extOSC/Scripts/OSCReceiver.cs +++ b/Assets/extOSC/Scripts/OSCReceiver.cs @@ -6,16 +6,23 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using extOSC.Core; using extOSC.Core.Network; - namespace extOSC { [AddComponentMenu("extOSC/OSC Receiver")] public class OSCReceiver : OSCBase { + #region Static Private Vars + + // Max packets processing time in ms. + private const long kMaxProcessingTime = 20; + + #endregion + #region Public Vars public override bool IsStarted => _receiverBackend.IsAvailable; @@ -76,6 +83,8 @@ public int LocalPort } } + public bool IsDrown => _isDrown; + #endregion #region Private Vars @@ -120,12 +129,18 @@ private OSCReceiverBackend _receiverBackend private readonly Stack _bundleUnbindStack = new Stack(); + private readonly Stopwatch _packetsStopwatch = new Stopwatch(); + private readonly object _lock = new object(); private OSCReceiverBackend __receiverBackend; + private int _previousPacketsCount; + private bool _processMessage; + private bool _isDrown; + #endregion #region Unity Methods @@ -136,6 +151,9 @@ protected virtual void Update() lock (_lock) { + _packetsStopwatch.Reset(); + _packetsStopwatch.Start(); + while (_packets.Count > 0) { var packet = _packets.Dequeue(); @@ -146,6 +164,23 @@ protected virtual void Update() OSCConsole.Received(this, packet); InvokePacket(packet); + +#if !EXTOSC_DISABLE_DROWN + if (_packetsStopwatch.ElapsedMilliseconds > kMaxProcessingTime) + { + var packetsCount = _packets.Count; + + _isDrown = _previousPacketsCount != 0 && _previousPacketsCount > packetsCount; + _previousPacketsCount = packetsCount; + + break; + } + else + { + _isDrown = false; + _previousPacketsCount = 0; + } +#endif } } }