示例#1
0
        private static void Main(string[] args)
        {
            var sysConsole = new SystemConsole();
            var console    = Helpers.CacheEnMasse(sysConsole, out var sysTelemetry, out var frontTelemetry);

            // CHANGE THIS
            int exampleId = 1;

            Core.Example example;

            switch (exampleId)
            {
            case 0:
                example = new MovingTextExample();
                break;

            case 1:
                example = new BouncingBoxExample();
                break;

            default: throw new Exception();
            }

            // wait for a key press before continueing
            sysConsole.ReadKey(true);

            // run the example
            example.Run(console, () => console.Flush());             // console);

            // wait for a key press before displaying statistics
            console.ReadKey(true);

            // display statistics about the drawing
            // using sysConsole to not interfere with the telemetry
            sysConsole.Clear();
            sysConsole.X          = 0;
            sysConsole.Y          = 0;
            sysConsole.Background = ConsoleColor.Black;
            sysConsole.Foreground = ConsoleColor.Gray;

            sysConsole.Write("System WriteCalls: " + sysTelemetry.WriteCalls + "\r\n");
            sysConsole.Write("Front-end write calls:" + frontTelemetry.WriteCalls + "\r\n");
            sysConsole.Write("System Telemetry: " + sysTelemetry + "\r\n");
            sysConsole.Write("Front-end Telemetry: " + frontTelemetry + "\r\n");

            sysConsole.ReadKey(true);
        }
        public async void ReadKey()
        {
            var console = new SystemConsole();

            // no idea how i'm suppose to test this
            // i'm just gonna throw this in some async void
            // and hope it doesn't block the rest of the tests
            // :v

            // i don't care
            try
            {
                Task.Run(async() => console.ReadKey(true));
            }
            catch
            {
            }
        }
        /// <summary>Does a commandline run.</summary>
        void CommandLineRun()
        {
            log.LogInfo("Initializing service <cyan>{0}<default> commandline instance...\nRelease: <magenta>{1}<default>, FileVersion: <magenta>{2}<default>", ServiceName, VersionInfo.AssemblyVersion, VersionInfo.FileVersion);
            var needAdminRights = false;

            if (Platform.IsMicrosoft)
            {
                foreach (var option in CommandlineArguments.Options)
                {
                    switch (option.Name)
                    {
                    case "start":
                    case "stop":
                    case "install":
                    case "uninstall": needAdminRights = true; break;
                    }
                }

                if (needAdminRights && !HasAdminRights)
                {
                    if (Debugger.IsAttached)
                    {
                        throw new InvalidOperationException("Please debug this program in administration mode!");
                    }

                    log.LogNotice("Restarting service with administration rights!");
                    Logger.Close();
                    var processStartInfo = new ProcessStartInfo(CommandlineArguments.Command, CommandlineArguments.ToString(false) + " --wait")
                    {
                        UseShellExecute = true,
                        Verb            = "runas",
                    };
                    Process.Start(processStartInfo);
                    return;
                }
                if (HasAdminRights)
                {
                    log.LogInfo("Current user has <green>admin<default> rights.");
                }
                else
                {
                    log.LogInfo("Running in <red>debug<default> mode <red>without admin<default> rights.");
                }
            }

            bool runCommandLine;
            var  wait = CommandlineArguments.IsOptionPresent("wait");

            if (Debugger.IsAttached)
            {
                runCommandLine = true;
                log.LogInfo("<red>Debugger<default> attached.");
            }
            else
            {
                if (CommandlineArguments.Options.Count == 0)
                {
                    Help();
                    return;
                }
                runCommandLine = CommandlineArguments.IsOptionPresent("run");
            }
            bool isInteractive;

            if (Platform.IsMicrosoft)
            {
                ServiceHelper.ServiceName = ServiceName;
                isInteractive             = Environment.UserInteractive;
                if (CommandlineArguments.IsHelpOptionFound())
                {
                    Help();
                    return;
                }
                foreach (var option in CommandlineArguments.Options)
                {
                    switch (option.Name)
                    {
                    case "start":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.StartService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>start<default> service command, doing commandline run!");
                        }

                        break;

                    case "stop":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.StopService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>stop<default> service command, doing commandline run!");
                        }

                        break;

                    case "install":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.InstallService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>install<default> service command, doing commandline run!");
                        }

                        break;

                    case "uninstall":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.UnInstallService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>uninstall<default> service command, doing commandline run!");
                        }

                        break;
                    }
                }
            }
            else
            {
                isInteractive  = !CommandlineArguments.IsOptionPresent("daemon");
                runCommandLine = true;
            }
            if (runCommandLine)
            {
                // --- start service as program
                ServiceParameters = new ServiceParameters(HasAdminRights, true, isInteractive);
                try
                {
                    RunWorker();
                }
                catch (Exception ex)
                {
                    log.LogError("Error while running service executable in commandline mode.\n" + ex.ToXT());
                }

                // --- exit
            }
            Logger.Flush();
            if (isInteractive && wait)
            {
                SystemConsole.RemoveKeyPressedEvent();
                while (SystemConsole.KeyAvailable)
                {
                    SystemConsole.ReadKey();
                }

                SystemConsole.WriteLine("--- Press <yellow>enter<default> to exit... ---");
                while (SystemConsole.ReadKey().Key != ConsoleKey.Enter)
                {
                }
            }
        }
示例#4
0
        static void Main()
        {
            LogConsole = LogConsole.Create(LogConsoleFlags.None);
            LogConsole.ExceptionMode = LogExceptionMode.Full;

            ParseArgs();
            Header();

            if (LocalEndPoints.Count > 0)
            {
                if (!Quiet)
                {
                    Log.LogInfo(string.Format("Listening at <cyan>{0}", LocalEndPoints.Keys.Join("<default>, <cyan>")));
                }

                bool useConsole = true;
                try { while (SystemConsole.KeyAvailable)
                      {
                          SystemConsole.ReadKey();
                      }
                }
                catch { useConsole = false; }
                DateTime lastEscapePress = DateTime.MinValue;

                List <Task> listenTasks = new List <Task>();
                foreach (TcpListener listener in LocalEndPoints.Values)
                {
                    Task task = Task.Factory.StartNew(() =>
                    {
                        while (!Exit)
                        {
                            TcpClient l_Client = listener.AcceptTcpClient();
                            LogLevel l_Level   = ShowConnects ? LogLevel.Information : LogLevel.Verbose;
                            Log.Write(l_Level, string.Format("Connect from <cyan>{0}", l_Client.Client.RemoteEndPoint));
                            Task.Factory.StartNew(() => Forward(l_Client));
                        }
                    }, TaskCreationOptions.LongRunning);
                    listenTasks.Add(task);
                }

                if (useConsole)
                {
                    while (!Exit)
                    {
                        if (SystemConsole.KeyAvailable)
                        {
                            switch (SystemConsole.ReadKey().Key)
                            {
                            case ConsoleKey.Escape:
                                if (lastEscapePress.AddSeconds(1) <= DateTime.Now)
                                {
                                    Exit = true;
                                    break;
                                }
                                lastEscapePress = DateTime.Now;
                                Log.LogInfo("Press escape again within 1s to exit.");
                                break;
                            }
                        }
                        Thread.Sleep(100);
                    }
                }
                else
                {
                    while (!Exit)
                    {
                        Thread.Sleep(1000);
                    }
                }
            }
            Logger.Flush();
            LogConsole.Close();
        }