Beispiel #1
0
 public override void TraceBlockOutput(
     LocalState <ValueSet <SingleValue> > normalState,
     LocalState <ValueSet <SingleValue> >?exceptionState,
     LocalState <ValueSet <SingleValue> >?exceptionFinallyState
     )
 {
     if (trace && showStates)
     {
         WriteIndented("--- after transfer ---", 1);
         WriteIndented("normal state:", 1);
         WriteIndented(normalState.ToString(), 2);
         WriteIndented("exception state:", 1);
         WriteIndented(exceptionState?.ToString(), 2);
         WriteIndented("finally state:", 1);
         WriteIndented(exceptionFinallyState?.ToString(), 2);
     }
 }
        public void Kill()
        {
            try
            {
                foreach (Process p in processes)
                {
                    if (!p.HasExited)
                    {
                        p.Kill();
                    }
                }

                processes.Clear();

                foreach (Job j in jobs)
                {
                    j.Close();
                }

                jobs.Clear();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error killing: " + e.Message);
            }
            finally
            {
                if (localState != LocalState.Disconnected)
                {
                    TunnelStateChanged(localState, LocalState.Disconnected);
                }

                localState = LocalState.Disconnected;
                Console.WriteLine("TunnelState: " + localState.ToString());
            }
        }
        public void Run(Action <LocalState> callOnStateChange)
        {
            this.callbackFunction = callOnStateChange;
            if (!File.Exists(binaryAbsolute))
            {
                Console.WriteLine("BrowserStackLocal binary was not found.");
                downloadBinary();
            }

            if (lastPid > 0)
            {
                KillByPid(lastPid);
            }

            if (this.verbose == true)
            {
                Console.WriteLine("Local Started with Arguments -- " + binaryArguments);
            }
            ProcessStartInfo processStartInfo = new ProcessStartInfo()
            {
                FileName               = binaryAbsolute,
                Arguments              = binaryArguments,
                CreateNoWindow         = true,
                WindowStyle            = ProcessWindowStyle.Hidden,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                UseShellExecute        = false
            };

            Process process = new Process();

            process.StartInfo           = processStartInfo;
            process.EnableRaisingEvents = true;
            DataReceivedEventHandler o = new DataReceivedEventHandler((s, e) =>
            {
                if (e.Data != null)
                {
                    output.Append(e.Data);
                    if (this.verbose == true)
                    {
                        Console.WriteLine("BinaryOutput: " + e.Data);
                    }

                    foreach (KeyValuePair <LocalState, Regex> kv in stateMatchers)
                    {
                        Match m = kv.Value.Match(e.Data);
                        if (m.Success)
                        {
                            if (kv.Key == LocalState.Error && m.Groups.Count > 1 && m.Groups[1].Captures.Count > 0)
                            {
                                lastError = m.Groups[1].Captures[0].Value;
                            }

                            if (localState != kv.Key)
                            {
                                TunnelStateChanged(localState, kv.Key);
                            }

                            localState = kv.Key;
                            output.Clear();
                            if (this.verbose == true)
                            {
                                Console.WriteLine("TunnelState: " + localState.ToString());
                            }
                            break;
                        }
                    }
                }
            });

            process.OutputDataReceived += o;
            process.ErrorDataReceived  += o;
            process.Exited             += ((s, e) =>
            {
                if (lastPid == process.Id)
                {
                    lastPid = -1;
                }

                Kill();
                processes.Remove(process);
            });

            ThreadStart ths = new ThreadStart(() =>
            {
                process.Start();
                processes.Add(process);

                lastPid = process.Id;

                Job job = new Job();
                job.AddProcess(process.Handle);
                jobs.Add(job);

                process.BeginOutputReadLine();
                process.BeginErrorReadLine();

                // process.WaitForExit();
                // process.CancelOutputRead();
                // process.CancelErrorRead();
            });

            lastError = null;
            TunnelStateChanged(LocalState.Idle, LocalState.Connecting);

            Thread th = new Thread(ths);

            th.Start();

            AppDomain.CurrentDomain.ProcessExit += new EventHandler((s, e) => Kill());
        }