Exemple #1
0
        /// <summary>
        /// Sends a request to master server, to register an existing spawner with given options
        /// </summary>
        public void RegisterSpawner(SpawnerOptions options, RegisterSpawnerCallback callback, IClientSocket connection)
        {
            if (!connection.IsConnected)
            {
                callback.Invoke(null, "Not connected");
                return;
            }

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

                var spawnerId = response.AsString();

                var controller = new SpawnerController(spawnerId, connection, options);

                // Save reference
                _locallyCreatedSpawners[spawnerId] = controller;

                callback.Invoke(controller, null);

                // Invoke the event
                if (SpawnerRegistered != null)
                {
                    SpawnerRegistered.Invoke(controller);
                }
            });
        }
        public virtual RegisteredSpawner CreateSpawner(IPeer peer, SpawnerOptions options)
        {
            var spawner = new RegisteredSpawner(GenerateSpawnerId(), peer, options);

            var peerSpawners =
                peer.GetProperty((int)MsfPropCodes.RegisteredSpawners) as Dictionary <string, RegisteredSpawner>;

            if (peerSpawners == null)
            {
                // If this is the first time registering a spawners

                // Save the dictionary
                peerSpawners = new Dictionary <string, RegisteredSpawner>();
                peer.SetProperty((int)MsfPropCodes.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
            if (SpawnerRegistered != null)
            {
                SpawnerRegistered.Invoke(spawner);
            }

            return(spawner);
        }
Exemple #3
0
        public RegisteredSpawner(string spawnerId, IPeer peer, SpawnerOptions options)
        {
            SpawnerId = spawnerId;
            Peer      = peer;
            Options   = options;

            _queue        = new Queue <SpawnTask>();
            _beingSpawned = new HashSet <SpawnTask>();
        }
Exemple #4
0
        public SpawnerController(int spawnerId, IClientSocket connection, SpawnerOptions options)
        {
            Connection = connection;
            SpawnerId  = spawnerId;
            Options    = options;

            DefaultSpawnerSettings = new DefaultSpawnerConfig()
            {
                MasterIp   = connection.ConnectionIp,
                MasterPort = connection.ConnectionPort,
                MachineIp  = options.MachineIp
            };

            // Add handlers
            connection.SetHandler((short)MsfOpCodes.SpawnRequest, HandleSpawnRequest);
            connection.SetHandler((short)MsfOpCodes.KillSpawnedProcess, HandleKillSpawnedProcessRequest);
        }
        public SpawnerController(int spawnerId, IClientSocket connection, SpawnerOptions options)
        {
            Connection = connection;
            SpawnerId  = spawnerId;
            Options    = options;

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

            // Add handlers
            connection.SetHandler((short)MsfMessageCodes.SpawnProcessRequest, SpawnProcessRequestHandler);
            connection.SetHandler((short)MsfMessageCodes.KillProcessRequest, KillProcessRequestHandler);
        }
        /// <summary>
        /// Create new instance of spawner controller
        /// </summary>
        /// <param name="spawnerId"></param>
        /// <param name="connection"></param>
        /// <param name="options"></param>
        public SpawnerController(int spawnerId, IClientSocket connection, SpawnerOptions spawnerOptions)
        {
            Logger = Msf.Create.Logger(typeof(SpawnerController).Name, LogLevel.Info);

            Connection = connection;
            SpawnerId  = spawnerId;

            SpawnSettings = new SpawnerConfig()
            {
                MasterIp         = connection.ConnectionIp,
                MasterPort       = connection.ConnectionPort,
                SpawnInBatchmode = false,
                MachineIp        = spawnerOptions.MachineIp,
                Region           = string.IsNullOrEmpty(spawnerOptions.Region) ? "International" : spawnerOptions.Region
            };

            // Add static handlers to listen one message for all controllers
            connection.SetHandler((short)MsfMessageCodes.SpawnProcessRequest, SpawnProcessRequestHandler);
            connection.SetHandler((short)MsfMessageCodes.KillProcessRequest, KillProcessRequestHandler);
        }
Exemple #7
0
 /// <summary>
 /// Sends a request to master server, to register an existing spawner with given options
 /// </summary>
 /// <param name="options"></param>
 /// <param name="callback"></param>
 public void RegisterSpawner(SpawnerOptions options, RegisterSpawnerCallback callback)
 {
     RegisterSpawner(options, callback, Connection);
 }
Exemple #8
0
        /// <summary>
        /// Start spawner. But before start we are required to be connected
        /// </summary>
        public virtual void StartSpawner()
        {
            // Stop if no connection
            if (!Msf.Connection.IsConnected)
            {
                logger.Error("Spawner cannot be started because of the lack of connection to the master.");
                return;
            }

            // In case we went from one scene to another, but we've already started the spawner
            if (IsSpawnerStarted)
            {
                return;
            }

            // We do not want to use public IP
            if (!usePublicIp)
            {
                // If machine IP is defined in cmd
                machineIp = Msf.Args.ExtractValue(Msf.Args.Names.RoomIp, machineIp);
            }

            // If room region is defined in cmd
            region = Msf.Args.ExtractValue(Msf.Args.Names.RoomRegion, region);

            IsSpawnerStarted = true;

            // Create spawner options
            var spawnerOptions = new SpawnerOptions
            {
                // If MaxProcesses count defined in cmd args
                MaxProcesses = Msf.Args.ExtractValueInt(Msf.Args.Names.MaxProcesses, maxProcesses),
                MachineIp    = machineIp,
                Region       = region
            };

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

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

            // 1. Register the spawner
            Msf.Server.Spawners.RegisterSpawner(spawnerOptions, (controller, error) =>
            {
                if (!string.IsNullOrEmpty(error))
                {
                    logger.Error($"Failed to create spawner: {error}");
                    return;
                }

                spawnerController = controller;
                spawnerController.Logger.LogLevel = spawnerLogLevel;

                spawnerController.SpawnSettings.UseWebSockets = Msf.Args.IsProvided(Msf.Args.Names.UseWebSockets)
                    ? Msf.Args.WebGl
                    : spawnWebSocketServers;

                // Set to run in batchmode
                if (spawnInBatchmode && !Msf.Args.DontSpawnInBatchmode)
                {
                    spawnerController.SpawnSettings.SpawnInBatchmode = true;
                }

                // 2. Set the default executable path
                spawnerController.SpawnSettings.ExecutablePath = Msf.Args.ExtractValue(Msf.Args.Names.RoomExecutablePath, executableFilePath);

                // 3. Set the machine IP
                spawnerController.SpawnSettings.MachineIp = machineIp;

                // 4. Set region
                spawnerController.SpawnSettings.Region = spawnerOptions.Region;

                logger.Info("Spawner successfully created. Id: " + controller.SpawnerId);

                OnSpawnerRegistered();
            });
        }
Exemple #9
0
        public virtual void StartSpawner()
        {
            if (!Msf.Connection.IsConnected)
            {
                logger.Error("Spawner cannot be started because of the lack of connection to the master.");
                return;
            }

            // In case we went from one scene to another, but we've already started the spawner
            if (IsSpawnerStarted)
            {
                return;
            }

            IsSpawnerStarted = true;

            var spawnerOptions = new SpawnerOptions
            {
                // If MaxProcesses count defined in cmd args
                MaxProcesses = Msf.Args.IsProvided(Msf.Args.Names.MaxProcesses) ? Msf.Args.MaxProcesses : maxProcesses
            };

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

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

            // 1. Register the spawner
            Msf.Server.Spawners.RegisterSpawner(spawnerOptions, (spawnerController, error) =>
            {
                if (!string.IsNullOrEmpty(error))
                {
                    logger.Error($"Failed to create spawner: {error}");
                    return;
                }

                this.spawnerController = spawnerController;
                this.spawnerController.Logger.LogLevel = spawnerLogLevel;

                spawnerController.DefaultSpawnerSettings.UseWebSockets = Msf.Args.IsProvided(Msf.Args.Names.UseWebSockets)
                    ? Msf.Args.WebGl
                    : spawnWebSocketServers;

                // Set to run in batchmode
                if (spawnInBatchmode && !Msf.Args.DontSpawnInBatchmode)
                {
                    spawnerController.DefaultSpawnerSettings.SpawnInBatchmode = true;
                }

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

                // 3. Set the machine IP
                spawnerController.DefaultSpawnerSettings.MachineIp = Msf.Args.IsProvided(Msf.Args.Names.RoomIp) ?
                                                                     Msf.Args.RoomIp : machineIp;

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

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

                logger.Info("Spawner successfully created. Id: " + spawnerController.SpawnerId);
            });
        }