示例#1
0
        public void Run()
        {
            Console.WriteLine("Started S******d server...");
            try
            {
                listener.Start();
                new Thread(() =>
                {
                    while (running)
                    {
                        Thread.CurrentThread.IsBackground = true;
                        String command = Console.ReadLine();
                        lock (semaphore)
                        {
                            if (command.Equals("shutdown"))
                            {
                                Console.WriteLine("Shutting down server...");
                                Shutdown();
                            }
                            else if (command.Equals("removegame"))
                            {
                                Console.WriteLine("Removed game...");
                                state.Shutdown();
                                state = null;
                            }
                            else if (command.Equals("initgame"))
                            {
                                Console.WriteLine("Started game...");
                                state = new MultiplayerGameState();
                            }
                            else if (command.Equals("startgame"))
                            {
                                state.StartGame();
                            }
                            else if (command.Equals("restartgame"))
                            {
                                Console.WriteLine("Restarting game...");
                                state.Shutdown();
                                state = new MultiplayerGameState();
                            }
                            else if (command.StartsWith("addbot"))
                            {
                                state.AddBot(command.Split(' ')[1]);
                            }
                            else if (command.Equals("clear"))
                            {
                                Console.Clear();
                            }
                        }
                    }
                }).Start();

                running = true;
                while (running)
                {
                    if (listener.Pending())
                    {
                        HandleNewConnection();
                    }
                    lock (semaphore)
                    {
                        if (state != null)
                        {
                            state.Update();
                        }
                    }
                }
                listener.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not start server - Check if you already have a service listening on this port");
                Thread.Sleep(5000);
            }
        }
示例#2
0
 public void Shutdown()
 {
     running = false;
     state.Shutdown();
     state = null;
 }