NetworkIdentity.UNetUpdate uses UpdateVarsMessage to reduce depencies on SendBytes

This commit is contained in:
vis2k 2018-07-26 20:30:47 +02:00
parent 2b54665915
commit 14fcafda0c
3 changed files with 31 additions and 13 deletions

View File

@ -596,17 +596,18 @@ static void OnLocalClientSpawnSceneObject(NetworkMessage netMsg)
static void OnUpdateVarsMessage(NetworkMessage netMsg) static void OnUpdateVarsMessage(NetworkMessage netMsg)
{ {
NetworkInstanceId netId = netMsg.reader.ReadNetworkId(); UpdateVarsMessage message = netMsg.ReadMessage<UpdateVarsMessage>();
if (LogFilter.logDev) { Debug.Log("ClientScene::OnUpdateVarsMessage " + netId + " channel:" + netMsg.channelId); }
if (LogFilter.logDev) { Debug.Log("ClientScene::OnUpdateVarsMessage " + message.netId + " channel:" + netMsg.channelId); }
NetworkIdentity localObject; NetworkIdentity localObject;
if (s_NetworkScene.GetNetworkIdentity(netId, out localObject)) if (s_NetworkScene.GetNetworkIdentity(message.netId, out localObject))
{ {
localObject.OnUpdateVars(netMsg.reader, false); localObject.OnUpdateVars(new NetworkReader(message.payload), false);
} }
else else
{ {
if (LogFilter.logWarn) { Debug.LogWarning("Did not find target for sync message for " + netId + " . Note: this can be completely normal because UDP messages may arrive out of order, so this message might have arrived after a Destroy message."); } if (LogFilter.logWarn) { Debug.LogWarning("Did not find target for sync message for " + message.netId + " . Note: this can be completely normal because UDP messages may arrive out of order, so this message might have arrived after a Destroy message."); }
} }
} }

View File

@ -333,6 +333,24 @@ public override void Serialize(NetworkWriter writer)
} }
} }
class UpdateVarsMessage : MessageBase
{
public NetworkInstanceId netId;
public byte[] payload;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.WriteBytesAndSize(payload);
}
}
class AnimationMessage : MessageBase class AnimationMessage : MessageBase
{ {
public NetworkInstanceId netId; public NetworkInstanceId netId;

View File

@ -742,12 +742,8 @@ internal void UNetUpdate()
// go through each channel // go through each channel
for (int channelId = 0; channelId < NetworkServer.numChannels; channelId++) for (int channelId = 0; channelId < NetworkServer.numChannels; channelId++)
{ {
// prepare message header
NetworkWriter writer = new NetworkWriter();
writer.StartMessage((short)MsgType.UpdateVars);
writer.Write(netId);
// serialize all the dirty components and send (if any were dirty) // serialize all the dirty components and send (if any were dirty)
NetworkWriter writer = new NetworkWriter();
if (OnSerializeAllSafely(m_NetworkBehaviours, writer, false, channelId)) if (OnSerializeAllSafely(m_NetworkBehaviours, writer, false, channelId))
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
@ -755,9 +751,12 @@ internal void UNetUpdate()
UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
(short)MsgType.UpdateVars, name, 1); (short)MsgType.UpdateVars, name, 1);
#endif #endif
// finish message and send // construct message and send
writer.FinishMessage(); UpdateVarsMessage message = new UpdateVarsMessage();
NetworkServer.SendBytesToReady(gameObject, writer.ToArray(), channelId); message.netId = netId;
message.payload = writer.ToArray();
NetworkServer.SendByChannelToReady(gameObject, (short)MsgType.UpdateVars, message, channelId);
} }
} }
} }