コード例 #1
0
ファイル: ExecServerApp.cs プロジェクト: c00t/stride
        /// <summary>
        /// Runs ExecServer in server mode (waiting for connection from ExecServer clients)
        /// </summary>
        /// <param name="entryAssemblyPath">Path to the client assembly in case we need to start another instance of same process.</param>
        /// <param name="executablePath">Path of the executable to run from this ExecServer instance</param>
        private int RunServer(string entryAssemblyPath, string executablePath, int serverInstanceIndex)
        {
            var address = GetEndpointAddress(executablePath, serverInstanceIndex);

            // TODO: The setting of disabling caching should be done per EXE (via config file) instead of global settings for ExecServer
            var useAppDomainCaching = Environment.GetEnvironmentVariable(DisableExecServerAppDomainCaching) != "true";

            // Start WCF pipe for communication with process
            var execServerApp = new ExecServerRemote(entryAssemblyPath, executablePath, true, useAppDomainCaching, serverInstanceIndex == 0);
            var host          = new ServiceHost(execServerApp);

            host.AddServiceEndpoint(typeof(IExecServerRemote), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
            {
                MaxReceivedMessageSize = int.MaxValue,
                SendTimeout            = TimeSpan.FromHours(1),
                ReceiveTimeout         = TimeSpan.FromHours(1),
            }, address);

            try
            {
                host.Open();
            }
            catch (AddressAlreadyInUseException)
            {
                // Uncomment the following line to see which process got a ExitCodeServerAlreadyInUse
                // File.WriteAllText(Path.Combine(Environment.CurrentDirectory, $"test_ExecServer{Process.GetCurrentProcess().Id}.log"), $"Exit code: {ExitCodeServerAlreadyInUse}\r\n");

                // Silently exit if the server is already running
                return(ExitCodeServerAlreadyInUse);
            }

            Console.WriteLine("Server [{0}] is running", executablePath);

            // Register for shutdown
            execServerApp.ShuttingDown += (sender, args) => host.Close();

            // Wait for the server to shutdown
            execServerApp.Wait();

            return(0);
        }
コード例 #2
0
ファイル: ExecServerApp.cs プロジェクト: c00t/stride
        private const int RetryWait     = 1000;          // in ms

        /// <summary>
        /// Runs the specified arguments copy.
        /// </summary>
        /// <param name="argsCopy">The arguments copy.</param>
        /// <returns>System.Int32.</returns>
        public int Run(string[] argsCopy)
        {
            if (argsCopy.Length == 0)
            {
                Console.WriteLine("Usage ExecServer.exe [/direct executablePath|/server entryAssemblyPath executablePath CPUindex] /shadow [executableArguments]");
                return(0);
            }
            var args = new List <string>(argsCopy);

            if (args[0] == "/direct")
            {
                args.RemoveAt(0);
                var executablePath = ExtractPath(args, "executable");
                var execServerApp  = new ExecServerRemote(executablePath, executablePath, false, false, true);
                int result         = execServerApp.Run(Environment.CurrentDirectory, new Dictionary <string, string>(), args.ToArray(), false, null);
                return(result);
            }

            if (args[0] == "/server")
            {
                args.RemoveAt(0);
                var entryAssemblyPath = ExtractPath(args, "entryAssembly");
                var executablePath    = ExtractPath(args, "executable");
                var cpu = int.Parse(args[0]);
                args.RemoveAt(0);
                int result = 0;
                try
                {
                    result = RunServer(entryAssemblyPath, executablePath, cpu);
                }
                catch (Exception ex)
                {
                    try
                    {
                        var pid     = Process.GetCurrentProcess().Id;
                        var logPath = GetExecServerErrorLogFilePath(executablePath, pid);
                        File.AppendAllText(logPath, $"Unexpected error while trying to run ExecServerApp [{executablePath}]. Exception: {ex}");
                    }
                    catch (Exception)
                    {
                        // Don't try to log an error
                    }
                    result = 1;
                }
                return(result);
            }
            else
            {
                bool useShadowCache = false;
                if (args[0] == "/shadow")
                {
                    args.RemoveAt(0);
                    useShadowCache = true;
                }

                var executablePath   = ExtractPath(args, "executable");
                var workingDirectory = ExtractPath(args, "working directory");

                // Collect environment variables
                var environmentVariables = new Dictionary <string, string>();
                foreach (DictionaryEntry environmentVariable in Environment.GetEnvironmentVariables())
                {
                    environmentVariables.Add((string)environmentVariable.Key, (string)environmentVariable.Value);
                }

                int?debuggerProcessId = null;
                using (var debugger = VisualStudioDebugger.GetAttached())
                {
                    if (debugger != null)
                    {
                        debuggerProcessId = debugger.ProcessId;
                    }
                }

                var result = RunClient(executablePath, workingDirectory, environmentVariables, args, useShadowCache, debuggerProcessId);
                return(result);
            }
        }