Skip to content

Commit

Permalink
fix: #1359. Revert "Destroy objects owned by this connection when dis…
Browse files Browse the repository at this point in the history
…connecting (#1179)"

This reverts commit 8931944.
  • Loading branch information
vis2k committed Dec 26, 2019
1 parent 8e77da3 commit 4cc4279
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 33 deletions.
1 change: 1 addition & 0 deletions Assets/Mirror/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public static void Disconnect()
if (connection != null)
{
connection.Disconnect();
connection.Dispose();
connection = null;
RemoveTransportHandlers();
}
Expand Down
41 changes: 40 additions & 1 deletion Assets/Mirror/Runtime/NetworkConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Mirror
/// <para>NetworkConnection objects also act as observers for networked objects. When a connection is an observer of a networked object with a NetworkIdentity, then the object will be visible to corresponding client for the connection, and incremental state changes will be sent to the client.</para>
/// <para>There are many virtual functions on NetworkConnection that allow its behaviour to be customized. NetworkClient and NetworkServer can both be made to instantiate custom classes derived from NetworkConnection by setting their networkConnectionClass member variable.</para>
/// </remarks>
public abstract class NetworkConnection
public abstract class NetworkConnection : IDisposable
{
public readonly HashSet<NetworkIdentity> visList = new HashSet<NetworkIdentity>();

Expand Down Expand Up @@ -82,6 +82,13 @@ internal set
/// </summary>
public NetworkIdentity identity { get; internal set; }

/// <summary>
/// A list of the NetworkIdentity objects owned by this connection. This list is read-only.
/// <para>This includes the player object for the connection - if it has localPlayerAutority set, and any objects spawned with local authority or set with AssignLocalAuthority.</para>
/// <para>This list can be used to validate messages from clients, to ensure that clients are only trying to control objects that they own.</para>
/// </summary>
public readonly HashSet<uint> clientOwnedObjects = new HashSet<uint>();

/// <summary>
/// Setting this to true will log the contents of network message to the console.
/// </summary>
Expand Down Expand Up @@ -122,6 +129,28 @@ internal NetworkConnection(int networkConnectionId)
#pragma warning restore 618
}

~NetworkConnection()
{
Dispose(false);
}

/// <summary>
/// Disposes of this connection, releasing channel buffers that it holds.
/// </summary>
public void Dispose()
{
Dispose(true);
// Take yourself off the Finalization queue
// to prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
clientOwnedObjects.Clear();
}

/// <summary>
/// Disconnects this connection.
/// </summary>
Expand Down Expand Up @@ -332,5 +361,15 @@ internal void TransportReceive(ArraySegment<byte> buffer, int channelId)
Disconnect();
}
}

internal void AddOwnedObject(NetworkIdentity obj)
{
clientOwnedObjects.Add(obj.netId);
}

internal void RemoveOwnedObject(NetworkIdentity obj)
{
clientOwnedObjects.Remove(obj.netId);
}
}
}
32 changes: 0 additions & 32 deletions Assets/Mirror/Runtime/NetworkConnectionToClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ namespace Mirror
{
public class NetworkConnectionToClient : NetworkConnection
{
/// <summary>
/// A list of the NetworkIdentity objects owned by this connection. This list is read-only.
/// <para>This includes the player object for the connection - if it has localPlayerAutority set, and any objects spawned with local authority or set with AssignLocalAuthority.</para>
/// <para>This list can be used to validate messages from clients, to ensure that clients are only trying to control objects that they own.</para>
/// </summary>
public readonly HashSet<NetworkIdentity> clientOwnedObjects = new HashSet<NetworkIdentity>();

public NetworkConnectionToClient(int networkConnectionId) : base(networkConnectionId)
{
}
Expand Down Expand Up @@ -62,31 +55,6 @@ public override void Disconnect()
isReady = false;
Transport.activeTransport.ServerDisconnect(connectionId);
RemoveObservers();
DestroyOwnedObjects();
}

internal void AddOwnedObject(NetworkIdentity obj)
{
clientOwnedObjects.Add(obj);
}

internal void RemoveOwnedObject(NetworkIdentity obj)
{
clientOwnedObjects.Remove(obj);
}

protected void DestroyOwnedObjects()
{
// work on copy because the list may be modified when we destroy the objects
HashSet<NetworkIdentity> objects = new HashSet<NetworkIdentity>(clientOwnedObjects);
foreach (NetworkIdentity netId in objects)
{
if (netId != null)
{
NetworkServer.Destroy(netId.gameObject);
}
}
clientOwnedObjects.Clear();
}
}
}
12 changes: 12 additions & 0 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ internal static void RemoveLocalConnection()
if (localConnection != null)
{
localConnection.Disconnect();
localConnection.Dispose();
localConnection = null;
}
RemoveConnection(0);
Expand Down Expand Up @@ -438,6 +439,7 @@ public static void DisconnectAllConnections()
// call OnDisconnected unless local player in host mode
if (conn.connectionId != 0)
OnDisconnected(conn);
conn.Dispose();
}
connections.Clear();
}
Expand Down Expand Up @@ -1092,6 +1094,16 @@ internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnectio
/// <param name="conn">The connections object to clean up for.</param>
public static void DestroyPlayerForConnection(NetworkConnection conn)
{
// => destroy what we can destroy.
HashSet<uint> tmp = new HashSet<uint>(conn.clientOwnedObjects);
foreach (uint netId in tmp)
{
if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity))
{
Destroy(identity.gameObject);
}
}

if (conn.identity != null)
{
DestroyObject(conn.identity, true);
Expand Down

0 comments on commit 4cc4279

Please sign in to comment.