Weaver SyncEvent code doesn't manually construct the message anymore. The NetworkWriter is now passed to NetworkBehaviour.SendEventInternal, where the message is properly constructed. Reduces dependencies on NetworkConnection.SendBytes and avoids expensive GetComponent calls that the Weaver previously generated.

This commit is contained in:
vis2k 2018-07-26 20:09:51 +02:00
parent 83927f27b0
commit 2b54665915
4 changed files with 36 additions and 33 deletions

View File

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

View File

@ -182,14 +182,28 @@ public override void Serialize(NetworkWriter writer)
}
}
/* These are not used directly but manually serialized, these are here for reference.
public struct SyncEventMessage
class SyncEventMessage : MessageBase
{
public NetworkId netId;
public int cmdHash;
public byte[] payload;
public NetworkInstanceId netId;
public int eventHash;
public byte[] payload; // the parameters for the Rpc function
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
eventHash = 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(eventHash);
writer.WriteBytesAndSize(payload);
}
}
/* These are not used directly but manually serialized, these are here for reference.
internal class SyncListMessage<T> where T: struct
{
public NetworkId netId;

View File

@ -150,7 +150,7 @@ public virtual bool InvokeRPC(int cmdHash, NetworkReader reader)
// ----------------------------- Sync Events --------------------------------
[EditorBrowsable(EditorBrowsableState.Never)]
protected void SendEventInternal(NetworkWriter writer, int channelId, string eventName)
protected void SendEventInternal(int eventHash, NetworkWriter writer, int channelId, string eventName)
{
if (!NetworkServer.active)
{
@ -158,8 +158,13 @@ protected void SendEventInternal(NetworkWriter writer, int channelId, string eve
return;
}
writer.FinishMessage();
NetworkServer.SendBytesToReady(gameObject, writer.ToArray(), channelId);
// construct the message
SyncEventMessage message = new SyncEventMessage();
message.netId = netId;
message.eventHash = eventHash;
message.payload = writer.ToArray();
NetworkServer.SendByChannelToReady(gameObject, (short)MsgType.SyncEvent, message, channelId);
#if UNITY_EDITOR
UnityEditor.NetworkDetailStats.IncrementStat(

View File

@ -1826,29 +1826,13 @@ MethodDefinition ProcessEventCall(EventDefinition ed, CustomAttribute ca)
WriteCreateWriter(evtWorker);
WriteMessageSize(evtWorker);
WriteMessageId(evtWorker, 7); //UNetwork.SYSTEM_SYNCEVENT
// create the command id constant
// create the syncevent id constant
FieldDefinition evtConstant = new FieldDefinition("kEvent" + ed.Name,
FieldAttributes.Static | FieldAttributes.Private,
Weaver.int32Type);
m_td.Fields.Add(evtConstant);
// write command constant
evtWorker.Append(evtWorker.Create(OpCodes.Ldloc_0)); // networkWriter
evtWorker.Append(evtWorker.Create(OpCodes.Ldsfld, evtConstant));
evtWorker.Append(evtWorker.Create(OpCodes.Callvirt, Weaver.NetworkWriterWritePacked32));
// write this.unetView.netId
evtWorker.Append(evtWorker.Create(OpCodes.Ldloc_0)); // networkWriter
evtWorker.Append(evtWorker.Create(OpCodes.Ldarg_0)); // this
evtWorker.Append(evtWorker.Create(OpCodes.Call, Weaver.getComponentReference)); // unetView
// load and write netId field
evtWorker.Append(evtWorker.Create(OpCodes.Callvirt, Weaver.getUNetIdReference)); // netId
evtWorker.Append(evtWorker.Create(OpCodes.Callvirt, Weaver.NetworkWriterWriteNetworkInstanceId)); // networkWriter.Write(this.unetView.netId)
// write all the arguments that the user passed to the syncevent
if (!WriteArguments(evtWorker, invoke.Resolve(), "SyncEvent", false))
return null;
@ -1864,6 +1848,7 @@ MethodDefinition ProcessEventCall(EventDefinition ed, CustomAttribute ca)
// invoke interal send and return
evtWorker.Append(evtWorker.Create(OpCodes.Ldarg_0)); // this
evtWorker.Append(evtWorker.Create(OpCodes.Ldsfld, evtConstant)); // eventHash
evtWorker.Append(evtWorker.Create(OpCodes.Ldloc_0)); // writer
evtWorker.Append(evtWorker.Create(OpCodes.Ldc_I4, channel)); // QoS transport channel (reliable/unreliable)
evtWorker.Append(evtWorker.Create(OpCodes.Ldstr, ed.Name));