コード例 #1
0
        private bool ConnectSocket(Uri uri)
        {
            _debuggerConnected = false;
            var      socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            EndPoint endpoint;

            if (uri.IsLoopback)
            {
                endpoint = new IPEndPoint(IPAddress.Loopback, uri.Port);
            }
            else
            {
                endpoint = new DnsEndPoint(uri.Host, uri.Port);
            }
            Debug.WriteLine("Connecting to remote debugger at {0}", uri.ToString());
            Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.Run(() => Task.WhenAny(
                                                                                  Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, endpoint, null),
                                                                                  Task.Delay(_debuggerConnectionTimeout)));
            try {
                if (socket.Connected)
                {
                    _debuggerConnected    = true;
                    _stream               = new DebugAdapterProcessStream(new NetworkStream(socket, ownsSocket: true));
                    _stream.Disconnected += OnDisconnected;
                }
                else
                {
                    Debug.WriteLine("Timed out waiting for debugger to connect.", nameof(DebugAdapterRemoteProcess));
                }
            } catch (AggregateException ex) {
                Debug.WriteLine("Error waiting for debugger to connect {0}".FormatInvariant(ex.InnerException ?? ex), nameof(DebugAdapterRemoteProcess));
            }

            return(_debuggerConnected);
        }
コード例 #2
0
ファイル: DebugAdapterProcess.cs プロジェクト: yepeiwen/PTVS
        private void StartProcess(string launchJson)
        {
            var connection = InitializeListenerSocket();

            var json = JObject.Parse(launchJson);
            var exe  = json["exe"].Value <string>();
            var scriptAndScriptArgs = json["args"].Value <string>();
            var cwd = json["cwd"].Value <string>();

            ParseOptions(json["options"].Value <string>());

            var argsList = new List <string> {
                string.IsNullOrWhiteSpace(_interpreterOptions) ? "" : _interpreterOptions,
                PythonToolsInstallPath.GetFile("ptvsd_launcher.py"),
                cwd.Trim('\\'),
                $"{_listenerPort}",
                $"{_processGuid}",
                $"{_debugOptions}",
                "-g"
            };
            var launcherArgs = string.Join(" ", argsList.Where(a => !string.IsNullOrWhiteSpace(a)).Select(ProcessOutput.QuoteSingleArgument));
            var arguments    = $"{launcherArgs} {scriptAndScriptArgs}";

            ProcessStartInfo psi = new ProcessStartInfo {
                FileName               = exe,
                Arguments              = arguments,
                WorkingDirectory       = cwd,
                RedirectStandardError  = false,
                RedirectStandardInput  = false,
                RedirectStandardOutput = false,
                UseShellExecute        = false,
                CreateNoWindow         = false,
            };

            var env = json["env"].Value <JArray>();

            foreach (JObject curValue in env)
            {
                var name  = curValue["name"].Value <string>();
                var value = curValue["value"].Value <string>();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    psi.EnvironmentVariables[name] = value;
                }
            }

            _process = new Process {
                EnableRaisingEvents = true,
                StartInfo           = psi
            };

            _process.Exited += OnExited;
            _process.Start();

            var logger = (IPythonToolsLogger)VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(IPythonToolsLogger));

            try {
                if (connection.Wait(_debuggerConnectionTimeout))
                {
                    var socket = connection.Result;
                    if (socket != null)
                    {
                        _debuggerConnected      = true;
                        _stream                 = new DebugAdapterProcessStream(new NetworkStream(socket, ownsSocket: true));
                        _stream.Initialized    += OnInitialized;
                        _stream.LegacyDebugger += OnLegacyDebugger;
                        if (!string.IsNullOrEmpty(_webBrowserUrl) && Uri.TryCreate(_webBrowserUrl, UriKind.RelativeOrAbsolute, out Uri uri))
                        {
                            OnPortOpenedHandler.CreateHandler(uri.Port, null, null, ProcessExited, LaunchBrowserDebugger);
                        }
                    }
                }
                else
                {
                    Debug.WriteLine("Timed out waiting for debuggee to connect.", nameof(DebugAdapterProcess));
                    logger?.LogEvent(PythonLogEvent.DebugAdapterConnectionTimeout, "Launch");
                }
            } catch (AggregateException ex) {
                Debug.WriteLine("Error waiting for debuggee to connect {0}".FormatInvariant(ex.InnerException ?? ex), nameof(DebugAdapterProcess));
            }

            if (_stream == null && !_process.HasExited)
            {
                _process.Kill();
            }
        }