Skip to content

Commit 5f90563

Browse files
committed
feat(extOSC): added drown detection in receiver
1 parent 53070b8 commit 5f90563

File tree

4 files changed

+89
-10
lines changed

4 files changed

+89
-10
lines changed

Assets/extOSC/Scripts/Editor/Editors/OSCReceiverEditor.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public class OSCReceiverEditor : UnityEditor.Editor
3232

3333
private static readonly GUIContent _closeOnPauseContent = new GUIContent("Close On Pause");
3434

35+
private static readonly GUIContent _drownContent = new GUIContent("Receiver is drown!");
36+
3537
private static string _advancedSettingsText = "\"Advanced settings\" are not available for UWP (WSA).";
38+
39+
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/\".";
3640

3741
private static MethodInfo _updateMethod;
3842

@@ -108,6 +112,18 @@ public override void OnInspectorGUI()
108112
// LOGO
109113
OSCEditorInterface.LogoLayout();
110114

115+
// IS DROWN INDICATE
116+
if (_receiver.IsDrown)
117+
{
118+
GUI.color = Color.red;
119+
using (new GUILayout.VerticalScope(OSCEditorStyles.Box))
120+
{
121+
GUILayout.Label(_drownContent, OSCEditorStyles.CenterBoldLabel);
122+
GUI.color = _defaultColor;
123+
EditorGUILayout.HelpBox(_drownText, MessageType.Error);
124+
}
125+
}
126+
111127
// INSPECTOR
112128
EditorGUILayout.LabelField("Active: " + _receiver.IsStarted, EditorStyles.boldLabel);
113129
using (new GUILayout.VerticalScope(OSCEditorStyles.Box))

Assets/extOSC/Scripts/Editor/OSCDefinesManager.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEditor;
44

55
using System;
6+
using System.Linq;
67

78
namespace extOSC.Editor
89
{
@@ -29,32 +30,42 @@ static OSCDefinesManager()
2930

3031
public static void SetDefine(string define, bool active)
3132
{
33+
// Get all defines groups.
3234
var buildTargets = (BuildTargetGroup[]) Enum.GetValues(typeof(BuildTargetGroup));
3335
foreach (var targetGroup in buildTargets)
3436
{
3537
if (!CheckBuildTarget(targetGroup)) continue;
3638

37-
var scriptingDefines = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
38-
if (!scriptingDefines.Contains(define) && active)
39+
// Get all defines.
40+
var definesString = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
41+
var defines = definesString.Split(';').ToList();
42+
43+
// Setup defines.
44+
if (active)
3945
{
40-
scriptingDefines += ";" + define;
46+
if (!defines.Contains(define))
47+
defines.Add(define);
4148
}
42-
else if (!active)
49+
else
4350
{
44-
scriptingDefines = scriptingDefines.Replace(define, string.Empty);
51+
defines.Remove(define);
4552
}
4653

47-
PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, scriptingDefines);
54+
// Store new defines.
55+
PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, string.Join(';', defines));
4856
}
4957
}
5058

5159
public static bool HasDefine(string define)
5260
{
5361
// Get current define group.
5462
var currentBuildTarget = EditorUserBuildSettings.selectedBuildTargetGroup;
63+
64+
var definesString = PlayerSettings.GetScriptingDefineSymbolsForGroup(currentBuildTarget);
65+
var defines = definesString.Split(';');
5566

56-
// Check.
57-
return PlayerSettings.GetScriptingDefineSymbolsForGroup(currentBuildTarget).Contains(define);
67+
// Check contain defines.
68+
return defines.Contains(define);
5869
}
5970

6071
#endregion
@@ -63,7 +74,7 @@ public static bool HasDefine(string define)
6374

