Exemple #1
0
        protected int Execute(
            string path,
            string batch,
            BaseBatchParameter parameter)
        {
            return(this._client.Connect(path, batch, parameter, delegate(RunnerClientConnection connection)
            {
                switch (connection.Command)
                {
                case RunnerCommand.None:
                    break;

                case RunnerCommand.Log:
                    Console.Out.WriteLine(connection.Message);
                    break;

                default:
                    throw new InvalidOperationException(String.Format(
                                                            "unknown command: {0}",
                                                            connection.Command));
                }
            }));
        }
        protected ProcessStartInfo BuildStarter(
            IPEndPoint endPoint,
            string path,
            string batch,
            BaseBatchParameter parameter)
        {
            ProcessStartInfo starter;
            ArgumentBuilder  arguments;

            starter = new ProcessStartInfo(this.UnityEditorFile);
            starter.CreateNoWindow  = false;
            starter.UseShellExecute = false;
            starter.WindowStyle     = ProcessWindowStyle.Hidden;
            arguments = this.ToArgumentBuilder();
            arguments.AppendRunnerArguments(
                endPoint.Address.ToString(),
                endPoint.Port.ToString());
            arguments.AppendRunnerArguments(
                path,
                batch);
            arguments.AppendRunnerArguments(parameter.ToArguments());
            starter.Arguments = arguments.ToString();
            return(starter);
        }
        public int Connect(
            string path,
            string batch,
            BaseBatchParameter parameter,
            Worker worker)
        {
            TcpListener listener;
            bool        stopped;

            listener = null;
            stopped  = false;
            try
            {
                Exception cause;
                int       code;
                bool      completed;
                Thread    thread;
                Process   launcher;

                cause     = null;
                code      = -1;
                completed = false;
                thread    = new Thread(delegate()
                {
                    RunnerClientConnection connection;

                    connection = null;
                    try
                    {
                        TcpClient client;
                        Process process;

                        client     = listener.AcceptTcpClient();
                        connection = new RunnerClientConnection(client);
                        process    = Process.GetProcessById(connection.ProcessId);
                        connection.Acknowledge();
                        while (connection.Read())
                        {
                            worker(connection);
                        }
                        code = connection.ExitCode;
                        process.WaitForExit();
                        completed = true;
                    }
                    catch (Exception throwable)
                    {
                        cause = throwable;
                    }
                    finally
                    {
                        if (connection != null)
                        {
                            connection.Close();
                        }
                    }
                });
                listener = new TcpListener(new IPEndPoint(IPAddress.Loopback, 0));
                listener.Start();
                thread.Start();
                launcher = Process.Start(this.BuildStarter(
                                             (IPEndPoint)listener.LocalEndpoint, path, batch, parameter));
                launcher.WaitForExit();
                if (launcher.ExitCode != 0)
                {
                    listener.Stop();
                    stopped = true;
                }
                thread.Join();
                if (!stopped && cause != null)
                {
                    throw new Exception("failed on worker thread", cause);
                }
                return(completed ? code : launcher.ExitCode);
            }
            finally
            {
                if (listener != null && !stopped)
                {
                    listener.Stop();
                }
            }
        }