Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Nov 25, 2020
1 parent b008504 commit e55671d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
8 changes: 6 additions & 2 deletions Assets/Mirror/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ internal static void OnDataReceived(ArraySegment<byte> data, int channelId)
/// <summary>
/// Handles OnClientConnected from transport
/// </summary>
static void OnConnectedInternal()
internal static void OnConnectedInternal()
{
if (connection != null)
{
Expand Down Expand Up @@ -393,7 +393,11 @@ public static void Shutdown()
// we do NOT call Transport.Shutdown, because someone only called
// NetworkClient.Shutdown. we can't assume that the server is
// supposed to be shut down too!
Transport.activeTransport.ClientDisconnect();
Transport.activeTransport?.ClientDisconnect();

// clear event listeners
OnConnected = null;
OnDisconnected = null;
}
}
}
7 changes: 5 additions & 2 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public static void Shutdown()

CleanupNetworkIdentities();
NetworkIdentity.ResetNextNetworkId();

OnConnected = null;
OnDisconnected = null;
}

static void CleanupNetworkIdentities()
Expand Down Expand Up @@ -437,7 +440,7 @@ public static void DisconnectAllConnections()
conn.Disconnect();
// call OnDisconnected unless local player in host mode
if (conn.connectionId != NetworkConnection.LocalConnectionId)
OnDisconnected(conn);
OnDisconnectedInternal(conn);
}
connections.Clear();
}
Expand Down Expand Up @@ -534,7 +537,7 @@ static void OnConnectedInternal(int connectionId)
{
// add connection
NetworkConnectionToClient conn = new NetworkConnectionToClient(connectionId);
OnConnected(conn);
OnConnectedInternal(conn);
}
else
{
Expand Down
8 changes: 0 additions & 8 deletions Assets/Mirror/Tests/Common/MemoryTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,12 @@ public override void ServerSend(int connectionId, int channelId, ArraySegment<by

public override bool ServerDisconnect(int connectionId)
{
// clear all pending messages that we may have received.
// over the wire, we wouldn't receive any more pending messages
// ether after calling disconnect.
serverIncoming.Clear();

// add client disconnected message with connectionId
clientIncoming.Enqueue(new Message(connectionId, EventType.Disconnected, null));

// add server disconnected message with connectionId
serverIncoming.Enqueue(new Message(connectionId, EventType.Disconnected, null));

// not active anymore
serverActive = false;

return false;
}

Expand Down
37 changes: 37 additions & 0 deletions Assets/Mirror/Tests/Editor/NetworkClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,42 @@ public void Send()
// received it on server?
Assert.That(called, Is.EqualTo(1));
}

[Test]
public void ShutdownClearsEvents()
{
int called = 0;
NetworkClient.OnConnected += (c) => { called++; };

NetworkClient.Connect("localhost");
((MemoryTransport)Transport.activeTransport).LateUpdate();

Assert.That(called, Is.EqualTo(1));

// remove events
NetworkClient.Disconnect();
NetworkClient.Shutdown();

NetworkClient.Connect("localhost");
((MemoryTransport)Transport.activeTransport).LateUpdate();

Assert.That(called, Is.EqualTo(1), "Call count should still be 1");

// reset
NetworkClient.Disconnect();
NetworkClient.Shutdown();


// add event again
NetworkClient.OnConnected += (c) =>
{
called++;
};

NetworkClient.Connect("localhost");
((MemoryTransport)Transport.activeTransport).LateUpdate();

Assert.That(called, Is.EqualTo(2), "Call count should now be 2");
}
}
}

0 comments on commit e55671d

Please sign in to comment.