Exemple #1
0
        /// <summary>
        /// Returns a Task that completes when shutdown is triggered via the given token, Ctrl+C or SIGTERM.
        /// </summary>
        /// <param name="host">The running <see cref="IGameHost"/>.</param>
        /// <param name="token">The token to trigger shutdown.</param>
        public static async Task WaitForShutdownAsync(this IGameHost host, CancellationToken token = default(CancellationToken))
        {
            var done = new ManualResetEventSlim(false);

            using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token))
                using (var lifetime = new GameHostLifetime(cts, done, shutdownMessage: string.Empty))
                    try
                    {
                        await host.WaitForTokenShutdownAsync(cts.Token);

                        lifetime.SetExitedGracefully();
                    }
                    finally
                    {
                        done.Set();
                    }
        }
Exemple #2
0
        static async Task RunAsync(this IGameHost host, CancellationToken token, string startupMessage)
        {
            try
            {
                await host.StartAsync(token);

                var hostingEnvironment = host.Services.GetService <IHostEnvironment>();
                var options            = host.Services.GetRequiredService <GameHostOptions>();

                if (!options.SuppressStatusMessages)
                {
                    Console.WriteLine($"Hosting environment: {hostingEnvironment.EnvironmentName}");
                    Console.WriteLine($"Content root path: {hostingEnvironment.ContentRootPath}");

                    var serverAddresses = host.ServerFeatures?.Get <IServerAddressesFeature>()?.Addresses;
                    if (serverAddresses != null)
                    {
                        foreach (var address in serverAddresses)
                        {
                            Console.WriteLine($"Now listening on: {address}");
                        }
                    }

                    if (!string.IsNullOrEmpty(startupMessage))
                    {
                        Console.WriteLine(startupMessage);
                    }
                }

                await host.WaitForTokenShutdownAsync(token);
            }
            finally
            {
#if !NET2
                if (host is IAsyncDisposable asyncDisposable)
                {
                    await asyncDisposable.DisposeAsync().ConfigureAwait(false);
                }
                else
#endif
                host.Dispose();
            }
        }