コード例 #1
0
        /// <summary>
        /// Takes a socket state object, sets its associated ship to dead then removes the ship from
        /// the list of clients
        /// </summary>
        private void HandleNetworkError(SocketState state)
        {
            int clientID = state.GetID();

            // dispose all resources used by socket
            // state.GetSocket().Dispose();

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

            // the ship that needs to be cleaned up
            // needed to get stats
            Ship deadShip;

            lock (world)
            {
                deadShip = world.AddShipToCleanup(clientID);
            }

            lock (stats)
            {
                // add stats to set of all stats when cleaning up clients
                AddShipToStats(deadShip);
            }

            Console.Out.WriteLine("Player " + clientID + " has left the game");
        }
コード例 #2
0
        /// <summary>
        /// Network action for processing client direction commands.
        /// </summary>
        private void HandleClientCommands(SocketState state)
        {
            if (state.HasError)
            {
                HandleNetworkError(state);
                return;
            }

            // enumerate the complete received messages.
            IEnumerable <string> messages = GetTokens(state.GetStringBuilder());

            lock (world)
            {
                // process all of the commands now,
                //may have recieved more than one before refreshing screen
                foreach (string command in messages)
                {
                    world.UpdateShipCommands(command, state.GetID());
                    state.GetStringBuilder().Remove(0, command.Length);
                }
            }

            // ask client for data (continue loop)
            Network.GetData(state);
        }
コード例 #3
0
        public void SocketState_GetAndSetID()
        {
            // 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);

                // ID should be same as what it was set when object was created
                Assert.AreEqual(-1, state.GetID());

                // check that setting id changes what is stored
                state.SetID(5);
                Assert.AreEqual(5, state.GetID());
            }
            // make sure we close the socket when we are done testing our state
            finally
            {
                sock.Dispose();
            }
        }
コード例 #4
0
        public void SocketState_ConstructorTest()
        {
            // 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);

                //the state should not be reporting an error
                Assert.IsFalse(state.HasError);

                // there error messgae should be an empty string
                Assert.IsTrue(state.ErrorMessage == "");

                //the size of the buffer should be 1024
                byte[] buffer = state.GetMessageBuffer();

                Assert.AreEqual(1024, buffer.Length);

                // the message buffer should be filled with 0's
                byte zeroByte = 0x0;
                for (int i = 0; i < buffer.Length; i++)
                {
                    Assert.IsTrue(buffer[i] == zeroByte);
                }

                // growable string builder which represents the message sent by server
                // should not contain anything.
                StringBuilder sb = state.GetStringBuilder();
                Assert.IsTrue(sb.ToString() == "");
                Assert.AreEqual(0, sb.Length);

                // the socket returned by GetSocket should be the same instance as
                // passed to the constructor
                Assert.AreEqual(sock, state.GetSocket());

                // 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);

                // ID should be same as what it was set
                Assert.AreEqual(-1, state.GetID());
            }
            // make sure we close the socket when we are done testing our state
            finally
            {
                sock.Dispose();
            }
        }