예제 #1
0
        public virtual RegisteredSpawner CreateSpawner(IPeer peer, SpawnerOptions options)
        {
            var spawner = new RegisteredSpawner(GenerateSpawnerId(), peer, options);

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

            // If this is the first time registering a spawners
            if (peerSpawners == null)
            {
                // Save the dictionary
                peerSpawners = new Dictionary <int, RegisteredSpawner>();
                peer.SetProperty((int)MstPeerPropertyCodes.RegisteredSpawners, peerSpawners);

                peer.OnPeerDisconnectedEvent += OnRegisteredPeerDisconnect;
            }

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

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

            // Invoke the event
            if (OnSpawnerRegisteredEvent != null)
            {
                OnSpawnerRegisteredEvent.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, IClientSocket connection)
        {
            if (!connection.IsConnected)
            {
                callback.Invoke(null, "Not connected");
                return;
            }

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

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

                // Save reference
                createdSpawnerControllers[spawnerId] = controller;

                callback.Invoke(controller, null);

                // Invoke the event
                OnSpawnerRegisteredEvent?.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>();
        }
예제 #4
0
        /// <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 = Mst.Create.Logger(typeof(SpawnerController).Name, LogLevel.Info);

            Connection = connection;
            SpawnerId  = spawnerId;

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

            // Add static handlers to listen one message for all controllers
            connection.SetHandler((short)MstMessageCodes.SpawnProcessRequest, SpawnProcessRequestHandler);
            connection.SetHandler((short)MstMessageCodes.KillProcessRequest, KillProcessRequestHandler);
        }
예제 #5
0
        /// <summary>
        /// Starts spawner. But before start we are required to be connected
        /// </summary>
        public virtual void StartSpawner()
        {
            // Stop if no connection
            if (!Mst.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;
            }

            // If machine IP is defined in cmd
            machineIp = Mst.Args.AsString(Mst.Args.Names.RoomIp, machineIp);

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

            IsSpawnerStarted = true;

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

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

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

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

                // 2. Save spawner controller
                spawnerController = controller;

                // 3. Set its log level
                spawnerController.Logger.LogLevel = spawnerLogLevel;

                // 4. Set use web sockets if required
                spawnerController.SpawnSettings.UseWebSockets = Mst.Args.AsBool(Mst.Args.Names.UseWebSockets, spawnWebSocketServers);

                // 5. Set the default executable path
                spawnerController.SpawnSettings.ExecutablePath = Mst.Args.AsString(Mst.Args.Names.RoomExecutablePath, executableFilePath);

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

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

                logger.Info($"Spawner successfully created. Id: {controller.SpawnerId}");

                // 8. Inform listeners
                OnSpawnerStartedEvent?.Invoke();
                OnSpawnerStarted();
            });
        }
예제 #6
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);
 }
예제 #7
0
        /// <summary>
        /// Start spawner. But before start we are required to be connected
        /// </summary>
        public virtual void StartSpawner()
        {
            // Stop if no connection
            if (!Mst.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 = Mst.Args.ExtractValue(Mst.Args.Names.RoomIp, machineIp);
            }

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

            IsSpawnerStarted = true;

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

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

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

            // 1. Register the spawner
            Mst.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 = Mst.Args.IsProvided(Mst.Args.Names.UseWebSockets)
                    ? Mst.Args.WebGl
                    : spawnWebSocketServers;

                // 2. Set the default executable path
                spawnerController.SpawnSettings.ExecutablePath = Mst.Args.ExtractValue(Mst.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();
            });
        }