Skip to content
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2bbbf5d
Zombie Toys - Adjustments
NoelStephensUnity Dec 27, 2020
87bd6d9
More changes
NoelStephensUnity Dec 28, 2020
4acce29
Minor updates
NoelStephensUnity Dec 30, 2020
16cce8b
MTT-174
NoelStephensUnity Jan 5, 2021
548280b
Merge branch 'develop' into feature/networkupdate
NoelStephensUnity Jan 5, 2021
755009c
WIP
NoelStephensUnity Jan 5, 2021
39f0502
WIP
NoelStephensUnity Jan 7, 2021
cadac6a
MTT-327
NoelStephensUnity Jan 7, 2021
d130e99
Merge branch 'develop' into feature/rpcqueuerevisited
NoelStephensUnity Jan 7, 2021
00eda77
MTT-367 MTT-327
NoelStephensUnity Jan 8, 2021
e1dc42f
Minor update
NoelStephensUnity Jan 8, 2021
5d75987
Merge branch 'feature/rpcqueuerevisited' into feature/networkupdate
NoelStephensUnity Jan 8, 2021
9a09588
Revert "Merge branch 'feature/rpcqueuerevisited' into feature/network…
NoelStephensUnity Jan 8, 2021
7b10b5e
Revert "Revert "Merge branch 'feature/rpcqueuerevisited' into feature…
NoelStephensUnity Jan 8, 2021
4e99f6f
Develop Merge Adjustments
NoelStephensUnity Jan 8, 2021
95bd375
Cluster-Merge
NoelStephensUnity Jan 8, 2021
a0032b5
Merge Fix - Batching
NoelStephensUnity Jan 9, 2021
4fb8d9b
Fixes for Batching
NoelStephensUnity Jan 11, 2021
fafc49e
feat: network game update loop modular registration MTT-254
NoelStephensUnity Jan 12, 2021
c95547f
Merge branch 'develop' into feature/networkupdate
NoelStephensUnity Jan 12, 2021
aa9bfa3
fix: updated legacy name from rpcQueueMananger to rpcQueueContainer
NoelStephensUnity Jan 12, 2021
451461b
fix: removed line between properties
NoelStephensUnity Jan 12, 2021
dda716b
refactor: Updated for BitReader BitWriter bool
NoelStephensUnity Jan 13, 2021
e103a95
refactor: Fixes and Standards
NoelStephensUnity Jan 13, 2021
b23eabb
Merge branch 'develop' into feature/networkupdate
NoelStephensUnity Jan 13, 2021
a80ea9c
refactor: develop branch merge
NoelStephensUnity Jan 13, 2021
ba66825
fix: Using different writers per SendStream, to fix issue with more t…
jeffreyrainy Jan 14, 2021
2bb33a8
refactor: Better divide by 8 option
NoelStephensUnity Jan 14, 2021
d6211ac
refactor: Review comment related changes.
NoelStephensUnity Jan 14, 2021
6052a47
refactor: removing USINGUTP
NoelStephensUnity Jan 14, 2021
c4d7c9d
refactor: moved rpc queue files
NoelStephensUnity Jan 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions com.unity.multiplayer.mlapi/Runtime/Core/MessageBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MLAPI.Serialization;
using MLAPI.Configuration;
using MLAPI.Profiling;
using MLAPI.Messaging;
using System.Collections.Generic;
using System.Linq;
using System;
Expand All @@ -15,12 +16,17 @@ public class SendStream
{
public string channel;
public PooledBitStream Stream = PooledBitStream.Get();
public PooledBitWriter Writer;
public bool Empty = true;
Comment thread
NoelStephensUnity marked this conversation as resolved.

public SendStream()
{
Writer = PooledBitWriter.Get(Stream);
}
}

// Stores the stream of batched RPC to send to each client, by ClientId
private Dictionary<ulong, SendStream> SendDict = new Dictionary<ulong, SendStream>();
private PooledBitWriter Writer = new PooledBitWriter(PooledBitStream.Get());
Comment thread
NoelStephensUnity marked this conversation as resolved.