6475
private static bool CheckBuildTarget(BuildTargetGroup buildTarget)
6576
{
66-
// Not available id Unknown.
77+
// Not available in Unknown.
6778
if (buildTarget == BuildTargetGroup.Unknown)
6879
return false;
6980

Assets/extOSC/Scripts/Editor/OSCMenuOptions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ public static class OSCMenuOptions
3232

3333
private const string _settingsUTF8 = _settingsEncoding + "UTF8";
3434

35+
private const string _settingsDrown = _settingsRoot + "Detect Receiver Drown";
36+
3537
private const string _encodingDefine = "EXTOSC_UTF8";
3638

39+
private const string _drownDefine = "EXTOSC_DISABLE_DROWN";
40+
3741
private const int _settingsIndex = _windowsIndex + 100;
3842

3943
private const string _linksRoot = _toolsRoot + "Links/";
@@ -96,6 +100,19 @@ public static bool SettingsSwitchUTF8Validate()
96100
return true;
97101
}
98102

103+
[MenuItem(_settingsDrown, false, _settingsIndex + 2)]
104+
public static void SettingsDrown()
105+
{
106+
OSCDefinesManager.SetDefine(_drownDefine, !OSCDefinesManager.HasDefine(_drownDefine));
107+
}
108+
109+
[MenuItem(_settingsDrown, true)]
110+
public static bool SettingsDrownValidate()
111+
{
112+
Menu.SetChecked(_settingsDrown, !OSCDefinesManager.HasDefine(_drownDefine));
113+
return true;
114+
}
115+
99116
// LINKS
100117
[MenuItem(_linksRoot + "GitHub: Repository", false, _linksIndex)]
101118
public static void ShowRepository(MenuCommand menuCommand)

Assets/extOSC/Scripts/OSCReceiver.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@
66

77
using System;
88
using System.Collections.Generic;
9+
using System.Diagnostics;
910

1011
using extOSC.Core;
1112
using extOSC.Core.Network;
1213

13-
1414
namespace extOSC
1515
{
1616
[AddComponentMenu("extOSC/OSC Receiver")]
1717
public class OSCReceiver : OSCBase
1818
{
19+
#region Static Private Vars
20+
21+
// Max packets processing time in ms.
22+
private const long kMaxProcessingTime = 20;
23+
24+
#endregion
25+
1926
#region Public Vars
2027

2128
public override bool IsStarted => _receiverBackend.IsAvailable;
@@ -76,6 +83,8 @@ public int LocalPort
7683
}
7784
}
7885

86+
public bool IsDrown => _isDrown;
87+
7988
#endregion
8089

8190
#region Private Vars
@@ -120,12 +129,18 @@ private OSCReceiverBackend _receiverBackend
120129

121130
private readonly Stack<IOSCBindBundle> _bundleUnbindStack = new Stack<IOSCBindBundle>();
122131

132+
private readonly Stopwatch _packetsStopwatch = new Stopwatch();
133+
123134
private readonly object _lock = new object();
124135

125136
private OSCReceiverBackend __receiverBackend;
126137

138+
private int _previousPacketsCount;
139+
127140
private bool _processMessage;
128141

142+
private bool _isDrown;
143+
129144
#endregion
130145

131146
#region Unity Methods
@@ -136,6 +151,9 @@ protected virtual void Update()
136151

137152
lock (_lock)
138153
{
154+
_packetsStopwatch.Reset();
155+
_packetsStopwatch.Start();
156+
139157
while (_packets.Count > 0)
140158
{
141159
var packet = _packets.Dequeue();
@@ -146,6 +164,23 @@ protected virtual void Update()
146164
OSCConsole.Received(this, packet);
147165

148166
InvokePacket(packet);
167+
168+
#if !EXTOSC_DISABLE_DROWN
169+
if (_packetsStopwatch.ElapsedMilliseconds > kMaxProcessingTime)
170+
{
171+
var packetsCount = _packets.Count;
172+
173+
_isDrown = _previousPacketsCount != 0 && _previousPacketsCount > packetsCount;
174+
_previousPacketsCount = packetsCount;
175+
176+
break;
177+
}
178+
else
179+
{
180+
_isDrown = false;
181+
_previousPacketsCount = 0;
182+
}
183+
#endif
149184
}
150185
}
151186
}

0 commit comments

Comments
 (0)