NetworkServer: GetEntitySerializationForConnection helper function

This commit is contained in:
vis2k 2021-05-26 18:04:48 +08:00
parent ca319fdb32
commit b4cecccbcb

View File

@ -1429,6 +1429,39 @@ static Serialization GetEntitySerialization(NetworkIdentity identity)
return serializations[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 // helper function to clean up cached serializations
static void CleanupSerializations() static void CleanupSerializations()
{ {
@ -1476,42 +1509,19 @@ static void BroadcastToConnection(NetworkConnectionToClient connection)
// NetworkServer.Destroy) // NetworkServer.Destroy)
if (identity != null) if (identity != null)
{ {
// get serialization for this entity (cached) // get serialization for this entity viewed by this connection
Serialization serialization = GetEntitySerialization(identity); // (if anything was serialized this time)
NetworkWriter serialization = GetEntitySerializationForConnection(identity, connection);
// is this entity owned by this connection? if (serialization != null)
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)
{ {
UpdateVarsMessage message = new UpdateVarsMessage UpdateVarsMessage message = new UpdateVarsMessage
{ {
netId = identity.netId, netId = identity.netId,
payload = serialization.ownerWriter.ToArraySegment() payload = serialization.ToArraySegment()
}; };
connection.Send(message); 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);
}
}
}
// spawned list should have no null entries because we // spawned list should have no null entries because we
// always call Remove in OnObjectDestroy everywhere. // always call Remove in OnObjectDestroy everywhere.
// if it does have null then someone used // if it does have null then someone used