// Used to store targets, internally
private ulong[] TargetList = new ulong[0];
Expand Down Expand Up @@ -115,37 +121,35 @@ public void QueueItem(in FrameQueueItem item)
{
SendDict[clientId].Empty = false;
SendDict[clientId].channel = item.channel;
Writer.SetStream(SendDict[clientId].Stream);

Writer.WriteBit(false); // Encrypted
Writer.WriteBit(false); // Authenticated
SendDict[clientId].Writer.WriteBit(false); // Encrypted
SendDict[clientId].Writer.WriteBit(false); // Authenticated

switch (item.queueItemType)
{
// 6 bits are used for the message type, which is an MLAPIConstants
case RpcQueueContainer.QueueItemType.ServerRpc:
Writer.WriteBits(MLAPIConstants.MLAPI_SERVER_RPC, 6); // MessageType
SendDict[clientId].Writer.WriteBits(MLAPIConstants.MLAPI_SERVER_RPC, 6); // MessageType
break;
case RpcQueueContainer.QueueItemType.ClientRpc:
Writer.WriteBits(MLAPIConstants.MLAPI_CLIENT_RPC, 6); // MessageType
SendDict[clientId].Writer.WriteBits(MLAPIConstants.MLAPI_CLIENT_RPC, 6); // MessageType
break;
}
}

// write the amounts of bytes that are coming up
PushLength(item.messageData.Count, ref Writer);
PushLength(item.messageData.Count, ref SendDict[clientId].Writer);

// write the message to send
// todo: is there a faster alternative to .ToArray()
Writer.WriteBytes(item.messageData.ToArray(), item.messageData.Count);
SendDict[clientId].Writer.WriteBytes(item.messageData.Array, item.messageData.Count, item.messageData.Offset);

ProfilerStatManager.bytesSent.Record((int)item.messageData.Count);
ProfilerStatManager.rpcsSent.Record();
}
}

public delegate void SendCallbackType(ulong clientId, SendStream messageStream);
public delegate void ReceiveCallbackType(BitStream messageStream, MLAPI.RpcQueueContainer.QueueItemType messageType, ulong clientId, float time);
public delegate void ReceiveCallbackType(BitStream messageStream, RpcQueueContainer.QueueItemType messageType, ulong clientId, float time);

/// <summary>
/// SendItems
Expand All @@ -165,17 +169,17 @@ public void SendItems(int threshold, SendCallbackType sendCallback)
if (length >= threshold)
{
sendCallback(entry.Key, entry.Value);
ProfilerStatManager.rpcBatchesSent.Record();

// clear the batch that was sent from the SendDict
entry.Value.Stream.SetLength(0);
entry.Value.Stream.Position = 0;
entry.Value.Empty = true;
ProfilerStatManager.rpcBatchesSent.Record();
}
}
}
}


/// <summary>
/// ReceiveItems
/// Process the messageStream and call the callback with individual RPC messages
Expand All @@ -185,7 +189,7 @@ public void SendItems(int threshold, SendCallbackType sendCallback)
/// <param name="messageType"> the message type to pass back to callback</param>
/// <param name="clientId"> the clientId to pass back to callback</param>
/// <param name="receiveTime"> the packet receive time to pass back to callback</param>
public int ReceiveItems(in BitStream messageStream, ReceiveCallbackType receiveCallback, MLAPI.RpcQueueContainer.QueueItemType messageType, ulong clientId, float receiveTime)
public int ReceiveItems(in BitStream messageStream, ReceiveCallbackType receiveCallback, RpcQueueContainer.QueueItemType messageType, ulong clientId, float receiveTime)
{
using PooledBitStream copy = PooledBitStream.Get();
Comment thread
NoelStephensUnity marked this conversation as resolved.
Outdated
do
Expand All @@ -195,6 +199,7 @@ public int ReceiveItems(in BitStream messageStream, ReceiveCallbackType receiveC

if (rpcSize < 0)
{
copy.Dispose();
Comment thread
NoelStephensUnity marked this conversation as resolved.
// abort if there's an error reading lengths
return 0;
}
Expand All @@ -211,6 +216,7 @@ public int ReceiveItems(in BitStream messageStream, ReceiveCallbackType receiveC
// RPCReceiveQueueItem peeks at content, it doesn't advance
messageStream.Seek(rpcSize, SeekOrigin.Current);
} while (messageStream.Position < messageStream.Length);
copy.Dispose();
return 0;
}
}
Expand Down
129 changes: 129 additions & 0 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkUpdateLoopSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System;
using UnityEngine;


