예제 #1
0
        // The thread that runs the command
        private void ThreadRecieveCommand(NetworkingManager.OnMessageRecievedEventArguments eventArguments)
        {
            #region Convert the JSON back to the server command
            // Convert the payload into an actual server command
            ServerCommand recievedServerCommand = JsonConvert.DeserializeObject <ServerCommand>(Encoding.Default.GetString(eventArguments.payload));
            // Convert the server command into the actual command that it is, rather than the base ServerCommand class, so we know which Run function to use
            dynamic convertedRecievedServerCommand = Activator.CreateInstance(recievedServerCommand.type);
            #endregion

            #region Set the variables on the new server command
            // Set the name of the sender
            convertedRecievedServerCommand.nameOfSender = recievedServerCommand.nameOfSender;
            // Set the type
            convertedRecievedServerCommand.type = recievedServerCommand.type;
            // Set the arguments
            convertedRecievedServerCommand.arguments = recievedServerCommand.arguments;
            // Set it's attached application to give it access to the program classes and databases
            convertedRecievedServerCommand.attachedApplication = attachedApplication;

            #endregion

            #region Run the server command
            // So, if the world has the player on it, this is a command being run by a player
            if (world.players.ContainsKey(recievedServerCommand.nameOfSender))
            {
                // Set the sender gameObject on the server command
                convertedRecievedServerCommand.sender = world.players[recievedServerCommand.nameOfSender].controlledGameObject;

                // Run the command once it is ready in the queue

                // Put this server command on the queue of the sender trying to run the command
                convertedRecievedServerCommand.sender.commandQueue.Enqueue(convertedRecievedServerCommand);
                // Go into an infinite loop, waiting for the command either to be removed from the queue, or the command to come to the top to be executed
                while (true)
                {
                    // If the command is the next to run in the queue, remove it from the queue and return true, letting the thread move on
                    if (convertedRecievedServerCommand.sender.commandQueue.Peek().type == convertedRecievedServerCommand.type)
                    {
                        if (convertedRecievedServerCommand.sender.commandQueue.Peek() == convertedRecievedServerCommand)
                        {
                            // Run the server command
                            convertedRecievedServerCommand.Run(convertedRecievedServerCommand.arguments, this);
                            // Take it off the queue of commands, since it just got run
                            convertedRecievedServerCommand.sender.commandQueue.Dequeue();
                            // Stop the loop
                            break;
                        }
                    }
                    // Otherwise, if the server command gets removed from the queue at any point, stop the loop
                    else if (!convertedRecievedServerCommand.sender.commandQueue.Contains(convertedRecievedServerCommand))
                    {
                        break;
                    }
                }
            }
            // Otherwise, this is probably someone pinging the server or just trying to connect
            else
            {
                // Run the server command
                convertedRecievedServerCommand.Run(convertedRecievedServerCommand.arguments, this);
            }
            #endregion
        }
예제 #2
0
 // Sends a command to all clients
 public void SendServerCommand(ServerCommand serverCommandToSend)
 {
     // Publish the server command as a JSON string on the send topic
     networkingManager.Publish(sendCommandsTopic, JsonConvert.SerializeObject(serverCommandToSend));
 }