Exemple #1
0
        protected Task Send(DevToolsQueue queue, JObject o, CancellationToken token)
        {
            Log("protocol", $"to-{queue.Id}: {GetFromOrTo(o)} {o}");
            var msg   = o.ToString(Formatting.None);
            var bytes = Encoding.UTF8.GetBytes(msg);

            return(_runLoop.Send(bytes, token, queue));
        }
Exemple #2
0
        private async Task Send(WebSocket to, JObject o, CancellationToken token)
        {
            string sender = browser == to ? "Send-browser" : "Send-ide";

            //if (method != "Debugger.scriptParsed" && method != "Runtime.consoleAPICalled")
            Log("protocol", $"{sender}: " + JsonConvert.SerializeObject(o));
            byte[] bytes = Encoding.UTF8.GetBytes(o.ToString());

            DevToolsQueue queue = GetQueueForSocket(to);

            Task task = queue.Send(bytes, token);

            if (task != null)
            {
                await _channelWriter.WriteAsync(task, token);
            }
        }
Exemple #3
0
 protected async Task RunLoopAsync(WasmDebuggerConnection ideConn, WasmDebuggerConnection browserConn, CancellationTokenSource cts)
 {
     try
     {
         this.ide                 = new DevToolsQueue(ideConn);
         this.browser             = new DevToolsQueue(browserConn);
         ideConn.OnReadAsync      = ProcessIdeMessage;
         browserConn.OnReadAsync  = ProcessBrowserMessage;
         _runLoop                 = new(new[] { ide, browser }, logger);
         _runLoop.RunLoopStopped += RunLoopStopped;
         await _runLoop.RunAsync(cts);
     }
     finally
     {
         _runLoop?.Dispose();
         _runLoop = null;
     }
 }
Exemple #4
0
        private void Send(WebSocket to, JObject o, CancellationToken token)
        {
            string sender = browser == to ? "Send-browser" : "Send-ide";

            string method = o["method"]?.ToString();

            //if (method != "Debugger.scriptParsed" && method != "Runtime.consoleAPICalled")
            Log("protocol", $"{sender}: " + JsonConvert.SerializeObject(o));
            byte[] bytes = Encoding.UTF8.GetBytes(o.ToString());

            DevToolsQueue queue = GetQueueForSocket(to);

            Task task = queue.Send(bytes, token);

            if (task != null)
            {
                pending_ops.Add(task);
            }
        }
Exemple #5
0
        // , HttpContext context)
        public async Task Run(Uri browserUri, WebSocket ideSocket)
        {
            Log("debug", $"DevToolsProxy: Starting on {browserUri}");
            using (this.ide = ideSocket)
            {
                Log("verbose", $"DevToolsProxy: IDE waiting for connection on {browserUri}");
                queues.Add(new DevToolsQueue(this.ide));
                using (this.browser = new ClientWebSocket())
                {
                    this.browser.Options.KeepAliveInterval = Timeout.InfiniteTimeSpan;
                    await this.browser.ConnectAsync(browserUri, CancellationToken.None);

                    queues.Add(new DevToolsQueue(this.browser));

                    Log("verbose", $"DevToolsProxy: Client connected on {browserUri}");
                    var x = new CancellationTokenSource();

                    pending_ops.Add(ReadOne(browser, x.Token));
                    pending_ops.Add(ReadOne(ide, x.Token));
                    pending_ops.Add(side_exception.Task);
                    pending_ops.Add(client_initiated_close.Task);

                    try
                    {
                        while (!x.IsCancellationRequested)
                        {
                            Task task = await Task.WhenAny(pending_ops.ToArray());

                            //logger.LogTrace ("pump {0} {1}", task, pending_ops.IndexOf (task));
                            if (task == pending_ops[0])
                            {
                                string msg = ((Task <string>)task).Result;
                                if (msg != null)
                                {
                                    pending_ops[0] = ReadOne(browser, x.Token); //queue next read
                                    ProcessBrowserMessage(msg, x.Token);
                                }
                            }
                            else if (task == pending_ops[1])
                            {
                                string msg = ((Task <string>)task).Result;
                                if (msg != null)
                                {
                                    pending_ops[1] = ReadOne(ide, x.Token); //queue next read
                                    ProcessIdeMessage(msg, x.Token);
                                }
                            }
                            else if (task == pending_ops[2])
                            {
                                bool res = ((Task <bool>)task).Result;
                                throw new Exception("side task must always complete with an exception, what's going on???");
                            }
                            else if (task == pending_ops[3])
                            {
                                bool res = ((Task <bool>)task).Result;
                                Log("verbose", $"DevToolsProxy: Client initiated close from {browserUri}");
                                x.Cancel();
                            }
                            else
                            {
                                //must be a background task
                                pending_ops.Remove(task);
                                DevToolsQueue queue = GetQueueForTask(task);
                                if (queue != null)
                                {
                                    Task tsk = queue.Pump(x.Token);
                                    if (tsk != null)
                                    {
                                        pending_ops.Add(tsk);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Log("error", $"DevToolsProxy::Run: Exception {e}");
                        //throw;
                    }
                    finally
                    {
                        if (!x.IsCancellationRequested)
                        {
                            x.Cancel();
                        }
                    }
                }
            }
        }