/// <summary>
        /// Takes a SocketState object, state, and initiates a game with the
        /// server associated with state by sending the name given by the user.
        /// </summary>
        private void FirstContact(SocketState state)
        {
            if (state.HasError)
            {
                // report the error to the user
                // show error message
                string errorMessage = "An error occured trying to contact the server";
                errorMessage += "\nPlease try to connect again";
                ReportNetworkError(errorMessage);
                // get out of the call loop
                return;
            }

            string name = window.GetUserName();

            name += "\n";

            // begin "handshake" by sending name
            Network.Send(state.GetSocket(), name);

            // Change the action that is taken when a network event occurs. Now when data is received,
            // the Networking library will invoke ReceiveStartup
            state.SetNetworkAction(ReceiveStartup);

            Network.GetData(state);
        }
Esempio n. 2
0
        /// <summary>
        /// This is the delegate for when a new socket is accepted
        /// The networking library will invoke this method when a browser connects
        /// </summary>
        /// <param name="state"></param>
        public static void HandleHttpConnection(SocketState state)
        {
            // Before receiving data from the browser, we need to change what we do when network activity occurs.
            state.SetNetworkAction(ServeHttpRequest);

            Network.GetData(state);
        }
        /// <summary>
        /// NetworkAction delegate which handles new client requests
        /// </summary>
        private void HandleNewClient(SocketState state)
        {
            // change the callback for the socketstate to a method which
            // receives the player's name
            state.SetNetworkAction(ReceivePlayerName);

            // ask for more data
            Network.GetData(state);
        }
        /// <summary>
        /// Takes a SocketState object, state, extracts the player ID and world
        /// size, updates the network action and waits for more data.
        /// </summary>
        private void ReceiveStartup(SocketState state)
        {
            if (state.HasError)
            {
                // report the error to the user
                // show error message
                string errorMessage = "An error occured trying to contact the server";
                errorMessage += "\nPlease try to connect again";
                ReportNetworkError(errorMessage);
                // get out of the call loop
                return;
            }

            // get the player ID and world size out of state.sb
            IEnumerable <string> tokens         = GetTokens(state.GetStringBuilder());
            List <string>        IDAndWorldSize = new List <string>();

            foreach (string token in tokens)
            {
                IDAndWorldSize.Add(token);

                // we only want two tokens
                if (IDAndWorldSize.Count >= 2)
                {
                    break;
                }
            }

            // see if we actually got out two tokens, should be world size and
            // player id, if we did, process them and change network action
            if (IDAndWorldSize.Count == 2)
            {
                // remove tokens from StringBuilder should be size of tokens
                int sizeOfTokensAndNewLines = IDAndWorldSize[0].Length + IDAndWorldSize[1].Length;
                state.GetStringBuilder().Remove(0, sizeOfTokensAndNewLines);

                // Update the action to take when network events happen
                state.SetNetworkAction(ProcessMessage);

                // create new world
                theWorld = new World();
                window.SetWorld(theWorld);


                // parse the id and worldsize and set them in our client
                GetWorldSizeAndID(IDAndWorldSize, out int ID, out int worldSize);
                //SetPlayerID(ID);
                SetWorldSize(worldSize);

                // now that we have a connection, we can start sending controls
                StartSendingControls(state.GetSocket());
            }

            // Start waiting for data
            Network.GetData(state);
        }
Esempio n. 5
0
        public void SocketState_SetNetworkActionNull()
        {
            // create a socket using to pass to a SocketState object
            Network.MakeSocket("localhost", out Socket sock, out IPAddress address);

            try
            {
                SocketState state = new SocketState(sock, -1);

                state.SetNetworkAction(null);

                // We have not set a network action, but it should still not be null
                // trying to invoke this action would be bad if it was null and cause
                // test to fail
                state.InvokeNetworkAction(state);
            }
            // make sure we close the socket when we are done testing our state
            finally
            {
                sock.Dispose();
            }
        }
        /// <summary>
        /// NetworkAction delegate which implements the server's part of the
        /// initial handshake.
        ///
        /// Creates a new Ship with the given name and a new unique ID and sets
        /// the SocketState's ID.
        ///
        /// Changes the state's callback method to one that handles command
        /// requests from the client then asks the clients for data.
        /// </summary>
        private void ReceivePlayerName(SocketState state)
        {
            // enumerate the complete received messages.
            IEnumerable <string> messages = GetTokens(state.GetStringBuilder());
            // find the name in the complete messages
            string playerName = "";

            if (messages.Any())
            {
                playerName = messages.First();
                state.GetStringBuilder().Clear();
            }
            // if there wasn't a complete message continue the event loop
            else
            {
                Network.GetData(state);
                return;
            }

            // the id and world are being changed by callback methods (different threads)
            int playerID = 0;

            lock (world)
            {
                playerID = IDCounter++;
                // make a ship with a the name and give it a unique id
                // add ship to world
                world.MakeNewShip(playerName.TrimEnd(), playerID);
                // set the socket state ID
                state.SetID(playerID);
            }

            if (state.HasError)
            {
                HandleNetworkError(state);
                return;
            }


            // change the callback to handle incoming commands from the client
            state.SetNetworkAction(HandleClientCommands);

            // send the startup info to the client (ID and world size)
            string startupInfo = playerID + "\n" + world.GetSize() + "\n";

            Network.Send(state.GetSocket(), startupInfo);

            // set FirstPlayerHasJoined to true if this is the first player and
            // get the start time
            if (!FirstPlayerHasJoined)
            {
                FirstPlayerHasJoined = true;
                // set the start time to now (current value will be server start time)
                startTime = DateTime.UtcNow;
            }

            // handshake is done print line that someone has connected
            Console.Out.WriteLine("A new Client has contacted the Server.");

            // add the client's socket to a set of all clients
            lock (clients)
            {
                clients.Add(state);
            }

            // ask the client for data (continue loop)
            Network.GetData(state);
        }