示例#1
0
        /// <summary>
        /// Entrypoint for the <see cref="Program"/>
        /// </summary>
        /// <param name="args">The command line arguments</param>
        /// <returns>The <see cref="Process.ExitCode"/></returns>
        public static async Task <int> Main(string[] args)
        {
            var listArgs = new List <string>(args);
            //first arg is 100% always the update path, starting it otherwise is solely for debugging purposes
            string updatePath;

            if (listArgs.Count > 0)
            {
                updatePath = listArgs[0];
                listArgs.RemoveAt(0);
                if (listArgs.Remove("--attach-debugger"))
                {
                    Debugger.Launch();
                }
            }
            else
            {
                updatePath = null;
            }
            try
            {
                using (var server = serverFactory.CreateServer(listArgs.ToArray(), updatePath))
                {
                    try
                    {
                        using (var cts = new CancellationTokenSource())
                        {
                            void AppDomainHandler(object a, EventArgs b) => cts.Cancel();

                            AppDomain.CurrentDomain.ProcessExit += AppDomainHandler;
                            try
                            {
                                Console.CancelKeyPress += (a, b) =>
                                {
                                    b.Cancel = true;
                                    cts.Cancel();
                                };
                                await server.RunAsync(cts.Token).ConfigureAwait(false);
                            }
                            finally
                            {
                                AppDomain.CurrentDomain.ProcessExit -= AppDomainHandler;
                            }
                        }
                    }
                    catch (OperationCanceledException) { }
                    return(server.RestartRequested ? 1 : 0);
                }
            }
            catch (Exception e)
            {
                if (updatePath != null)
                {
                    File.WriteAllText(updatePath, e.ToString());
                    return(2);
                }
                throw;
            }
        }
示例#2
0
        static async Task <int> Main()
        {
            System.Diagnostics.Debugger.Launch();
            System.Console.OutputEncoding = Encoding.UTF8;
            var app = factory.CreateServer(System.Console.OpenStandardInput(), System.Console.OpenStandardOutput());

            return((int)await app.Run());
        }
示例#3
0
#pragma warning restore SA1401 // Fields must be private

        /// <summary>
        /// Entrypoint for the <see cref="Program"/>
        /// </summary>
        /// <param name="args">The command line arguments</param>
        /// <returns>The <see cref="global::System.Diagnostics.Process.ExitCode"/>.</returns>
        public static async Task <int> Main(string[] args)
        {
            // first arg is 100% always the update path, starting it otherwise is solely for debugging purposes
            var    listArgs   = new List <string>(args);
            string updatePath = null;

            if (listArgs.Count > 0)
            {
                updatePath = listArgs.First();
                listArgs.RemoveAt(0);

                // second arg should be host watchdog version
                if (listArgs.Count > 0 &&
                    Version.TryParse(listArgs.First(), out var hostWatchdogVersion) &&
                    hostWatchdogVersion.Major != HostWatchdogVersion.Major)
                {
                    throw new InvalidOperationException(
                              $"Incompatible host watchdog version ({hostWatchdogVersion}) for server ({HostWatchdogVersion})! A major update was released and a full restart will be required. Please manually offline your servers!");
                }

                if (listArgs.Remove("--attach-debugger"))
                {
                    Debugger.Launch();
                }
            }

            var updatedArgsArray = listArgs.ToArray();

            try
            {
                using var shutdownNotifier = new ProgramShutdownTokenSource();
                var     cancellationToken = shutdownNotifier.Token;
                IServer server;
                try
                {
                    server = await ServerFactory.CreateServer(updatedArgsArray, updatePath, cancellationToken).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    // Console cancelled
                    return(0);
                }

                if (server == null)
                {
                    return(0);
                }

                try
                {
                    await server.Run(cancellationToken).ConfigureAwait(false);
                }
                catch (OperationCanceledException) { }
                return(server.RestartRequested ? 1 : 0);
            }
            catch (Exception e)
            {
                if (updatePath != null)
                {
                    await ServerFactory.IOManager.WriteAllBytes(updatePath, Encoding.UTF8.GetBytes(e.ToString()), default).ConfigureAwait(false);

                    return(2);
                }

                // If you hit an exception debug break on this line it caused the application to crash
                throw;
            }
        }
示例#4
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            ConsoleUtil.SetConsoleCtrlHandler(new ConsoleUtil.ControlCtrlDelegate(HandlerRoutine), true);
            bool success = true;

            Console.WriteLine("正在初始化服务程序......");
            IObjectBuilder builder = new TypeCreator();

            _serverFactory = new ServerFactory();
            try
            {
                GlobalConfig gc = GlobalConfigTool.Load();
                foreach (ServerSuperIO.Config.Server serverCfg in gc.Servers)
                {
                    IServer server = _serverFactory.CreateServer(serverCfg.ServerConfig);
                    server.AddDeviceCompleted    += server_AddDeviceCompleted;
                    server.DeleteDeviceCompleted += server_DeleteDeviceCompleted;
                    server.Start();
                    _serverFactory.AddServer(server);

                    foreach (Config.Device devCfg in serverCfg.Devices)
                    {
                        try
                        {
                            IRunDevice runDev = builder.BuildUp <IRunDevice>(devCfg.AssemblyFile, devCfg.Instance);

                            runDev.DeviceParameter.DeviceID = devCfg.DeviceID;
                            runDev.DeviceDynamic.DeviceID   = devCfg.DeviceID;
                            runDev.CommunicateType          = devCfg.CommunicateType;
                            runDev.Initialize(devCfg.DeviceID);

                            if (server.AddDevice(runDev) != devCfg.DeviceID)
                            {
                                Console.WriteLine("增加设备:" + devCfg.DeviceID + " 失败!");
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            continue;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                success = false;
                Console.WriteLine(ex.Message);
            }

            if (success)
            {
                Console.WriteLine("初始化服务程序完成");
            }

            while ("exit" == Console.ReadLine())
            {
                _serverFactory.RemoveAllServer();
                break;
            }
        }