示例#1
0
            public void StartServer(PkgInfo info)
            {
                foreach (var proc in NodeJSUtil.GetNodeProcesses())
                {
                    NodeJsProcessSet.Add(proc.Id);
                }

                Logger.WriteLine("::StartServer - Starting server in directory " + Path.GetDirectoryName(info.FilePath), Logger.Type.StandardOutput);
                var cmdProcess = new Process();
                var path       = Path.GetDirectoryName(info.FilePath);

                cmdProcess.StartInfo.FileName               = "cmd.exe";
                cmdProcess.StartInfo.Arguments              = $"/C http-server \"{path}\" -p {GetNextBestPort()}";
                cmdProcess.StartInfo.UseShellExecute        = false;
                cmdProcess.StartInfo.CreateNoWindow         = true;
                cmdProcess.StartInfo.RedirectStandardOutput = true;
                cmdProcess.StartInfo.RedirectStandardError  = true;
                if (Directory.GetLogicalDrives().Contains(path))
                {
                    cmdProcess.StartInfo.Arguments = cmdProcess.StartInfo.Arguments.Replace(@"\", @"\\");
                }
                cmdProcess.Start();

                //Assumption will be that it should always be found
                currentProcessPid = 0;
                while (currentProcessPid == 0)
                {
                    try {
                        currentProcessPid = NodeJSUtil.GetNodeProcesses()
                                            .ToList()
                                            .Find(proc => !NodeJsProcessSet.Contains(proc.Id))
                                            .Id;
                    } catch (Exception) {
                    }
                    System.Threading.Thread.Sleep(100);
                }


                var temp = cmdProcess;

                System.Threading.Tasks.Task.Run(() => {
                    while (temp.Handle == IntPtr.Zero)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                    var stdout = "";
                    while (stdout != null)
                    {
                        stdout = temp.StandardOutput.ReadLine();
                        if (stdout != null)
                        {
                            Logger.WriteLine(stdout, Logger.Type.StandardOutput);
                        }
                    }
                });

                System.Threading.Tasks.Task.Run(() => {
                    while (temp.Handle == IntPtr.Zero)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                    var stderr = "";
                    while (stderr != null)
                    {
                        stderr = temp.StandardError.ReadLine();
                        if (stderr != null)
                        {
                            if (stderr.Contains("EADDRINUSE"))
                            {
                                server.IsRunning = false;
                                break;
                            }
                        }
                        Logger.WriteLine(stderr, Logger.Type.StandardOutput);
                    }
                });
                IsRunning = true;
            }
示例#2
0
            public void StartServer(PkgInfo info)
            {
                foreach (var proc in NodeJSUtil.GetNodeProcesses())
                {
                    NodeJsProcessSet.Add(proc.Id);
                }
                Logger.WriteLine("::StartServer - Starting server in directory " + Path.GetDirectoryName(info.FilePath), Logger.Type.StandardOutput);
                var cmdProcess = new Process();
                var path       = Path.GetDirectoryName(info.FilePath);

                cmdProcess.StartInfo.FileName               = "cmd.exe";
                cmdProcess.StartInfo.Arguments              = $"/C http-server \"{path}\" -p {GetNextBestPort()}";
                cmdProcess.StartInfo.UseShellExecute        = false;
                cmdProcess.StartInfo.CreateNoWindow         = true;
                cmdProcess.StartInfo.RedirectStandardOutput = true;
                cmdProcess.StartInfo.RedirectStandardError  = true;
                if (Directory.GetLogicalDrives().Contains(path))
                {
                    cmdProcess.StartInfo.Arguments = cmdProcess.StartInfo.Arguments.Replace(@"\", @"\\");
                }
                cmdProcess.Start();

                //Assumption will be that it should always be found
                currentProcessPid = 0;
                while (currentProcessPid == 0)
                {
                    try {
                        var nodeProcesses = NodeJSUtil.GetNodeProcesses().ToList();
                        if (nodeProcesses.Count > 0)
                        {
                            currentProcessPid = nodeProcesses
                                                .Find(proc => !NodeJsProcessSet.Contains(proc.Id))
                                                .Id;
                        }
                    } catch (Exception e) {
                    }
                    System.Threading.Thread.Sleep(100);
                }

                var temp = cmdProcess;

                while (temp.Handle == IntPtr.Zero)
                {
                    System.Threading.Thread.Sleep(100);
                }

                Stopwatch stopwatchTimeoutTracker = new Stopwatch();

                stopwatchTimeoutTracker.Start();
                IsRunning = false;

                //Couldn't think of a better solution
                //Just gotta hope this works well
                //Otherwise we'll be stuck here forever :(
                var stdout = "";

                while (stdout != null)
                {
                    stdout = temp.StandardOutput.ReadLine();
                    if (stdout != null)
                    {
                        if (stdout.ToLower().Contains("starting up http-server, serving"))
                        {
                            IsRunning = true;
                            break;
                        }
                        Logger.WriteLine(stdout, Logger.Type.StandardOutput);
                    }
                }

                if (!IsRunning && temp.HasExited)
                {
                    var strdErr = temp.StandardError.ReadToEnd();
                    if (strdErr.Contains("EADDRINUSE"))
                    {
                        throw new ServerInitializationException("Address already in use");
                    }
                }
            }