NetworkServer: broadcast to connection code moved into BroadcastToConnection

This commit is contained in:
vis2k 2021-05-26 17:48:53 +08:00
parent 7af1156d4a
commit 2085cd387f

View File

@ -1438,45 +1438,8 @@ static Serialization GetEntitySerialization(NetworkIdentity identity)
return serializations[identity];
}
// NetworkLateUpdate called after any Update/FixedUpdate/LateUpdate
// (we add this to the UnityEngine in NetworkLoop)
static readonly List<NetworkConnectionToClient> connectionsCopy =
new List<NetworkConnectionToClient>();
internal static void NetworkLateUpdate()
{
// only process spawned & connections if active
if (active)
{
// copy all connections into a helper collection so that
// OnTransportDisconnected can be called while iterating.
// -> OnTransportDisconnected removes from the collection
// -> which would throw 'can't modify while iterating' errors
// => see also: https://github.com/vis2k/Mirror/issues/2739
// (copy nonalloc)
// TODO remove this when we move to 'lite' transports with only
// socket send/recv later.
connectionsCopy.Clear();
connections.Values.CopyTo(connectionsCopy);
// go through all connections
foreach (NetworkConnectionToClient connection in connectionsCopy)
{
// check for inactivity
#pragma warning disable 618
if (disconnectInactiveConnections &&
!connection.IsAlive(disconnectInactiveTimeout))
{
Debug.LogWarning($"Disconnecting {connection} for inactivity!");
connection.Disconnect();
continue;
}
#pragma warning restore 618
// has this connection joined the world yet?
// for each READY connection:
// pull in UpdateVarsMessage for each entity it observes
if (connection.isReady)
// helper function to broadcast the world to a connection
static void BroadcastToConnection(NetworkConnectionToClient connection)
{
// for each entity that this connection is seeing
foreach (NetworkIdentity identity in connection.observing)
@ -1531,6 +1494,50 @@ internal static void NetworkLateUpdate()
}
}
// NetworkLateUpdate called after any Update/FixedUpdate/LateUpdate
// (we add this to the UnityEngine in NetworkLoop)
static readonly List<NetworkConnectionToClient> connectionsCopy =
new List<NetworkConnectionToClient>();
internal static void NetworkLateUpdate()
{
// only process spawned & connections if active
if (active)
{
// copy all connections into a helper collection so that
// OnTransportDisconnected can be called while iterating.
// -> OnTransportDisconnected removes from the collection
// -> which would throw 'can't modify while iterating' errors
// => see also: https://github.com/vis2k/Mirror/issues/2739
// (copy nonalloc)
// TODO remove this when we move to 'lite' transports with only
// socket send/recv later.
connectionsCopy.Clear();
connections.Values.CopyTo(connectionsCopy);
// go through all connections
foreach (NetworkConnectionToClient connection in connectionsCopy)
{
// check for inactivity
#pragma warning disable 618
if (disconnectInactiveConnections &&
!connection.IsAlive(disconnectInactiveTimeout))
{
Debug.LogWarning($"Disconnecting {connection} for inactivity!");
connection.Disconnect();
continue;
}
#pragma warning restore 618
// has this connection joined the world yet?
// for each READY connection:
// pull in UpdateVarsMessage for each entity it observes
if (connection.isReady)
{
// broadcast world state to this connection
BroadcastToConnection(connection);
}
// update connection to flush out batched messages
connection.Update();
}