From 3ebd5b46be24dd0f92d3ea238a7fdfd3efe3eed3 Mon Sep 17 00:00:00 2001 From: Larry Ewing Date: Tue, 20 Oct 2020 13:31:33 -0500 Subject: [PATCH] [browser][debugger] Clean up MessageId logic to prepare for sessions in the test harness (#43188) * Clean up MessageId logic * Update src/mono/wasm/debugger/DebuggerTestSuite/InspectorClient.cs Co-authored-by: Ankit Jain * Update src/mono/wasm/debugger/DebuggerTestSuite/InspectorClient.cs Co-authored-by: Ankit Jain * Update src/mono/wasm/debugger/DebuggerTestSuite/InspectorClient.cs Co-authored-by: Ankit Jain Co-authored-by: Ankit Jain --- .../debugger/BrowserDebugProxy/DevToolsProxy.cs | 7 +++--- .../debugger/DebuggerTestSuite/DevToolsClient.cs | 7 +----- .../debugger/DebuggerTestSuite/InspectorClient.cs | 29 ++++++++-------------- 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsProxy.cs b/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsProxy.cs index ade381d..204e9b7 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsProxy.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsProxy.cs @@ -195,9 +195,9 @@ namespace Microsoft.WebAssembly.Diagnostics Log("protocol", $"browser: {msg}"); if (res["id"] == null) - pending_ops.Add(OnEvent(new SessionId(res["sessionId"]?.Value()), res["method"].Value(), res["params"] as JObject, token)); + pending_ops.Add(OnEvent(res.ToObject(), res["method"].Value(), res["params"] as JObject, token)); else - OnResponse(new MessageId(res["sessionId"]?.Value(), res["id"].Value()), Result.FromJson(res)); + OnResponse(res.ToObject(), Result.FromJson(res)); } private void ProcessIdeMessage(string msg, CancellationToken token) @@ -206,8 +206,9 @@ namespace Microsoft.WebAssembly.Diagnostics if (!string.IsNullOrEmpty(msg)) { var res = JObject.Parse(msg); + var id = res.ToObject(); pending_ops.Add(OnCommand( - new MessageId(res["sessionId"]?.Value(), res["id"].Value()), + id, res["method"].Value(), res["params"] as JObject, token)); } diff --git a/src/mono/wasm/debugger/DebuggerTestSuite/DevToolsClient.cs b/src/mono/wasm/debugger/DebuggerTestSuite/DevToolsClient.cs index 5caae92..b8fbac8 100644 --- a/src/mono/wasm/debugger/DebuggerTestSuite/DevToolsClient.cs +++ b/src/mono/wasm/debugger/DebuggerTestSuite/DevToolsClient.cs @@ -19,7 +19,7 @@ namespace Microsoft.WebAssembly.Diagnostics TaskCompletionSource side_exit = new TaskCompletionSource(); List pending_writes = new List(); Task current_write; - readonly ILogger logger; + protected readonly ILogger logger; public DevToolsClient(ILogger logger) { @@ -158,10 +158,5 @@ namespace Microsoft.WebAssembly.Diagnostics return false; } - - protected virtual void Log(string priority, string msg) - { - // - } } } diff --git a/src/mono/wasm/debugger/DebuggerTestSuite/InspectorClient.cs b/src/mono/wasm/debugger/DebuggerTestSuite/InspectorClient.cs index 7a8e1c8..e461300 100644 --- a/src/mono/wasm/debugger/DebuggerTestSuite/InspectorClient.cs +++ b/src/mono/wasm/debugger/DebuggerTestSuite/InspectorClient.cs @@ -13,7 +13,7 @@ namespace Microsoft.WebAssembly.Diagnostics { internal class InspectorClient : DevToolsClient { - List<(int, TaskCompletionSource)> pending_cmds = new List<(int, TaskCompletionSource)>(); + Dictionary> pending_cmds = new Dictionary>(); Func onEvent; int next_cmd_id; @@ -22,18 +22,15 @@ namespace Microsoft.WebAssembly.Diagnostics Task HandleMessage(string msg, CancellationToken token) { var res = JObject.Parse(msg); - if (res["id"] == null) - DumpProtocol(string.Format("Event method: {0} params: {1}", res["method"], res["params"])); - else - DumpProtocol(string.Format("Response id: {0} res: {1}", res["id"], res)); if (res["id"] == null) return onEvent(res["method"].Value(), res["params"] as JObject, token); - var id = res["id"].Value(); - var idx = pending_cmds.FindIndex(e => e.Item1 == id); - var item = pending_cmds[idx]; - pending_cmds.RemoveAt(idx); - item.Item2.SetResult(Result.FromJson(res)); + + var id = res.ToObject(); + if (!pending_cmds.Remove(id, out var item)) + logger.LogError($"Unable to find command {id}"); + + item.SetResult(Result.FromJson(res)); return null; } @@ -49,6 +46,9 @@ namespace Microsoft.WebAssembly.Diagnostics } public Task SendCommand(string method, JObject args, CancellationToken token) + => SendCommand(new SessionId(null), method, args, token); + + public Task SendCommand(SessionId sessionId, string method, JObject args, CancellationToken token) { int id = ++next_cmd_id; if (args == null) @@ -62,20 +62,13 @@ namespace Microsoft.WebAssembly.Diagnostics }); var tcs = new TaskCompletionSource(); - pending_cmds.Add((id, tcs)); + pending_cmds[new MessageId(sessionId.sessionId, id)] = tcs; var str = o.ToString(); - //Log ("protocol", $"SendCommand: id: {id} method: {method} params: {args}"); var bytes = Encoding.UTF8.GetBytes(str); Send(bytes, token); return tcs.Task; } - - protected virtual void DumpProtocol(string msg) - { - // Console.WriteLine (msg); - //XXX make logging not stupid - } } } -- 2.7.4