public virtual RegisteredSpawner CreateSpawner(IPeer peer, SpawnerOptions options)
        {
            var spawner = new RegisteredSpawner(GenerateSpawnerId(), peer, options);

            Dictionary <int, RegisteredSpawner> peerSpawners = peer.GetProperty((int)MsfPeerPropertyCodes.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)MsfPeerPropertyCodes.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);
        }
        /// <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)MsfMessageCodes.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
                _locallyCreatedSpawners[spawnerId] = controller;

                callback.Invoke(controller, null);

                // Invoke the event
                OnSpawnerRegisteredEvent?.Invoke(controller);
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates spawner for given peer using options
        /// </summary>
        /// <param name="peer"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public virtual RegisteredSpawner CreateSpawner(IPeer peer, SpawnerOptions options)
        {
            // Create registered spawner instance
            var spawnerInstance = new RegisteredSpawner(GenerateSpawnerId(), peer, options);

            // Find spawners in peer property
            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);

                // Listen to disconnection
                peer.OnPeerDisconnectedEvent += OnRegisteredPeerDisconnect;
            }

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

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

            // Invoke the event
            OnSpawnerRegisteredEvent?.Invoke(spawnerInstance);

            return(spawnerInstance);
        }