using System; using System.Collections.Generic; namespace Mirror { public class NetworkConnectionToClient : NetworkConnection { public override string address => Transport.activeTransport.ServerGetClientAddress(connectionId); /// NetworkIdentities that this connection can see // TODO move to server's NetworkConnectionToClient? public new readonly HashSet observing = new HashSet(); /// All NetworkIdentities owned by this connection. Can be main player, pets, etc. // IMPORTANT: this needs to be , not . // fixes a bug where DestroyOwnedObjects wouldn't find the // netId anymore: https://github.com/vis2k/Mirror/issues/1380 // Works fine with NetworkIdentity pointers though. public new readonly HashSet clientOwnedObjects = new HashSet(); // unbatcher public Unbatcher unbatcher = new Unbatcher(); public NetworkConnectionToClient(int networkConnectionId) : base(networkConnectionId) {} // Send stage three: hand off to transport protected override void SendToTransport(ArraySegment segment, int channelId = Channels.Reliable) => Transport.activeTransport.ServerSend(connectionId, segment, channelId); /// Disconnects this connection. public override void Disconnect() { // set not ready and handle clientscene disconnect in any case // (might be client or host mode here) isReady = false; Transport.activeTransport.ServerDisconnect(connectionId); // IMPORTANT: NetworkConnection.Disconnect() is NOT called for // voluntary disconnects from the other end. // -> so all 'on disconnect' cleanup code needs to be in // OnTransportDisconnect, where it's called for both voluntary // and involuntary disconnects! } internal void AddToObserving(NetworkIdentity netIdentity) { observing.Add(netIdentity); // spawn identity for this conn NetworkServer.ShowForConnection(netIdentity, this); } internal void RemoveFromObserving(NetworkIdentity netIdentity, bool isDestroyed) { observing.Remove(netIdentity); if (!isDestroyed) { // hide identity for this conn NetworkServer.HideForConnection(netIdentity, this); } } internal void RemoveFromObservingsObservers() { foreach (NetworkIdentity netIdentity in observing) { netIdentity.RemoveObserver(this); } observing.Clear(); } internal void AddOwnedObject(NetworkIdentity obj) { clientOwnedObjects.Add(obj); } internal void RemoveOwnedObject(NetworkIdentity obj) { clientOwnedObjects.Remove(obj); } internal void DestroyOwnedObjects() { // create a copy because the list might be modified when destroying HashSet tmp = new HashSet(clientOwnedObjects); foreach (NetworkIdentity netIdentity in tmp) { if (netIdentity != null) { NetworkServer.Destroy(netIdentity.gameObject); } } // clear the hashset because we destroyed them all clientOwnedObjects.Clear(); } } }