Exemple #1
0
        static void Main(string[] args)
        {
#if DEBUG
            config.logLevel = LogType.Debug;
#endif
            startTime = Utils.getTimestamp();
            changeState(ServerState.Starting, "StarryboundServer::Main");
            Console.Title = "Loading... Starrybound Server (" + VersionNum + ") (" + ProtocolVersion + ")";

            try
            {
                int     processId = Convert.ToInt32(File.ReadAllText("starbound_server.pid"));
                Process proc      = Process.GetProcessById(processId);
                proc.Kill();
                File.Delete("starbound_server.pid");
            }
            catch (Exception) { }

            monitorThread = new Thread(new ThreadStart(crashMonitor));
            monitorThread.Start();

            if (IsMono)
            {
                Environment.CurrentDirectory = Path.GetDirectoryName(typeof(StarryboundServer).Assembly.Location);
            }

            AppDomain.CurrentDomain.ProcessExit        += new EventHandler(ProcessExit);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);

            if (!IsMono)
            {
                NativeMethods.SetConsoleCtrlHandler(new NativeMethods.HandlerRoutine(ConsoleCtrlCheck), true);
            }

            BootstrapConfig.SetupConfig();

            writeLog("", LogType.FileOnly);
            writeLog("-- Log Start: " + DateTime.Now + " --", LogType.FileOnly);

            logInfo("##############################################");
            logInfo("####   Avilance Ltd. Starrybound Server   ####");
            logInfo("####   Copyright (c) Avilance Ltd. 2013   ####");
            logInfo("####       Licensed under the GPLv3       ####");
            logInfo("##############################################");
            logInfo("Version: " + VersionNum + " (" + ProtocolVersion + ")");
            logInfo("Loading Starrybound Server...");

            Config.SetupConfig();
            ServerConfig.SetupConfig();
            Groups.SetupGroups();
            Users.SetupUsers();
#if !DEBUG
            if (config.logLevel == LogType.Debug)
            {
                logWarn("The logLevel in your config is currently set to DEBUG. This **WILL** flood your console and log file, if you do not want this please edit your config logLevel to INFO");
                logWarn("Launch will proceed in 5 seconds.");
                System.Threading.Thread.Sleep(5000);
            }
#endif
#if !NOSERVER
            if (config.proxyPort == config.serverPort)
            {
                logFatal("You cannot have the serverPort and proxyPort on the same port!");
                Thread.Sleep(5000);
                Environment.Exit(3);
            }
#endif
            var geoippath = Path.Combine(SavePath, "GeoIP.dat");
            if (config.enableGeoIP && File.Exists(geoippath))
            {
                Geo = new GeoIPCountry(geoippath);
            }

            foreach (string sector in config.sectors)
            {
                byte[] sectorBytes = Encoding.UTF8.GetBytes(sector);
                byte[] buffer      = new byte[sectorBytes.Length + 1];
                buffer[0] = (byte)sectorBytes.Length;
                Buffer.BlockCopy(sectorBytes, 0, buffer, 1, sectorBytes.Length);
                sectors.Add(sectorBytes);
            }
            Bans.ProcessBans();

            logInfo("Starrybound Server initialization complete.");

            listener       = new ListenerThread();
            listenerThread = new Thread(new ThreadStart(listener.runTcp));
            listenerThread.Start();

            udpThread = new Thread(new ThreadStart(listener.runUdp));
            udpThread.Start();

            while (serverState != ServerState.ListenerReady)
            {
            }
            if ((int)serverState > 3)
            {
                return;
            }

            Console.Title = "Starting... Starrybound Server (" + VersionNum + ") (" + ProtocolVersion + ")";
#if !NOSERVER
            logInfo("Starting parent Starbound server - This may take a few moments...");
            sbServer       = new ServerThread();
            sbServerThread = new Thread(new ThreadStart(sbServer.run));
            sbServerThread.Start();
            while (serverState != ServerState.StarboundReady)
            {
            }
            if ((int)serverState > 3)
            {
                return;
            }
#endif
            logInfo("Parent Starbound server is ready. Starrybound Server now accepting connections.");
            changeState(ServerState.Running, "StarryboundServer::Main");
        }
        void parseOutput(string consoleLine)
        {
            try
            {
                foreach (string line in filterConsole)
                {
                    if (consoleLine.Contains(line))
                    {
                        return;
                    }
                }

                if (consoleLine.StartsWith("Error: ") || consoleLine.StartsWith("AssetException:"))
                {
                    this.parseError = true;
                }
                else if ((consoleLine.StartsWith("Warn:") || consoleLine.StartsWith("Info:") || consoleLine.StartsWith("Debug:")) && this.parseError)
                {
                    logStarboundError(" ");
                    this.parseError = false;
                }
                else if (String.IsNullOrWhiteSpace(consoleLine) && this.parseError)
                {
                    logStarboundError(" ");
                    this.parseError = false;
                    return;
                }

                if (consoleLine.StartsWith("Warn: Perf: "))
                {
                    string[] perf     = consoleLine.Remove(0, 12).Split(' ');
                    string   function = perf[0];
                    float    millis   = Convert.ToSingle(perf[2]);
                    if (millis > 5000)
                    {
                        StarryboundServer.logWarn("Parent Server [" + function + "] lagged for " + (millis / 1000) + " seconds");
                    }
                    return;
                }
                else if (consoleLine.Contains("Info: Server version"))
                {
                    string[] versionString   = consoleLine.Split('\'');
                    string   versionName     = versionString[1];
                    int      protocolVersion = int.Parse(versionString[3]);
                    StarryboundServer.starboundVersion.Protocol = protocolVersion;
                    StarryboundServer.starboundVersion.Name     = versionName;
                    if (protocolVersion != StarryboundServer.ProtocolVersion)
                    {
                        StarryboundServer.logFatal("Detected protcol version [" + protocolVersion + "] != [" + StarryboundServer.ProtocolVersion + "] to expected protocol version!");
                        Thread.Sleep(5000);
                        Environment.Exit(4);
                    }
                    StarryboundServer.logInfo("Parent Server Version: [" + versionName + "] Protocol: [" + protocolVersion + "]");
                    return;
                }
                else if (consoleLine.Contains("TcpServer will close, listener thread caught exception"))
                {
                    StarryboundServer.logFatal("Parent Server TcpServer listener thread caught exception, Forcing a restart.");
                    StarryboundServer.changeState(ServerState.Crashed, "ServerThread::parseOutput", "Starbound Tcp listener has crashed");
                }
                else if (consoleLine.Contains("TcpServer listening on: "))
                {
                    StarryboundServer.changeState(ServerState.StarboundReady, "ServerThread::parseOutput");
                    ServerConfig.RemovePrivateConfig();
                }
                else if (consoleLine.StartsWith("Info: Kicking client "))
                {
                    string[] kick = consoleLine.Remove(0, 21).Split(' ');
                    string   user = kick[0];
                    string   id   = kick[1];
                    string   ip   = kick[2];
                    StarryboundServer.logWarn("Parent Server disconnected " + user + " " + ip + " for inactivity.");
                    return;
                }

                if (!this.parseError)
                {
                    Console.WriteLine("[STAR] " + consoleLine);
                }
                else
                {
                    logStarboundError(consoleLine);
                }
            }
            catch (Exception) { }
        }