From b4cecccbcb5be6416ddb3edbcfb26a04e8c7ae45 Mon Sep 17 00:00:00 2001 From: vis2k Date: Wed, 26 May 2021 18:04:48 +0800 Subject: [PATCH] NetworkServer: GetEntitySerializationForConnection helper function --- Assets/Mirror/Runtime/NetworkServer.cs | 74 +++++++++++++++----------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 47a31c96b..66508e135 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -1429,6 +1429,39 @@ static Serialization GetEntitySerialization(NetworkIdentity identity) return serializations[identity]; } + // helper function to get the right serialization for a connection + static NetworkWriter GetEntitySerializationForConnection(NetworkIdentity identity, NetworkConnectionToClient connection) + { + // get serialization for this entity (cached) + Serialization serialization = GetEntitySerialization(identity); + + // is this entity owned by this connection? + bool owned = identity.connectionToClient == connection; + + // send serialized data + // owner writer if owned + if (owned) + { + // was it dirty / did we actually serialize anything? + if (serialization.ownerWritten > 0) + { + return serialization.ownerWriter; + } + } + // observers writer if not owned + else + { + // was it dirty / did we actually serialize anything? + if (serialization.observersWritten > 0) + { + return serialization.observersWriter; + } + } + + // nothing was serialized + return null; + } + // helper function to clean up cached serializations static void CleanupSerializations() { @@ -1476,40 +1509,17 @@ static void BroadcastToConnection(NetworkConnectionToClient connection) // NetworkServer.Destroy) if (identity != null) { - // get serialization for this entity (cached) - Serialization serialization = GetEntitySerialization(identity); - - // is this entity owned by this connection? - bool owned = identity.connectionToClient == connection; - - // send serialized data - // owner writer if owned - if (owned) + // get serialization for this entity viewed by this connection + // (if anything was serialized this time) + NetworkWriter serialization = GetEntitySerializationForConnection(identity, connection); + if (serialization != null) { - // was it dirty / did we actually serialize anything? - if (serialization.ownerWritten > 0) + UpdateVarsMessage message = new UpdateVarsMessage { - UpdateVarsMessage message = new UpdateVarsMessage - { - netId = identity.netId, - payload = serialization.ownerWriter.ToArraySegment() - }; - connection.Send(message); - } - } - // observers writer if not owned - else - { - // was it dirty / did we actually serialize anything? - if (serialization.observersWritten > 0) - { - UpdateVarsMessage message = new UpdateVarsMessage - { - netId = identity.netId, - payload = serialization.observersWriter.ToArraySegment() - }; - connection.Send(message); - } + netId = identity.netId, + payload = serialization.ToArraySegment() + }; + connection.Send(message); } } // spawned list should have no null entries because we