Skip to content

Commit

Permalink
feat(extOSC): added drown detection in receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
Iam1337 committed Jun 26, 2022
1 parent 53070b8 commit 5f90563
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
16 changes: 16 additions & 0 deletions Assets/extOSC/Scripts/Editor/Editors/OSCReceiverEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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))
Expand Down
29 changes: 20 additions & 9 deletions Assets/extOSC/Scripts/Editor/OSCDefinesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEditor;

using System;
using System.Linq;

namespace extOSC.Editor
{
Expand All @@ -29,32 +30,42 @@ 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));
}
}

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
Expand All @@ -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;

Expand Down
17 changes: 17 additions & 0 deletions Assets/extOSC/Scripts/Editor/OSCMenuOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 36 additions & 1 deletion Assets/extOSC/Scripts/OSCReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +83,8 @@ public int LocalPort
}
}

public bool IsDrown => _isDrown;

#endregion

#region Private Vars
Expand Down Expand Up @@ -120,12 +129,18 @@ private OSCReceiverBackend _receiverBackend

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

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
Expand All @@ -136,6 +151,9 @@ protected virtual void Update()

lock (_lock)
{
_packetsStopwatch.Reset();
_packetsStopwatch.Start();

while (_packets.Count > 0)
{
var packet = _packets.Dequeue();
Expand All @@ -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
}
}
}
Expand Down

0 comments on commit 5f90563

Please sign in to comment.