Beispiel #1
0
        /// <summary>
        /// Starts the server, including a client connection loop and the game loop.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            settings = SpaceWarsSettings.ReadXML("settings.xml");
            clients  = new LinkedList <SocketState>();
            watch    = new Stopwatch();
            gameData = new StringBuilder();

            theWorld = new World(settings.GetSize(), settings.GetStars(), settings.GetShotDelay(), settings.GetRespawnDelay(), settings.TeamModeEnabled());

            Networking.ServerAwaitingClientLoop(HandleNewClient);

            watch.Start();

            gameLoop = new Thread(() =>
            {
                while (true)
                {
                    Update();
                }
            });

            gameLoop.Start();
        }
Beispiel #2
0
        /// <summary>
        /// Gets initial data from the client, more specifically the player name. Sends initial game info to the client.
        /// </summary>
        /// <param name="state"></param>
        private static void ReceivePlayerName(SocketState state)
        {
            try
            {
                // This block splits up the server data within the StringBuilder, and places it into initialInfo.
                StringBuilder sb          = state.sb;
                char[]        separator   = new char[] { '\n' };
                string[]      initialInfo = sb.ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries);

                // Assigns the ID sent by the server to the socket state for uniqie identification of connections.
                state.sb.Remove(0, initialInfo[0].Length + 1);
                string name = initialInfo[0];

                Ship newShip;
                lock (theWorld)
                {
                    newShip = theWorld.AddShipRandomPosition(name, settings.GetShotDelay(), settings.GetThrustStrength(), settings.GetTurningRate(), settings.GetStartingHP(), settings.GetShipHitBoxSize());
                }

                state.ClientCallback = HandleClientRequest;
                state.ID             = newShip.GetID();

                Networking.SendSocket(state.theSocket, "" + newShip.GetID() + "\n" + theWorld.GetSize() + "\n");

                lock (clients)
                {
                    clients.AddLast(state);
                }

                Networking.GetData(state);
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION: " + e.Message);
            }
        }