Пример #1
0
        public virtual RegisteredSpawner CreateSpawner(IPeer peer, SpawnerOptions options)
        {
            var spawner = new RegisteredSpawner(GenerateSpawnerId(), peer, options);

            if (!(peer.GetProperty((int)PeerPropertyKeys.RegisteredSpawners) is Dictionary <int, RegisteredSpawner> peerSpawners))
            {
                // If this is the first time registering a spawners

                // Save the dictionary
                peerSpawners = new Dictionary <int, RegisteredSpawner>();
                peer.SetProperty((int)PeerPropertyKeys.RegisteredSpawners, peerSpawners);

                peer.Disconnected += OnRegisteredPeerDisconnect;
            }

            // Add a new spawner
            peerSpawners[spawner.SpawnerId] = spawner;

            // Add the spawner to a list of all spawners
            Spawners[spawner.SpawnerId] = spawner;

            // Invoke the event
            SpawnerRegistered?.Invoke(spawner);

            return(spawner);
        }
Пример #2
0
        /// <summary>
        /// Sends a request to master server, to register an existing spawner with given options
        /// </summary>
        public void RegisterSpawner(SpawnerOptions options, RegisterSpawnerCallback callback)
        {
            if (!Connection.IsConnected)
            {
                callback.Invoke(null, "Not connected");
                return;
            }

            Connection.SendMessage((short)OpCodes.RegisterSpawner, options, (status, response) =>
            {
                if (status != ResponseStatus.Success)
                {
                    callback.Invoke(null, response.AsString("Unknown Error"));
                    return;
                }

                var spawnerId = response.AsInt();

                var controller = new SpawnerController(this, spawnerId, Connection, options);

                // Save reference
                _locallyCreatedSpawners[spawnerId] = controller;

                callback.Invoke(controller, null);

                // Invoke the event
                SpawnerRegistered?.Invoke(controller);
            });
        }
Пример #3
0
        public RegisteredSpawner(int spawnerId, IPeer peer, SpawnerOptions options)
        {
            SpawnerId = spawnerId;
            Peer      = peer;
            Options   = options;

            _queue        = new Queue <SpawnTask>();
            _beingSpawned = new HashSet <SpawnTask>();
        }
        public RegisteredSpawner(int id, IClient client, SpawnerOptions options)
        {
            ID      = id;
            Client  = client;
            Options = options;

            _queue        = new Queue <SpawnTask>();
            _beingSpawned = new HashSet <SpawnTask>();
        }
        private RegisteredSpawner CreateSpawner(IClient client, SpawnerOptions options)
        {
            var spawner = new RegisteredSpawner(GenerateSpawnerId(), client, options);

            // Add the spawner to a list of all spawners
            _registeredSpawners.Add(spawner);

            return(spawner);
        }
Пример #6
0
        public SpawnerController(SpawnerPlugin owner, int spawnerId, IClientSocket connection, SpawnerOptions options)
        {
            _spawners = owner;

            Connection = connection;
            SpawnerId  = spawnerId;
            Options    = options;

            DefaultSpawnerSettings = new DefaultSpawnerConfig()
            {
                MasterIp         = connection.ConnectionIp,
                MasterPort       = connection.ConnectionPort,
                MachineIp        = options.MachineIp,
                SpawnInBatchmode = CommandLineArgs.IsProvided("-batchmode")
            };

            // Add handlers
            connection.SetHandler((short)OpCodes.SpawnRequest, HandleSpawnRequest);
            connection.SetHandler((short)OpCodes.KillSpawnedProcess, HandleKillSpawnedProcessRequest);
        }
