/// <summary> /// Hides an object from all clients. /// Server only. /// </summary> public void NetworkHideAll() { //A faster message send bypassing MessageSender completely using (PooledBitStream baseStream = PooledBitStream.Get()) { DoVisibleHideWrite(baseStream); baseStream.PadStream(); using (BitStream stream = MessagePacker.WrapMessage(networkBehaviourManager.unspawnMessageType, 0, baseStream, SecuritySendFlags.None)) { using (List <ulong> .Enumerator clients = networkManager.clients) { while (clients.MoveNext()) { if (clients.Current == networkManager.serverID) { continue; } if (!m_PendingObservers.Remove(clients.Current) && !m_Observers.Remove(clients.Current)) { continue; } networkManager.transport.Send(clients.Current, new ArraySegment <byte>(stream.GetBuffer(), 0, (int)stream.Length), networkManager.networkInternalChannel); } } } } }
/// <summary> /// Removes all ownership of an object from any client and returns ownership to the server. Can only be called by the server or the owner of this Network Behaviour. /// </summary> public void RemoveOwnership() { if (!networkManager.isRunning) { Debug.LogError("Cannot remove ownership. The network is not running."); return; } if (!isNetworkSpawned) { throw new NetworkException("Cannot change ownership. This Network Behaviour is not spawned."); } if (!isServer && !isOwner) { throw new NotServerException("Only the server can call NetworkBehaviour.RemoveOwnership when they are not the owner of the Network Behaviour."); } //Owner does not change if (isOwnedByServer) { return; } if (isServer) { m_OwnerClientID = networkManager.serverID; OnGainedOwnership(); } using (PooledBitStream baseStream = PooledBitStream.Get()) { DoOwnershipWrite(baseStream, networkManager.serverID); if (isServer) { baseStream.PadStream(); using (BitStream stream = MessagePacker.WrapMessage(networkBehaviourManager.ownerChangeMessageType, 0, baseStream, SecuritySendFlags.None)) { using (HashSet <ulong> .Enumerator observers = GetObservers()) { while (observers.MoveNext()) { if (observers.Current == NetworkManager.Get().serverID) { continue; } NetworkManager.Get().transport.Send(observers.Current, new ArraySegment <byte>(stream.GetBuffer(), 0, (int)stream.Length), networkManager.networkInternalChannel); } } } } else { MessageSender.Send(networkManager.serverID, networkBehaviourManager.ownerChangeMessageType, networkManager.networkInternalChannel, baseStream); } } }
/// <summary> /// Does not supress errors invloving clients that already have visibility of this Network Behaviour. /// Server only. /// </summary> /// <param name="clientIDs"></param> public void NetworkShow(List <ulong> clientIDs, Stream spawnPayload = null) { //A faster message send bypassing MessageSender completely if (clientIDs == null) { throw new ArgumentNullException(nameof(clientIDs)); } using (PooledBitStream baseStream = PooledBitStream.Get()) { DoVisibleShowWrite(baseStream, spawnPayload); baseStream.PadStream(); if (clientIDs.Count == 0) { return; //No one to send to. } using (BitStream stream = MessagePacker.WrapMessage(networkBehaviourManager.spawnMessageType, 0, baseStream, SecuritySendFlags.None)) { for (int i = 0; i < clientIDs.Count; i++) { if (clientIDs[i] == NetworkManager.Get().serverID) { continue; } if (m_PendingObservers.Contains(clientIDs[i])) { Debug.LogError("This Network Behaviour is already pending visibility to client '" + clientIDs[i] + "'. Watch for a call to NetworkStart for when it successfully connects.", this); continue; } if (m_Observers.Contains(clientIDs[i])) { Debug.LogError("This Network Behaviour is already pending visibility to client '" + clientIDs[i] + "'. Watch for a call to NetworkStart for when it successfully connects.", this); continue; } m_PendingObservers.Add(clientIDs[i]); NetworkManager.Get().transport.Send(clientIDs[i], new ArraySegment <byte>(stream.GetBuffer(), 0, (int)stream.Length), networkManager.networkInternalChannel); } } } }
static public void SendToAll(byte messageType, byte channel, BitStream messageStream) { messageStream.PadStream(); using (BitStream stream = MessagePacker.WrapMessage(messageType, 0, messageStream, SecuritySendFlags.None)) { using (List <ulong> .Enumerator clients = NetworkManager.Get().clients) { while (clients.MoveNext()) { if (NetworkManager.Get().isServer&& clients.Current == NetworkManager.Get().serverID) { continue; } NetworkManager.Get().transport.Send(clients.Current, new ArraySegment <byte>(stream.GetBuffer(), 0, (int)stream.Length), channel); } } } }
/// <summary> /// Hides an object from a specific clients. /// Server only. /// </summary> /// <param name="clientIDs">The clients to hide the object from.</param> public void NetworkHide(List <ulong> clientIDs) { //A faster message send bypassing MessageSender completely if (clientIDs == null) { throw new ArgumentNullException(nameof(clientIDs)); } using (PooledBitStream baseStream = PooledBitStream.Get()) { DoVisibleHideWrite(baseStream); baseStream.PadStream(); if (clientIDs.Count == 0) { return; //No one to send to. } using (BitStream stream = MessagePacker.WrapMessage(networkBehaviourManager.unspawnMessageType, 0, baseStream, SecuritySendFlags.None)) { for (int i = 0; i < clientIDs.Count; i++) { if (clientIDs[i] == networkManager.serverID) { continue; } if (!m_PendingObservers.Remove(clientIDs[i]) && !m_Observers.Remove(clientIDs[i])) { Debug.LogError("This Network Behaviour is already not visible to client '" + clientIDs[i] + "'.", this); continue; } networkManager.transport.Send(clientIDs[i], new ArraySegment <byte>(stream.GetBuffer(), 0, (int)stream.Length), networkManager.networkInternalChannel); } } } }
static public void SendToAllExcept(ulong clientIDToIgnore, MessageType messageType, byte channel, BitStream messageStream) { SendToAllExcept(clientIDToIgnore, (byte)messageType, channel, messageStream); }
static public void SendToAllExcept(ulong clientIDToIgnore, MessageType messageType, string channelName, BitStream messageStream) { SendToAllExcept(clientIDToIgnore, (byte)messageType, NetworkManager.Get().transport.GetChannelByName(channelName), messageStream); }
static public void SendToAllExcept(ulong clientIDToIgnore, byte messageType, BitStream messageStream) { SendToAllExcept(clientIDToIgnore, messageType, NetworkTransport.DEFAULT_CHANNEL, messageStream); }
static public void SendToAll(MessageType messageType, byte channel, BitStream messageStream) { SendToAll((byte)messageType, channel, messageStream); }
static public void SendToAll(byte messageType, string channelName, BitStream messageStream) { SendToAll(messageType, NetworkManager.Get().transport.GetChannelByName(channelName), messageStream); }
static public void SendToAll(byte messageType, BitStream messageStream) { SendToAll(messageType, NetworkTransport.DEFAULT_CHANNEL, messageStream); }
/// <summary> /// Changes the owner of the object. Can only be called from the server or the owner of this Network Behaviour. /// </summary> /// <param name="targetClientID">The new owner clientId</param> public void SetOwner(ulong targetClientID) { if (!networkManager.isRunning) { Debug.LogError("Cannot set ownership. The network is not running."); return; } if (!isServer) { if (isOwner && targetClientID == networkManager.serverID) { RemoveOwnership(); return; } throw new NotServerException("Only the server can call NetworkBehaviour.SetOwner to target anything but the server."); } if (!isNetworkSpawned) { throw new NetworkException("Cannot change ownership. This Network Behaviour is not spawned."); } if (!IsNetworkVisibleTo(targetClientID)) { throw new NetworkException("Cannot change ownership to a client that does not have visibility of this Network Behaviour."); } //Owner does not change if (targetClientID == ownerID) { return; } if (targetClientID == networkManager.serverID) { RemoveOwnership(); return; } if (isOwner) { m_OwnerClientID = targetClientID; OnLostOwnership(); } else //This may seem redundant but we want ownership changes to be set before the OnLostOwnership call { m_OwnerClientID = targetClientID; } //Send to all (not pending)observers using (PooledBitStream baseStream = PooledBitStream.Get()) { DoOwnershipWrite(baseStream, targetClientID); baseStream.PadStream(); using (BitStream stream = MessagePacker.WrapMessage(networkBehaviourManager.ownerChangeMessageType, 0, baseStream, SecuritySendFlags.None)) { using (HashSet <ulong> .Enumerator observers = GetObservers()) { while (observers.MoveNext()) { if (observers.Current == NetworkManager.Get().serverID) { continue; } NetworkManager.Get().transport.Send(observers.Current, new ArraySegment <byte>(stream.GetBuffer(), 0, (int)stream.Length), networkManager.networkInternalChannel); } } } } }