The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com). ## [1.5.1] - 2023-06-07 ### Added - Added support for serializing `NativeArray<>` and `NativeList<>` in `FastBufferReader`/`FastBufferWriter`, `BufferSerializer`, `NetworkVariable`, and RPCs. (To use `NativeList<>`, add `UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT` to your Scripting Define Symbols in `Project Settings > Player`) (#2375) - The location of the automatically-created default network prefab list can now be configured (#2544) - Added: Message size limits (max single message and max fragmented message) can now be set using NetworkManager.MaximumTransmissionUnitSize and NetworkManager.MaximumFragmentedMessageSize for transports that don't work with the default values (#2530) - Added `NetworkObject.SpawnWithObservers` property (default is true) that when set to false will spawn a `NetworkObject` with no observers and will not be spawned on any client until `NetworkObject.NetworkShow` is invoked. (#2568) ### Fixed - Fixed: Fixed a null reference in codegen in some projects (#2581) - Fixed issue where the `OnClientDisconnected` client identifier was incorrect after a pending client connection was denied. (#2569) - Fixed warning "Runtime Network Prefabs was not empty at initialization time." being erroneously logged when no runtime network prefabs had been added (#2565) - Fixed issue where some temporary debug console logging was left in a merged PR. (#2562) - Fixed the "Generate Default Network Prefabs List" setting not loading correctly and always reverting to being checked. (#2545) - Fixed issue where users could not use NetworkSceneManager.VerifySceneBeforeLoading to exclude runtime generated scenes from client synchronization. (#2550) - Fixed missing value on `NetworkListEvent` for `EventType.RemoveAt` events. (#2542,#2543) - Fixed issue where parenting a NetworkTransform under a transform with a scale other than Vector3.one would result in incorrect values on non-authoritative instances. (#2538) - Fixed issue where a server would include scene migrated and then despawned NetworkObjects to a client that was being synchronized. (#2532) - Fixed the inspector throwing exceptions when attempting to render `NetworkVariable`s of enum types. (#2529) - Making a `NetworkVariable` with an `INetworkSerializable` type that doesn't meet the `new()` constraint will now create a compile-time error instead of an editor crash (#2528) - Fixed Multiplayer Tools package installation docs page link on the NetworkManager popup. (#2526) - Fixed an exception and error logging when two different objects are shown and hidden on the same frame (#2524) - Fixed a memory leak in `UnityTransport` that occurred if `StartClient` failed. (#2518) - Fixed issue where a client could throw an exception if abruptly disconnected from a network session with one or more spawned `NetworkObject`(s). (#2510) - Fixed issue where invalid endpoint addresses were not being detected and returning false from NGO UnityTransport. (#2496) - Fixed some errors that could occur if a connection is lost and the loss is detected when attempting to write to the socket. (#2495) ## Changed - Adding network prefabs before NetworkManager initialization is now supported. (#2565) - Connecting clients being synchronized now switch to the server's active scene before spawning and synchronizing NetworkObjects. (#2532) - Updated `UnityTransport` dependency on `com.unity.transport` to 1.3.4. (#2533) - Improved performance of NetworkBehaviour initialization by replacing reflection when initializing NetworkVariables with compile-time code generation, which should help reduce hitching during additive scene loads. (#2522)
149 lines
6.1 KiB
C#
149 lines
6.1 KiB
C#
using System.Collections.Generic;
|
|
using Unity.Collections;
|
|
|
|
namespace Unity.Netcode
|
|
{
|
|
internal class DeferredMessageManager : IDeferredNetworkMessageManager
|
|
{
|
|
protected struct TriggerData
|
|
{
|
|
public FastBufferReader Reader;
|
|
public NetworkMessageHeader Header;
|
|
public ulong SenderId;
|
|
public float Timestamp;
|
|
public int SerializedHeaderSize;
|
|
}
|
|
protected struct TriggerInfo
|
|
{
|
|
public float Expiry;
|
|
public NativeList<TriggerData> TriggerData;
|
|
}
|
|
|
|
protected readonly Dictionary<IDeferredNetworkMessageManager.TriggerType, Dictionary<ulong, TriggerInfo>> m_Triggers = new Dictionary<IDeferredNetworkMessageManager.TriggerType, Dictionary<ulong, TriggerInfo>>();
|
|
|
|
private readonly NetworkManager m_NetworkManager;
|
|
|
|
internal DeferredMessageManager(NetworkManager networkManager)
|
|
{
|
|
m_NetworkManager = networkManager;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Defers processing of a message until the moment a specific networkObjectId is spawned.
|
|
/// This is to handle situations where an RPC or other object-specific message arrives before the spawn does,
|
|
/// either due to it being requested in OnNetworkSpawn before the spawn call has been executed
|
|
///
|
|
/// There is a one second maximum lifetime of triggers to avoid memory leaks. After one second has passed
|
|
/// without the requested object ID being spawned, the triggers for it are automatically deleted.
|
|
/// </summary>
|
|
public virtual unsafe void DeferMessage(IDeferredNetworkMessageManager.TriggerType trigger, ulong key, FastBufferReader reader, ref NetworkContext context)
|
|
{
|
|
if (!m_Triggers.TryGetValue(trigger, out var triggers))
|
|
{
|
|
triggers = new Dictionary<ulong, TriggerInfo>();
|
|
m_Triggers[trigger] = triggers;
|
|
}
|
|
|
|
if (!triggers.TryGetValue(key, out var triggerInfo))
|
|
{
|
|
triggerInfo = new TriggerInfo
|
|
{
|
|
Expiry = m_NetworkManager.RealTimeProvider.RealTimeSinceStartup + m_NetworkManager.NetworkConfig.SpawnTimeout,
|
|
TriggerData = new NativeList<TriggerData>(Allocator.Persistent)
|
|
};
|
|
triggers[key] = triggerInfo;
|
|
}
|
|
|
|
triggerInfo.TriggerData.Add(new TriggerData
|
|
{
|
|
Reader = new FastBufferReader(reader.GetUnsafePtr(), Allocator.Persistent, reader.Length),
|
|
Header = context.Header,
|
|
Timestamp = context.Timestamp,
|
|
SenderId = context.SenderId,
|
|
SerializedHeaderSize = context.SerializedHeaderSize
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cleans up any trigger that's existed for more than a second.
|
|
/// These triggers were probably for situations where a request was received after a despawn rather than before a spawn.
|
|
/// </summary>
|
|
public virtual unsafe void CleanupStaleTriggers()
|
|
{
|
|
foreach (var kvp in m_Triggers)
|
|
{
|
|
ulong* staleKeys = stackalloc ulong[kvp.Value.Count];
|
|
int index = 0;
|
|
foreach (var kvp2 in kvp.Value)
|
|
{
|
|
if (kvp2.Value.Expiry < m_NetworkManager.RealTimeProvider.RealTimeSinceStartup)
|
|
{
|
|
staleKeys[index++] = kvp2.Key;
|
|
PurgeTrigger(kvp.Key, kvp2.Key, kvp2.Value);
|
|
}
|
|
}
|
|
|
|
for (var i = 0; i < index; ++i)
|
|
{
|
|
kvp.Value.Remove(staleKeys[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected virtual void PurgeTrigger(IDeferredNetworkMessageManager.TriggerType triggerType, ulong key, TriggerInfo triggerInfo)
|
|
{
|
|
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
|
|
{
|
|
NetworkLog.LogWarning($"Deferred messages were received for a trigger of type {triggerType} with key {key}, but that trigger was not received within within {m_NetworkManager.NetworkConfig.SpawnTimeout} second(s).");
|
|
}
|
|
|
|
foreach (var data in triggerInfo.TriggerData)
|
|
{
|
|
data.Reader.Dispose();
|
|
}
|
|
|
|
triggerInfo.TriggerData.Dispose();
|
|
}
|
|
|
|
public virtual void ProcessTriggers(IDeferredNetworkMessageManager.TriggerType trigger, ulong key)
|
|
{
|
|
if (m_Triggers.TryGetValue(trigger, out var triggers))
|
|
{
|
|
// This must happen after InvokeBehaviourNetworkSpawn, otherwise ClientRPCs and other messages can be
|
|
// processed before the object is fully spawned. This must be the last thing done in the spawn process.
|
|
if (triggers.TryGetValue(key, out var triggerInfo))
|
|
{
|
|
foreach (var deferredMessage in triggerInfo.TriggerData)
|
|
{
|
|
// Reader will be disposed within HandleMessage
|
|
m_NetworkManager.ConnectionManager.MessageManager.HandleMessage(deferredMessage.Header, deferredMessage.Reader, deferredMessage.SenderId, deferredMessage.Timestamp, deferredMessage.SerializedHeaderSize);
|
|
}
|
|
|
|
triggerInfo.TriggerData.Dispose();
|
|
triggers.Remove(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cleans up any trigger that's existed for more than a second.
|
|
/// These triggers were probably for situations where a request was received after a despawn rather than before a spawn.
|
|
/// </summary>
|
|
public virtual void CleanupAllTriggers()
|
|
{
|
|
foreach (var kvp in m_Triggers)
|
|
{
|
|
foreach (var kvp2 in kvp.Value)
|
|
{
|
|
foreach (var data in kvp2.Value.TriggerData)
|
|
{
|
|
data.Reader.Dispose();
|
|
}
|
|
kvp2.Value.TriggerData.Dispose();
|
|
}
|
|
}
|
|
m_Triggers.Clear();
|
|
}
|
|
}
|
|
}
|