private async Task <IHost> CreateHost(string[] args) { var persistenceContextProvider = this.DeterminePersistenceContextProvider(args); var persistenceContext = persistenceContextProvider.CreateNewConfigurationContext(); this.LoadGameClientDefinitions(persistenceContext); var chatServerDefinition = persistenceContext.Get <ChatServerDefinition>().First(); var host = Host.CreateDefaultBuilder() .ConfigureLogging(configureLogging => { configureLogging.ClearProviders(); var settings = new Log4NetSettings { ConfigFile = Log4NetConfigFilePath, Watch = true }; configureLogging.AddLog4Net(settings); }) .ConfigureServices(c => c.AddSingleton(this.servers) .AddSingleton(chatServerDefinition) .AddSingleton(chatServerDefinition.ConvertToSettings()) .AddSingleton(IpAddressResolverFactory.DetermineIpResolver(args)) .AddSingleton(this.gameServers) .AddSingleton(this.gameServers.Values) .AddSingleton(persistenceContextProvider) .AddSingleton <IServerConfigurationChangeListener, ServerConfigurationChangeListener>() .AddSingleton <ILoginServer, LoginServer>() .AddSingleton <IGuildServer, GuildServer>() .AddSingleton <IFriendServer, FriendServer>() .AddSingleton <IChatServer, ChatServer>() .AddSingleton <ConnectServerContainer>() .AddSingleton <IEnumerable <IConnectServer> >(provider => provider.GetService <ConnectServerContainer>()) .AddSingleton <GameServerContainer>() .AddHostedService(provider => provider.GetService <IChatServer>()) .AddHostedService(provider => provider.GetService <ConnectServerContainer>()) .AddHostedService(provider => provider.GetService <GameServerContainer>()) .AddHostedService <AdminPanel>() .AddSingleton(new AdminPanelSettings(this.DetermineAdminPort(args))) .AddHostedService <ApiHost>()) .Build(); Log.Info("Host created"); this.servers.Add(host.Services.GetService <IChatServer>()); Log.Info("Starting host..."); var stopwatch = new Stopwatch(); stopwatch.Start(); await host.StartAsync(default);
/// <summary> /// The main entry point for the application. /// </summary> /// <param name="args">The arguments. </param> internal static void Main(string[] args) { // todo: create with HostBuilder var loggerFactory = new NullLoggerFactory().AddLog4Net(Log4NetConfigFilePath, true); logger = loggerFactory.CreateLogger <Program>(); var addressResolver = IpAddressResolverFactory.DetermineIpResolver(args, loggerFactory); var settings = new Settings("ChatServer.cfg"); var serviceContainer = new ServiceContainer(); serviceContainer.AddService(typeof(ILoggerFactory), loggerFactory); int chatServerListenerPort = settings.ChatServerListenerPort ?? 55980; int exDbPort = settings.ExDbPort ?? 55906; string exDbHost = settings.ExDbHost ?? "127.0.0.1"; try { // To make the chat server use our configured encryption key, we need to trick a bit. We add an endpoint with a special client version which is defined in the plugin. var configuration = new ChatServerSettings(); configuration.Endpoints.Add(new ChatServerEndpoint { ClientVersion = ConfigurableNetworkEncryptionPlugIn.Version, NetworkPort = chatServerListenerPort }); var pluginManager = new PlugInManager(null, loggerFactory, serviceContainer); pluginManager.DiscoverAndRegisterPlugInsOf <INetworkEncryptionFactoryPlugIn>(); var chatServer = new ChatServer(configuration, addressResolver, loggerFactory, pluginManager); chatServer.Start(); var exDbClient = new ExDbClient(exDbHost, exDbPort, chatServer, chatServerListenerPort, loggerFactory); logger.LogInformation("ChatServer started and ready"); while (Console.ReadLine() != "exit") { // keep application running } exDbClient.Disconnect(); chatServer.Shutdown(); } catch (Exception ex) { logger.LogCritical(ex, "Unexpected error occured"); } }
/// <summary> /// The main entry point for the application. /// </summary> /// <param name="args">The arguments. </param> internal static void Main(string[] args) { var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); XmlConfigurator.ConfigureAndWatch(logRepository, new FileInfo(Log4NetConfigFilePath)); var addressResolver = IpAddressResolverFactory.DetermineIpResolver(args); var settings = new Settings("ChatServer.cfg"); int chatServerListenerPort = settings.ChatServerListenerPort ?? 55980; int exDbPort = settings.ExDbPort ?? 55906; string exDbHost = settings.ExDbHost ?? "127.0.0.1"; try { // To make the chat server use our configured encryption key, we need to trick a bit. We add an endpoint with a special client version which is defined in the plugin. var configuration = new ChatServerSettings(); configuration.Endpoints.Add(new ChatServerEndpoint { ClientVersion = ConfigurableNetworkEncryptionPlugIn.Version, NetworkPort = chatServerListenerPort }); var chatServer = new ChatServer(configuration, null, addressResolver); chatServer.Start(); var exDbClient = new ExDbClient(exDbHost, exDbPort, chatServer, chatServerListenerPort); Log.Info("ChatServer started and ready"); while (Console.ReadLine() != "exit") { // keep application running } exDbClient.Disconnect(); chatServer.Shutdown(); } catch (Exception ex) { Log.Fatal("Unexpected error occured", ex); } }
/// <summary> /// Initializes a new instance of the <see cref="Program"/> class. /// Constructor for the main entry program. /// </summary> /// <param name="args">The command line args.</param> public Program(string[] args) { if (args.Contains("-demo")) { this.persistenceContextProvider = new InMemoryPersistenceContextProvider(); var initialization = new DataInitialization(this.persistenceContextProvider); initialization.CreateInitialData(); } else { this.persistenceContextProvider = this.PrepareRepositoryManager(args.Contains("-reinit"), args.Contains("-autoupdate")); } var ipResolver = IpAddressResolverFactory.DetermineIpResolver(args); Log.Info("Start initializing sub-components"); var signalRServerObserver = new SignalRGameServerStateObserver(); var serverConfigListener = new ServerConfigurationChangeListener(this.servers, signalRServerObserver); var persistenceContext = this.persistenceContextProvider.CreateNewConfigurationContext(); var loginServer = new LoginServer(); var chatServerDefinition = persistenceContext.Get <ChatServerDefinition>().First(); var chatServer = new ChatServer(chatServerDefinition.ConvertToSettings(), signalRServerObserver, ipResolver); this.servers.Add(chatServer); var guildServer = new GuildServer(this.gameServers, this.persistenceContextProvider); var friendServer = new FriendServer(this.gameServers, chatServer, this.persistenceContextProvider); var connectServers = new Dictionary <GameClientDefinition, IGameServerStateObserver>(); ClientVersionResolver.DefaultVersion = new ClientVersion(6, 3, ClientLanguage.English); foreach (var gameClientDefinition in persistenceContext.Get <GameClientDefinition>()) { ClientVersionResolver.Register(gameClientDefinition.Version, new ClientVersion(gameClientDefinition.Season, gameClientDefinition.Episode, gameClientDefinition.Language)); } foreach (var connectServerDefinition in persistenceContext.Get <ConnectServerDefinition>()) { var clientVersion = new ClientVersion(connectServerDefinition.Client.Season, connectServerDefinition.Client.Episode, connectServerDefinition.Client.Language); var connectServer = ConnectServerFactory.CreateConnectServer(connectServerDefinition, signalRServerObserver, clientVersion); this.servers.Add(connectServer); if (!connectServers.TryGetValue(connectServerDefinition.Client, out var observer)) { connectServers[connectServerDefinition.Client] = connectServer; } else { Log.WarnFormat($"Multiple connect servers for game client '{connectServerDefinition.Client.Description}' configured. Only one per client makes sense."); if (!(observer is MulticastConnectionServerStateObserver)) { var multicastObserver = new MulticastConnectionServerStateObserver(); multicastObserver.AddObserver(observer); multicastObserver.AddObserver(connectServer); connectServers[connectServerDefinition.Client] = multicastObserver; } } } Log.Info("Start initializing game servers"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); foreach (var gameServerDefinition in persistenceContext.Get <DataModel.Configuration.GameServerDefinition>()) { using (ThreadContext.Stacks["gameserver"].Push(gameServerDefinition.ServerID.ToString())) { var gameServer = new GameServer(gameServerDefinition, guildServer, loginServer, this.persistenceContextProvider, friendServer, signalRServerObserver); foreach (var endpoint in gameServerDefinition.Endpoints) { gameServer.AddListener(new DefaultTcpGameServerListener(endpoint, gameServer.ServerInfo, gameServer.Context, connectServers[endpoint.Client], ipResolver)); } this.servers.Add(gameServer); this.gameServers.Add(gameServer.Id, gameServer); Log.InfoFormat("Game Server {0} - [{1}] initialized", gameServer.Id, gameServer.Description); } } stopwatch.Stop(); Log.Info($"All game servers initialized, elapsed time: {stopwatch.Elapsed}"); var adminPort = this.DetermineAdminPort(args); Log.Info($"Start initializing admin panel for port {adminPort}."); this.adminPanel = new AdminPanel(adminPort, this.servers, this.persistenceContextProvider, serverConfigListener, Log4NetConfigFilePath); Log.Info($"Admin panel initialized, port {adminPort}."); if (args.Contains("-autostart")) { chatServer.Start(); foreach (var gameServer in this.gameServers.Values) { gameServer.Start(); } foreach (var connectServer in this.servers.OfType <IConnectServer>()) { connectServer.Start(); } } }