コード例 #1
0
        public static void LoadServers()
        {
            LobbyServer = new LobbyServer();
            GameServers = new List <GameServer>();

            string[] directories = Directory.GetDirectories("GameServers");
            foreach (string directory in directories)
            {
                if (File.Exists(directory + "\\config.xml"))
                {
                    GameServer gs = new GameServer(directory);
                    gs.Index = GameServers.Count;
                    GameServers.Add(gs);
                    gs.LoginServer.Start();
                    gs.Start();
                }
            }

            string decPath = (StartupPath + "\\mServer_dec");
            string encPath = (StartupPath + "\\mServer_enc");

            using (Stream stream = File.Create(decPath))
            {
                using (BinaryWriter writer = new BinaryWriter(stream, Encoding.GetEncoding(949)))
                {
                    byte i = 0;
                    writer.Write((byte)GameServers.Count);
                    foreach (GameServer gs in GameServers)
                    {
                        byte[] addressBytes = gs.LoginServer.EndPoint.Address.GetAddressBytes();
                        Array.Reverse(addressBytes);
                        writer.Write(i++);
                        writer.Write(addressBytes);
                        writer.Write((byte)(gs.LoginServer.EndPoint.Port / 256));
                        writer.Write((byte)(gs.LoginServer.EndPoint.Port % 256));
                        writer.Write(Encoding.GetEncoding(949).GetBytes(string.Format("{0};{1}\0", gs.Name, gs.Description)));
                        writer.Write((byte)0x00);
                    }
                }
            }

            Checksum = ~CRC32.Calculate(File.ReadAllBytes(decPath));
            ZLIB.Compress(decPath, encPath);
            RawData = File.ReadAllBytes(encPath);

            File.Delete(decPath);
            File.Delete(encPath);

            Program.WriteLine("{0} game servers loaded!", GameServers.Count);
            LobbyServer.Start();
        }
コード例 #2
0
        private static void Main(string[] args)
        {
            XDocument doc = XDocument.Load("config.xml");

            HostName = doc.Element("config").Element("hostname").Value;
            Port     = (int)doc.Element("config").Element("port");
            //frameworkPath = doc.Element("config").Element("framework").Value;
            //connectorPath = doc.Element("config").Element("connector").Value;

            var mysqlserver   = doc.Element("config").Element("mysql").Attribute("server").Value;
            var mysqldatabase = doc.Element("config").Element("mysql").Attribute("database").Value;
            var mysqlusername = doc.Element("config").Element("mysql").Attribute("username").Value;
            var mysqlpassword = doc.Element("config").Element("mysql").Attribute("password").Value;

            mySqlConnectionString = string.Format("SERVER={0}; DATABASE={1}; UID={2}; PASSWORD={3}",
                                                  mysqlserver, mysqldatabase, mysqlusername, mysqlpassword);

            IPBanList = new HashSet <string>();
            foreach (var line in File.ReadAllLines("ipban.txt"))
            {
                var regex = new Regex("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
                if (regex.IsMatch(line))
                {
                    IPBanList.Add(line);
                }
            }

            IsLoaded = -1;

            try
            {
                ConnectToDatabase();

                Map.LoadCollisionData("shared\\sotp.dat");

                LoadServers();

                IsLoaded = 1;
            }
            catch (Exception e)
            {
                IsLoaded = 0;
                Program.WriteLine(e);
                Console.ReadKey(true);
            }

            if (IsLoaded == 1)
            {
                while (DateTime.UtcNow < scheduledReset.ResetTime)
                {
                    lock (SyncObj)
                    {
                        DateTime dt1 = DateTime.UtcNow;
                        LobbyServer.AcceptConnection();
                        foreach (var client in LobbyServer.Clients)
                        {
                            if (DateTime.UtcNow.Subtract(client.LastPacket).TotalMinutes > 1)
                            {
                                client.Connected = false;
                            }
                        }
                        foreach (GameServer gs in GameServers)
                        {
                            try
                            {
                                gs.UpdateGame();
                                gs.AcceptConnection();
                                gs.LoginServer.AcceptConnection();
                            }
                            catch (Exception e)
                            {
                                if (!Directory.Exists("CrashLogs"))
                                {
                                    Directory.CreateDirectory("CrashLogs");
                                }
                                string filename = ("CrashLogs\\crashlog-" + DateTime.Now.ToFileTime() + ".txt");
                                var    sw       = new StreamWriter(File.Create(filename));
                                sw.WriteLine(e.ToString());
                                sw.Close();
                                Program.WriteLine(e);
                                Console.ReadKey(true);
                                Process.Start(Process.GetCurrentProcess().ProcessName + ".exe");
                                Process.GetCurrentProcess().Kill();
                                return;
                            }

                            foreach (ScheduledReset.Warning sw in scheduledReset.Warnings.ToArray())
                            {
                                if (DateTime.UtcNow > sw.WarningTime)
                                {
                                    foreach (Client c in gs.Clients)
                                    {
                                        c.SendMessage(sw.Message);
                                        c.SendMessage(sw.Message);
                                        c.SendMessage(sw.Message);
                                    }
                                    scheduledReset.Warnings.Remove(sw);
                                }
                            }

                            if (scheduledReset.Cancelled)
                            {
                                foreach (Client c in gs.Clients)
                                {
                                    c.SendMessage("Server reset cancelled.");
                                    c.SendMessage("Server reset cancelled.");
                                    c.SendMessage("Server reset cancelled.");
                                }
                                scheduledReset.Cancelled = false;
                            }
                        }
                        if (DateTime.UtcNow.Subtract(lastDatabaseConnection).TotalHours > 1)
                        {
                            if (MySqlConnection != null)
                            {
                                MySqlConnection.Close();
                                ConnectToDatabase();
                            }
                        }
                        DateTime dt2 = DateTime.UtcNow;
                        if (dt2.Subtract(dt1).TotalSeconds > 0.150)
                        {
                            RunningSlowly = true;
                            //WriteLine("[{0}]: the server is running slowly!", DateTime.UtcNow);
                        }
                        else
                        {
                            RunningSlowly = false;
                        }
                    }

                    Thread.Sleep(50);

                    if (Console.KeyAvailable)
                    {
                        switch (Console.ReadKey(true).Key)
                        {
                        case ConsoleKey.Q: ScheduleReset(0); break;
                        }
                    }
                }

                ShutdownGameServers();

                if (MySqlConnection != null)
                {
                    MySqlConnection.Close();
                }

                if (ShouldReset)
                {
                    Process.Start(Process.GetCurrentProcess().ProcessName + ".exe");
                }

                Process.GetCurrentProcess().Kill();
            }
        }