Пример #7
0
    public virtual void StartSpawner()
    {
        // In case we went from one scene to another, but we've already started the spawner
        if (IsSpawnerStarted)
        {
            return;
        }

        if (DontDestroyIfStarted)
        {
            // Move to hierarchy root, so that we can't destroy it
            if (transform.parent != null)
            {
                transform.SetParent(null, false);
            }

            // Make sure this object is not destroyed
            DontDestroyOnLoad(gameObject);
        }

        IsSpawnerStarted = true;

        var spawnerOptions = new SpawnerOptions();

        spawnerOptions.MaxProcesses = MaxSpawnedProcesses;
        if (Msf.Args.IsProvided(Msf.Args.Names.MaxProcesses))
        {
            spawnerOptions.MaxProcesses = Msf.Args.MaxProcesses;
        }

        // If we're running in editor, and we want to override the executable path
        if (Msf.Runtime.IsEditor && OverrideExePathInEditor)
        {
            DefaultExePath = ExePathFromEditor;
        }

        Logger.Info("Registering as a spawner with options: \n" + spawnerOptions);

        // 1. Register the spawner
        Msf.Server.Spawners.RegisterSpawner(spawnerOptions, (spawner, error) =>
        {
            if (error != null)
            {
                Logger.Error("Failed to create spawner: " + error);
                return;
            }

            SpawnerController = spawner;

            spawner.DefaultSpawnerSettings.AddWebGlFlag = Msf.Args.IsProvided(Msf.Args.Names.WebGl)
                ? Msf.Args.WebGl
                : SpawnWebglServers;

            // Set to run in batchmode
            spawner.DefaultSpawnerSettings.RunInBatchmode = Msf.Args.IsProvided(Msf.Args.Names.SpawnInBatchmode)
                ? Msf.Args.SpawnInBatchmode
                : DefaultSpawnInBatchmode;

            // 2. Set the default executable path
            spawner.DefaultSpawnerSettings.ExecutablePath = Msf.Args.IsProvided(Msf.Args.Names.ExecutablePath) ?
                                                            Msf.Args.ExecutablePath : DefaultExePath;

            // 3. Set the machine IP
            spawner.DefaultSpawnerSettings.MachineIp = Msf.Args.IsProvided(Msf.Args.Names.MachineIp) ?
                                                       Msf.Args.MachineIp : DefaultMachineIp;

            // 4. (Optional) Set the method which does the spawning, if you want to
            // fully control how processes are spawned
            spawner.SetSpawnRequestHandler(HandleSpawnRequest);

            // 5. (Optional) Set the method, which kills processes when kill request is received
            spawner.SetKillRequestHandler(HandleKillRequest);

            Logger.Info("Spawner successfully created. Id: " + spawner.SpawnerId);
        });
    }
Пример #8
0
    public void SpawnerApi()
    {
        var defaultOptions = new SpawnerOptions();

        // Registers a spawner to master server, so that master server knows about it's existance.
        // Your spawner will receive requests to spawn processes (or something else,
        // if necessary)
        Msf.Server.Spawners.RegisterSpawner(defaultOptions, (spawner, error) =>
        {
            if (spawner == null)
            {
                Logs.Error(error);
            }
        });

        var spawnerOptions = new SpawnerOptions()
        {
            MaxProcesses = 0,           // Unlimited,
            MachineIp    = "127.0.0.1", // IP address of this machine, will be passed to game servers too
            Region       = "US",        // Region identifier, can be anything
            Properties   = new Dictionary <string, string>()
            {
                // If you need spawner to have some extra properties
                { "ExtraProperty", "Whatever" }
            }
        };

        // Example of a more customized approach
        Msf.Server.Spawners.RegisterSpawner(spawnerOptions, (spawner, error) =>
        {
            if (spawner == null)
            {
                Logs.Error(error);
                return;
            }

            // Set the build path (default ''(empty string))
            spawner.DefaultSpawnerSettings.ExecutablePath = "C:/Win/Build.exe";

            // Change whether or not the spawner process should run in batchmode
            spawner.DefaultSpawnerSettings.RunInBatchmode = false;

            // (Optional) If you want to handle spawn requests manually
            spawner.SetSpawnRequestHandler((packet, message) =>
            {
                // We've got a request to spawn a new process
                // packet - contains spawn info
                // message - the original message of the request. You'll need to respond to it
                // with ResponseStatus.Success, if process started successfully
                var hasError = false;
                if (hasError)
                {
                    // Example on how to handle errors
                    message.Respond("A mysterious error", ResponseStatus.Failed);
                    return;
                }

                // TODO Start a process or a virtual game server

                // Respond with success
                message.Respond(ResponseStatus.Success);
            });

            // (Optional) If you want to handle kill requests manually
            spawner.SetKillRequestHandler(spawnId =>
            {
                // This is a request to kill a spawned process
                // TODO Find a process by spawnId and kill it
            });
        });
    }