/// <summary> /// Creates an instance of ServerGui and begins running a standard application method loop, /// then returns a reference to the created instance. /// </summary> /// <param name="server">The server object this instance should store a reference to.</param> /// <returns>See summary.</returns> public static ServerGui CreateGui(GameServer server) { ServerGui.gui = new ServerGui(server); new Thread(new ThreadStart(ServerGui.Run)).Start(); // Block until the GUI's handles have been created to prevent // invalid cross-thread control access while (!ServerGui.gui.Visible) { Thread.Sleep(1); } return ServerGui.gui; }
/// <summary> /// Starts/runs the server. /// </summary> /// <param name="databaseManager">The database manager used to interact with the master game database.</param> public void Run(/*DatabaseManager databaseManager*/) { this.ServerGui = ServerGui.CreateGui(this); this.ServerGui.AddStatusText(Resources.DatabaseManagerConnectingToDatabase); try { this.DbManager = new MainDatabaseDataContext(); this.ServerGui.AddStatusText(Resources.DatabaseManagerConnectedToDatabase); ChatFilter.Initialize(); PacketSender.Initialize(this); //// TODO: Add all the IProcessables //// this.processers.Add(... this.clientListener = new PlayerListener(); this.clientListener.Start(); this.queryListener = new QueryListener(); this.queryListener.Start(); // Server is now running! this.Running = true; // Run the processing loop... while (this.Running) { this.CleanDisconnectedPlayers(); for (int i = 0; i < this.processers.Count; i++) { this.processers[i].Process(); } // TODO: Do all processing here Thread.Sleep(1); } // Shut down the connection listeners this.clientListener.Stop(); this.queryListener.Stop(); this.ServerGui.AddStatusText(Resources.NetworkListenersShutDown); // Disconnect all clients while (this.Clients.Count > 0) { this.Clients[0].Dispose(); this.Clients.RemoveAt(0); this.ServerGui.UpdatePlayerCount(this.Clients.Count); } // Close database connection // TODO: We didn't actually close anything... this.ServerGui.AddStatusText(Resources.DatabaseManagerConnectionCloseSuccess); // Successfully shut down everything this.ServerGui.AddStatusText(Resources.ServerSuccessfulShutdown); } catch (InvalidOperationException) { this.ServerGui.AddStatusText(Resources.DatabaseManagerConnectionFailed); } // Wait 3 seconds, destroy the GUI then fully exit the environment Thread.Sleep(3000); this.ServerGui.Dispose(); Environment.Exit(Environment.ExitCode); }