Example #1
0
        public static void Main(string[] args)
        {
            // If we set the exceptionhandler also in debug, VS wont throw them and i cant react in Debug-mode
            // They will be printed to the console interface
#if !DEBUG
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#endif
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
#if !DEBUG
            try {
#endif

            // Cleanup before loading any other data
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);

            // Save some infos about our thread and assembly
            mThread   = Thread.CurrentThread;
            mProcess  = Process.GetCurrentProcess();
            mAssembly = Assembly.GetEntryAssembly();
            if (mThread != null)
            {
                // We set a name on our core thread
                mThread.Name = "Core Thread";
            }

            // Initialize our timer manager
            TimerThread ttObj = new TimerThread();
            mTimerThread      = new Thread(new ThreadStart(ttObj.TimerMain));
            mTimerThread.Name = "Timer Thread";

            // Prepare console for a large output
            int width = Math.Min(100, Console.LargestWindowWidth - 2);
            Console.CursorVisible = false;
            Console.Clear();
            Console.WindowLeft = Console.WindowTop = 0;
            if (Console.WindowWidth < width)
            {
                Console.WindowWidth = width;
            }

            // Real fullscreen mode *_*
#if REAL_FULLSCREEN
            IntPtr hConsole = GetStdHandle(-11);
            SetConsoleDisplayMode(hConsole, 0);
#endif

            // Set colors for the logo printer
            LogoPrinter.PrefixColor    = EConsoleColor.Blue;
            LogoPrinter.SufixColor     = EConsoleColor.Blue;
            LogoPrinter.TextColor      = EConsoleColor.Gray;
            LogoPrinter.CopyrightColor = EConsoleColor.Status;
            LogoPrinter.PrintLogo();

            // Output some infos about version and that
            Version ver = mAssembly.GetName().Version;
            ServerConsole.StatusLine("Rovolution Server - Version {0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision);

            // Set error and exception handler (dll import)
            mConsoleEventHandler = new ConsoleEventHandler(OnConsoleEvent);
            SetConsoleCtrlHandler(mConsoleEventHandler, true);

            // Read server config
            mAppConf = new ApplicationSettings();
            mAppConf.ReadAll();

            // Mysql init
            Stopwatch watch = Stopwatch.StartNew();
            ServerConsole.Info("Connecting to SQL Server {0}...", mAppConf.Connection["DB Server"]);
            mDatabase = new RovolutionDatabase(Conf.Connection["DB Server"], int.Parse(Conf.Connection["DB Port"]), Conf.Connection["DB User"], Conf.Connection["DB Password"], Conf.Connection["DB Database"]);
            GodLesZ.Library.MySql.EMysqlConnectionError conRes = mDatabase.Prepare();
            if (conRes != GodLesZ.Library.MySql.EMysqlConnectionError.None)
            {
                ServerConsole.WriteLine(EConsoleColor.Error, " failed!");
                throw new Exception("Failed to open Database Connection! Type: " + conRes.ToString() + Environment.NewLine + (mDatabase.LastError != null ? ", Message: " + mDatabase.LastError.Message : ""));
            }
            watch.Stop();
            ServerConsole.WriteLine(EConsoleColor.Status, " done! Needed {0:F2} sec", watch.Elapsed.TotalSeconds);
            watch = null;

            // Load scripts (including events & that)
            ScriptDatabase.Initialize(@"Scripts\ScriptList.xml");

            ScriptCompiler.Compile(AppDomain.CurrentDomain.BaseDirectory + Path.Combine(Settings.Default.MainConfDir, Settings.Default.ScriptAssemblies), true, true);
            // Load assemblies for debug
            // TODO: we should load the assemblies for debugging
            //		 so VS could hijack them and we could debug them at runtime
            //		 also need the *.pdb files for this..

            // Packets handler
            PacketLoader.Initialize();

            // Initialize World events
            ScriptCompiler.Initialize("Rovolution.Server.Scripts");

            // Now we are able load our ressources
            World.Load();

            // Content init done, load Socket pool
            SocketPool.Create();
            mSocketConnector = new SocketConnector(mAppConf.Connection["Server IP"], mAppConf.Connection.GetInt("Server Port"));
            PacketHandlers.Initialize();

            // Start Timer Thread
            mTimerThread.Start();

            // Start timer for checking connections
            NetState.Initialize();


            // Initialize & load finished
            // Clean
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);


            // Trigger ServerStarted event
            Events.InvokeServerStarted();


            DateTime now, last = DateTime.Now;
            const int sampleInterval   = 100;
            const float ticksPerSecond = (float)(TimeSpan.TicksPerSecond * sampleInterval);
            int sample = 0;

            // The server loop
            // - looks for new sockets and process all packets
            while (mSignal.WaitOne())
            {
                // Refresh timer
                Timer.Slice();

                // Kick out old connections
                NetState.FlushAll();
                NetState.ProcessDisposedQueue();
                // Catch new connections
                mSocketConnector.Slice();

                if (Slice != null)
                {
                    Slice();
                }

                // just for Diagnostics
                if ((++sample % sampleInterval) == 0)
                {
                    now = DateTime.Now;
                    mCyclesPerSecond[mCycleIndex++ % mCyclesPerSecond.Length] = ticksPerSecond / (now.Ticks - last.Ticks);
                    last = now;
                }
            }
#if !DEBUG
        }

        catch (Exception e) {
            CurrentDomain_UnhandledException(null, new UnhandledExceptionEventArgs(e, true));
        }
#endif
        }