Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
fix debugger attach/detach issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ialex32x committed Nov 15, 2019
1 parent ef133b8 commit 0bf6baf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
58 changes: 34 additions & 24 deletions unity/Assets/Duktape/Source/DuktapeDebugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,34 @@ public static DuktapeDebugger CreateDebugger(IntPtr ctx, int port, int bufferSiz
return _instance;
}

public static void Shutdown()
private static void OnDestroy()
{
if (_instance != null)
{
_instance.Stop();
_instance = null;
}
_instance?.Stop();
}

private void Start(int port)
{
Stop();
_server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_server.Bind(new IPEndPoint(IPAddress.Any, port));
_server.Listen(1);
_server.Listen(8);
_server.BeginAccept(_Accept, _server);
_loop = DuktapeRunner.SetLoop(OnUpdate);
DuktapeRunner.onDestroy += OnDestroy;
}

private void _Accept(IAsyncResult ar)
{
try
{
var socket = _server.EndAccept(ar);
Debug.LogWarningFormat("accept: {0}", socket.RemoteEndPoint);
lock (_pending)
{
if (_pending.Count == 0)
{
socket.NoDelay = true;
_pending.Add(socket);
Debug.LogWarningFormat("accept({0}): {1}", _pending.Count, socket.RemoteEndPoint);
}
else
{
Expand Down Expand Up @@ -127,7 +124,7 @@ private void OnUpdate()
{
if (!_client.Connected || (_client.Poll(1000, SelectMode.SelectRead) && _client.Available == 0))
{
Debug.LogError("dead");
// Debug.LogError("dead");
_client.Close();
_client = null;
}
Expand All @@ -136,6 +133,11 @@ private void OnUpdate()

private void DetachCurrent()
{
if (_client != null)
{
_client.Close();
_client = null;
}
DuktapeDLL.duk_unity_detach_debugger(_ctx, _debugger);
_debugger = IntPtr.Zero;
}
Expand All @@ -147,11 +149,6 @@ private void Stop()
_pending.Clear();
}
DetachCurrent();
if (_client != null)
{
_client.Close();
_client = null;
}
if (_server != null)
{
_server.Close();
Expand Down Expand Up @@ -186,7 +183,11 @@ private static uint duk_unity_debug_read_function(int udata, IntPtr buffer, uint
catch (Exception exception)
{
Debug.LogWarningFormat("debugger connection lost: {0}", exception);
_instance._client = null;
if (_instance != null && _instance._client != null)
{
_instance._client.Close();
_instance._client = null;
}
}
return 0;
}
Expand Down Expand Up @@ -224,7 +225,7 @@ private static uint duk_unity_debug_write_function(int udata, IntPtr buffer, uin
[MonoPInvokeCallback(typeof(DuktapeDLL.duk_unity_debug_peek_function))]
private static uint duk_unity_debug_peek_function(int udata)
{
Debug.LogWarning("duk_unity_debug_peek_function");
// Debug.LogWarning("duk_unity_debug_peek_function");
try
{
if (_instance != null && _instance._client != null)
Expand All @@ -234,32 +235,36 @@ private static uint duk_unity_debug_peek_function(int udata)
if (_instance._client.Poll(1000, SelectMode.SelectRead))
{
var n = _instance._client.Available;
Debug.LogWarningFormat("peek available {0}", n);
// Debug.LogWarningFormat("peek available {0}", n);
if (n > 0)
{
return (uint)n;
}
Debug.LogWarningFormat("remote closed");
// Debug.LogWarningFormat("remote closed");
}
else if (_instance._client.Poll(1000, SelectMode.SelectError))
{
Debug.LogWarningFormat("peek error");
// Debug.LogWarningFormat("peek error");
}
else
{
Debug.LogWarningFormat("no data");
// Debug.LogWarningFormat("no data");
return 0;
}
}
Debug.LogWarningFormat("closing");
// Debug.LogWarningFormat("closing");
_instance._client.Close();
_instance._client = null;
}
}
catch (Exception exception)
{
Debug.LogWarningFormat("debugger connection lost: {0}", exception);
_instance._client = null;
if (_instance != null && _instance._client != null)
{
_instance._client.Close();
_instance._client = null;
}
}
return 0;
}
Expand All @@ -277,14 +282,19 @@ private static void duk_unity_debug_write_flush_function(int udata)
[MonoPInvokeCallback(typeof(DuktapeDLL.duk_unity_debug_request_function))]
private static int duk_unity_debug_request_function(IntPtr ctx, int udata, int nvalues)
{
Debug.LogWarningFormat("duk_unity_debug_request_function: {0}", nvalues);
// Debug.LogWarningFormat("duk_unity_debug_request_function: {0}", nvalues);
return 0;
}

[MonoPInvokeCallback(typeof(DuktapeDLL.duk_unity_debug_detached_function))]
private static void duk_unity_debug_detached_function(IntPtr ctx, int udata)
{
Debug.LogWarningFormat("duk_unity_debug_detached_function");
// Debug.LogWarningFormat("duk_unity_debug_detached_function");
if (_instance != null && _instance._client != null)
{
_instance._client.Close();
_instance._client = null;
}
}
}
}
33 changes: 30 additions & 3 deletions unity/Assets/Duktape/Source/DuktapeRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,28 @@ public class DuktapeRunner : MonoBehaviour
private static int _id;
private static Dictionary<int, Coroutine> _timers = new Dictionary<int, Coroutine>();

private static List<Action> _onDestroyCallbacks = new List<Action>();

public static event Action onDestroy
{
add
{
if (!_onDestroyCallbacks.Contains(value))
{
_onDestroyCallbacks.Add(value);
}
}
remove
{
_onDestroyCallbacks.Remove(value);
}
}

public static DuktapeRunner GetRunner()
{
if (_runner == null)
{
var go = new GameObject {hideFlags = HideFlags.HideAndDontSave};
var go = new GameObject { hideFlags = HideFlags.HideAndDontSave };
GameObject.DontDestroyOnLoad(go);
_runner = go.AddComponent<DuktapeRunner>();
}
Expand All @@ -35,7 +52,7 @@ public static int SetLoop(Action fn)

public static int SetTimeout(DuktapeFunction fn, double ms)
{
return SetTimeout(fn, (float) ms);
return SetTimeout(fn, (float)ms);
}

public static int SetTimeout(DuktapeFunction fn, float ms)
Expand All @@ -47,7 +64,7 @@ public static int SetTimeout(DuktapeFunction fn, float ms)

public static int SetInterval(DuktapeFunction fn, double ms)
{
return SetInterval(fn, (float) ms);
return SetInterval(fn, (float)ms);
}

public static int SetInterval(DuktapeFunction fn, float ms)
Expand Down Expand Up @@ -137,5 +154,15 @@ private IEnumerator _Interval(int id, Action fn, float seconds)
fn();
}
}

private void OnDestroy()
{
var size = _onDestroyCallbacks.Count;
for (var i = 0; i < size; i++)
{
var cb = _onDestroyCallbacks[i];
cb();
}
}
}
}
1 change: 0 additions & 1 deletion unity/Assets/Examples/Source/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ void Awake()

void OnDestroy()
{
DuktapeDebugger.Shutdown();
if (vm.context != null)
{
DuktapeDLL.duk_example_detach_debugger(vm.context.rawValue, IntPtr.Zero);
Expand Down

0 comments on commit 0bf6baf

Please sign in to comment.