/// <summary>
        /// Callback for when a connection had disconnected
        /// </summary>
        protected void MasterClient_OnDisconnect(MasterClient client)
        {
            // Remove client, and call OnUpdate Event
            try
            {
                // Release this stream's AsyncEventArgs to the object pool
                base.Release(client.Stream);

                // Remove client from online list
                if (Clients.TryRemove(client.ConnectionID, out client) && !client.Disposed)
                {
                    client.Dispose();
                }
            }
            catch (Exception e)
            {
                Program.ErrorLog.Write("An Error occured at [MasterServer.OnDisconnect] : Generating Exception Log");
                ExceptionHandler.GenerateExceptionLog(e);
            }
        }
        /// <summary>
        /// Accepts a TcpClient, and begin the serverlist fetching process for the client.
        /// This method is executed when the user updates his server browser ingame
        /// </summary>
        protected override void ProcessAccept(GamespyTcpStream Stream)
        {
            // Get our connection id
            long         ConID = Interlocked.Increment(ref ConnectionCounter);
            MasterClient client;

            // End the operation and display the received data on
            // the console.
            try
            {
                // Convert the TcpClient to a MasterClient
                client = new MasterClient(Stream, ConID);
                Clients.TryAdd(client.ConnectionID, client);

                // Start receiving data
                Stream.BeginReceive();
            }
            catch (ObjectDisposedException) // Ignore
            {
                // Remove client
                Clients.TryRemove(ConID, out client);
            }
            catch (IOException) // Connection closed before a TcpClientStream could be made
            {
                // Remove client
                Clients.TryRemove(ConID, out client);
            }
            catch (Exception e)
            {
                // Remove client
                Clients.TryRemove(ConID, out client);

                // Report error
                Program.ErrorLog.Write("NOTICE: An Error occured at [MstrServer.AcceptClient] : Generating Exception Log");
                ExceptionHandler.GenerateExceptionLog(e);
            }
        }
 protected override void OnException(Exception e)
 {
     ExceptionHandler.GenerateExceptionLog(e);
 }
Esempio n. 4
0
        /// <summary>
        /// Program Entry Point
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // Create our version string
            string version = String.Concat(Version.Major, ".", Version.Minor, ".", Version.Build);

            // Setup the console
            Console.Title = $"Battlefield2 Statistics Master Gamespy Emulator v{version}";
            Console.WriteLine(@"__________         __    __  .__           _________             ");
            Console.WriteLine(@"\______   \_____ _/  |__/  |_|  |   ____  /   _____/_____ ___.__.");
            Console.WriteLine(@" |    |  _/\__  \\   __\   __\  | _/ __ \ \_____  \\____ <   |  |");
            Console.WriteLine(@" |    |   \ / __ \|  |  |  | |  |_\  ___/ /        \  |_> >___  |");
            Console.WriteLine(@" |______  /(____  /__|  |__| |____/\___  >_______  /   __// ____|");
            Console.WriteLine(@"        \/      \/                     \/        \/|__|   \/     ");
            Console.WriteLine();
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine("Battlefield 2 Master Gamespy Server Emulator v" + version);
            Console.WriteLine("Created for BF2Statistics.com by Wilson212");
            Console.WriteLine();

            // Start login servers
            try
            {
                // Make sure Logs dir is created
                if (!Directory.Exists(Path.Combine(RootPath, "Logs")))
                {
                    Directory.CreateDirectory(Path.Combine(RootPath, "Logs"));
                }

                // Wrap error log into a writer
                ErrorLog = new LogWriter(Path.Combine(RootPath, "Logs", "MasterServer_Error.log"), false);

                // Setup Exception Handle
                AppDomain.CurrentDomain.UnhandledException += ExceptionHandler.OnUnhandledException;
                Application.ThreadException += ExceptionHandler.OnThreadException;

                // Start the Gamespy Servers
                ServerManager.StartServers();
                Console.WriteLine();
                Console.Write("Cmd > ");
            }
            catch (Exception e)
            {
                // Display error
                Console.WriteLine(e.Message);
                Console.WriteLine(" *** An exception will be logged *** ");
                Console.WriteLine();
                Console.Write("Press any key to close...");

                // Create exception log and wait for key input
                ExceptionHandler.GenerateExceptionLog(e);
                Console.ReadLine();
                return;
            }

            // Main program loop
            while (IsRunning)
            {
                CheckInput();
            }

            // Shut login servers down
            Console.WriteLine("Shutting down local Gamespy sockets...");
            ServerManager.Shutdown();
        }