namespace MLAPI
{
/// <summary>
/// NetworkUpdateLoopBehaviour
/// Derive from this class if you need to register a NetworkedBehaviour based class
/// </summary>
public class NetworkUpdateLoopBehaviour:NetworkedBehaviour,INetworkUpdateLoopSystem
Comment thread
NoelStephensUnity marked this conversation as resolved.
Outdated
{
protected virtual Action InternalRegisterNetworkUpdateStage(NetworkUpdateManager.NetworkUpdateStages stage )
{
return null;
}

public Action RegisterUpdate(NetworkUpdateManager.NetworkUpdateStages stage )
{
return InternalRegisterNetworkUpdateStage(stage);
}

protected void RegisterUpdateLoopSystem()
{
NetworkUpdateManager.NetworkLoopRegistration(this);
}

protected void OnNetworkLoopSystemRemove()
{
if(onNetworkLoopSystemDestroyed != null)
{
onNetworkLoopSystemDestroyed.Invoke(this);
}
}

Action<INetworkUpdateLoopSystem> onNetworkLoopSystemDestroyed;
Comment thread
NoelStephensUnity marked this conversation as resolved.
Outdated

public void RegisterUpdateLoopSystemDestroyCallback(Action<INetworkUpdateLoopSystem> networkLoopSystemDestroyedCallback)
{
onNetworkLoopSystemDestroyed = networkLoopSystemDestroyedCallback;
}
}

/// <summary>
/// UpdateLoopBehaviour
/// Derive from this class if you only require MonoBehaviour functionality
/// </summary>
public class UpdateLoopBehaviour:MonoBehaviour,INetworkUpdateLoopSystem
{
protected virtual Action InternalRegisterNetworkUpdateStage(NetworkUpdateManager.NetworkUpdateStages stage )
{
return null;
}

public Action RegisterUpdate(NetworkUpdateManager.NetworkUpdateStages stage )
{
return InternalRegisterNetworkUpdateStage(stage);
}

protected void RegisterUpdateLoopSystem()
{
NetworkUpdateManager.NetworkLoopRegistration(this);
}

protected void OnNetworkLoopSystemRemove()
{
if(onNetworkLoopSystemDestroyed != null)
{
onNetworkLoopSystemDestroyed.Invoke(this);
}
}

Action<INetworkUpdateLoopSystem> onNetworkLoopSystemDestroyed;
Comment thread
NoelStephensUnity marked this conversation as resolved.
Outdated

public void RegisterUpdateLoopSystemDestroyCallback(Action<INetworkUpdateLoopSystem> networkLoopSystemDestroyedCallback)
{
onNetworkLoopSystemDestroyed = networkLoopSystemDestroyedCallback;
}
}

/// <summary>
/// GenericUpdateLoopSystem
/// Derive from this class for generic (non-MonoBehaviour) classes
/// </summary>
public class GenericUpdateLoopSystem:INetworkUpdateLoopSystem
{
protected virtual Action InternalRegisterNetworkUpdateStage(NetworkUpdateManager.NetworkUpdateStages stage )
{
return null;
}

public Action RegisterUpdate(NetworkUpdateManager.NetworkUpdateStages stage )
{
return InternalRegisterNetworkUpdateStage(stage);
}

protected void RegisterUpdateLoopSystem()
{
NetworkUpdateManager.NetworkLoopRegistration(this);
}

protected void OnNetworkLoopSystemRemove()
{
if(onNetworkLoopSystemDestroyed != null)
{
onNetworkLoopSystemDestroyed.Invoke(this);
}
}

Action<INetworkUpdateLoopSystem> onNetworkLoopSystemDestroyed;
Comment thread
NoelStephensUnity marked this conversation as resolved.
Outdated

public void RegisterUpdateLoopSystemDestroyCallback(Action<INetworkUpdateLoopSystem> networkLoopSystemDestroyedCallback)
{
onNetworkLoopSystemDestroyed = networkLoopSystemDestroyedCallback;
}
}


/// <summary>
/// INetworkUpdateLoopSystem
/// Use this interface if you need a custom class beyond the scope of GenericUpdateLoopSystem, UpdateLoopBehaviour, and NetworkUpdateLoopBehaviour
/// </summary>
public interface INetworkUpdateLoopSystem
{
Action RegisterUpdate(NetworkUpdateManager.NetworkUpdateStages stage );

void RegisterUpdateLoopSystemDestroyCallback(Action<INetworkUpdateLoopSystem> networkLoopSystemDestroyedCallbsack);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading