Example #1
0
        /// <summary>
        /// Sends the start up information of the world to the client.
        /// </summary>
        private static void SendStartUp(NetworkController.SocketState state)
        {
            state.callBack = ProcessMessage;

            //Sends the world to the current socket
            NetworkController.NetworkHandler.Send(state, state.getID() + "\n" + world.width + "\n" + world.height + "\n");



            Console.WriteLine("Sending startup info...");
        }
Example #2
0
        /*: it is important that the server sends the startup info before adding the client to the list of all clients.
         *  This guarantees that the startup info is sent before any world info. Remember that the server is running a timer that may send world info to the list of clients at any time.
         */


        /// <summary>
        ///
        /// </summary>
        /// <param name="state"></param>
        private static void ReceiveName(NetworkController.SocketState state)
        {
            //
            Socket getData = state.theSocket;



            state.callBack = (SendStartUp);


            Console.WriteLine(state.getID());

            //This will only work for testing the FIRST client. We will have to change to something that determines the name among other possible messages.....
            string name = state.sb.ToString();

            //create the new player and add him to the world.
            Snake player;

            //Locking this up to try and fix race cond
            lock (world)
            {
                player = world.AddNewSnake(name);
            }
            state.setID(player.getID());

            SendStartUp(state);

            // Console.WriteLine(name);
            //Also locking to fix bug with two clients trying to connect.
            lock (connectedClients)
            {
                connectedClients.AddLast(state);
            }
            // Console.WriteLine(state.getID());
            //Again, will have to change this to send player specific ID.



            NetworkController.NetworkHandler.AwaitDataFromServer(state);
        }
Example #3
0
        /// <summary>
        /// Used to read client input and react accordingly.
        /// </summary>
        private static void ProcessMessage(NetworkController.SocketState state)
        {
            //Save current ID to determine if we continue reading directions(If this ID snake is dead, do not read messages)
            int ID = state.getID();

            //Saves the input to our message
            string message = state.sb.ToString();

            //Used to seperate by newline
            string[] remove = { "\n" };


            //Used to save direction from our string
            char direction;

            //Used to parse into int for passing direction to our world
            int temp;

            //Split each command to move
            string[] theMsg = message.Split(remove, StringSplitOptions.RemoveEmptyEntries);



            try
            {
                foreach (string dirC in theMsg)
                {
                    //Assure is a valid and complete direction request
                    if (dirC[0] == '(' && dirC[dirC.Length - 1] == ')')
                    {
                        direction = dirC[1];


                        lock (world)
                        {
                            //   Console.WriteLine(direction);

                            if (world.getAllSnakes().ContainsKey(ID))
                            {
                                int.TryParse(direction.ToString(), out temp);

                                //  Console.WriteLine(temp);
                                //Move the snake in the given direction.

                                world.getAllSnakes()[ID].setDir(temp);
                                //    Console.WriteLine(world.getAllSnakes()[ID].getDir());
                            }
                            else
                            {
                            }
                        }
                    }
                    else if (dirC[0] == 'E' && dirC[1] == 'N' && dirC[2] == 'D')
                    {
                        state.sb.Clear();
                        state.callBack = null;

                        Console.WriteLine("STATE ID:" + state.getID());
                        Console.WriteLine("CONNECTED CLIENT ID:" + connectedClients.First.Value.getID());

                        NetworkController.SocketState tempdel = null;
                        foreach (NetworkController.SocketState el in connectedClients)
                        {
                            if (el.getID() == state.getID())
                            {
                                tempdel = el;
                            }
                        }

                        connectedClients.Remove(tempdel);
                        Console.WriteLine("SIZE:" + connectedClients.Count);
                    }
                    //Remove processed message from buffer.
                    state.sb.Remove(0, dirC.Length + 1);
                }


                if (state.getID() == -1)
                {
                }
                else
                {
                    NetworkController.NetworkHandler.AwaitDataFromServer(state);
                }
            }
            catch (Exception)
            {
            }
        }