Skip to content

Commit

Permalink
perf: adding check for no connections (#2130)
Browse files Browse the repository at this point in the history
* adding check for no connections

Dont need to run Server Update if there are no connections

* adding toggle to disable skip

* formatt

* removing skipUpdateIfNoConnections check, and merging if checks

* removing extra line

* adding tests for NoConnections
  • Loading branch information
James-Frowen committed Aug 21, 2020
1 parent 5b02c8b commit 150b14a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,23 @@ public static void DisconnectAllConnections()
connections.Clear();
}

/// <summary>
/// If connections is empty or if only has host
/// </summary>
/// <returns></returns>
public static bool NoConnections()
{
return connections.Count == 0 || (connections.Count == 1 && localConnection != null);
}

/// <summary>
/// Called from NetworkManager in LateUpdate
/// <para>The user should never need to pump the update loop manually</para>
/// </summary>
public static void Update()
{
if (!active)
// dont need to update server if not active or no client connections
if (!active || NoConnections())
return;

// Check for dead clients but exclude the host client because it
Expand Down
56 changes: 56 additions & 0 deletions Assets/Mirror/Tests/Editor/NetworkServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,5 +1157,61 @@ public void SendCalledWhileNotActive_ShouldGiveWarning(string functionName)
break;
}
}


[Test]
public void NoConnectionsTest_WithNoConnection()
{
Assert.That(NetworkServer.NoConnections(), Is.True);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));
}

[Test]
public void NoConnectionsTest_WithConnections()
{
NetworkServer.connections.Add(1, null);
NetworkServer.connections.Add(2, null);
Assert.That(NetworkServer.NoConnections(), Is.False);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(2));

NetworkServer.connections.Clear();
}

[Test]
public void NoConnectionsTest_WithHostOnly()
{
ULocalConnectionToServer connectionToServer = new ULocalConnectionToServer();
ULocalConnectionToClient connectionToClient = new ULocalConnectionToClient();
connectionToServer.connectionToClient = connectionToClient;
connectionToClient.connectionToServer = connectionToServer;

NetworkServer.SetLocalConnection(connectionToClient);
NetworkServer.connections.Add(0, connectionToClient);

Assert.That(NetworkServer.NoConnections(), Is.True);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(1));

NetworkServer.connections.Clear();
NetworkServer.RemoveLocalConnection();
}

[Test]
public void NoConnectionsTest_WithHostAndConnection()
{
ULocalConnectionToServer connectionToServer = new ULocalConnectionToServer();
ULocalConnectionToClient connectionToClient = new ULocalConnectionToClient();
connectionToServer.connectionToClient = connectionToClient;
connectionToClient.connectionToServer = connectionToServer;

NetworkServer.SetLocalConnection(connectionToClient);
NetworkServer.connections.Add(0, connectionToClient);
NetworkServer.connections.Add(1, null);

Assert.That(NetworkServer.NoConnections(), Is.False);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(2));

NetworkServer.connections.Clear();
NetworkServer.RemoveLocalConnection();
}
}
}

0 comments on commit 150b14a

Please sign in to comment.