SyncList uses SyncListMessage to reduce dependencies on SendBytes

This commit is contained in:
vis2k 2018-07-26 21:07:34 +02:00
parent 14fcafda0c
commit 3ceebaec78
3 changed files with 37 additions and 12 deletions

View File

@ -657,25 +657,24 @@ static void OnSyncEventMessage(NetworkMessage netMsg)
static void OnSyncListMessage(NetworkMessage netMsg)
{
var netId = netMsg.reader.ReadNetworkId();
var cmdHash = (int)netMsg.reader.ReadPackedUInt32();
SyncListMessage message = netMsg.ReadMessage<SyncListMessage>();
if (LogFilter.logDebug) { Debug.Log("ClientScene::OnSyncListMessage " + netId); }
if (LogFilter.logDebug) { Debug.Log("ClientScene::OnSyncListMessage " + message.netId); }
NetworkIdentity uv;
if (s_NetworkScene.GetNetworkIdentity(netId, out uv))
if (s_NetworkScene.GetNetworkIdentity(message.netId, out uv))
{
uv.HandleSyncList(cmdHash, netMsg.reader);
uv.HandleSyncList(message.syncListHash, new NetworkReader(message.payload));
}
else
{
if (LogFilter.logWarn) { Debug.LogWarning("Did not find target for SyncList message for " + netId); }
if (LogFilter.logWarn) { Debug.LogWarning("Did not find target for SyncList message for " + message.netId); }
}
#if UNITY_EDITOR
UnityEditor.NetworkDetailStats.IncrementStat(
UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
(short)MsgType.SyncList, NetworkBehaviour.GetCmdHashHandlerName(cmdHash), 1);
(short)MsgType.SyncList, NetworkBehaviour.GetCmdHashHandlerName(message.syncListHash), 1);
#endif
}

View File

@ -203,6 +203,27 @@ public override void Serialize(NetworkWriter writer)
}
}
class SyncListMessage : MessageBase
{
public NetworkInstanceId netId;
public int syncListHash;
public byte[] payload;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
syncListHash = reader.ReadInt32(); // hash is always 4 full bytes, WritePackedInt would send 1 extra byte here
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.Write(syncListHash);
writer.WriteBytesAndSize(payload);
}
}
/* These are not used directly but manually serialized, these are here for reference.
internal class SyncListMessage<T> where T: struct
{

View File

@ -3,6 +3,7 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine.Networking.NetworkSystem;
namespace UnityEngine.Networking
{
@ -249,16 +250,20 @@ void SendMsg(Operation op, int itemIndex, T item)
return;
}
// construct and send message
SyncListMessage message = new SyncListMessage();
message.netId = uv.netId;
message.syncListHash = m_CmdHash;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage((short)MsgType.SyncList);
writer.Write(uv.netId);
writer.WritePackedUInt32((uint)m_CmdHash);
writer.Write((byte)op);
writer.WritePackedUInt32((uint)itemIndex);
SerializeItem(writer, item);
writer.FinishMessage();
NetworkServer.SendBytesToReady(uv.gameObject, writer.ToArray(), m_Behaviour.GetNetworkChannel());
message.payload = writer.ToArray();
NetworkServer.SendByChannelToReady(uv.gameObject, (short)MsgType.SyncList, message, m_Behaviour.GetNetworkChannel());
#if UNITY_EDITOR
UnityEditor.NetworkDetailStats.IncrementStat(