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.8.0] - 2023-12-12 ### Added - Added a new RPC attribute, which is simply `Rpc`. (#2762) - This is a generic attribute that can perform the functions of both Server and Client RPCs, as well as enabling client-to-client RPCs. Includes several default targets: `Server`, `NotServer`, `Owner`, `NotOwner`, `Me`, `NotMe`, `ClientsAndHost`, and `Everyone`. Runtime overrides are available for any of these targets, as well as for sending to a specific ID or groups of IDs. - This attribute also includes the ability to defer RPCs that are sent to the local process to the start of the next frame instead of executing them immediately, treating them as if they had gone across the network. The default behavior is to execute immediately. - This attribute effectively replaces `ServerRpc` and `ClientRpc`. `ServerRpc` and `ClientRpc` remain in their existing forms for backward compatibility, but `Rpc` will be the recommended and most supported option. - Added `NetworkManager.OnConnectionEvent` as a unified connection event callback to notify clients and servers of all client connections and disconnections within the session (#2762) - Added `NetworkManager.ServerIsHost` and `NetworkBehaviour.ServerIsHost` to allow a client to tell if it is connected to a host or to a dedicated server (#2762) - Added `SceneEventProgress.SceneManagementNotEnabled` return status to be returned when a `NetworkSceneManager` method is invoked and scene management is not enabled. (#2735) - Added `SceneEventProgress.ServerOnlyAction` return status to be returned when a `NetworkSceneManager` method is invoked by a client. (#2735) - Added `NetworkObject.InstantiateAndSpawn` and `NetworkSpawnManager.InstantiateAndSpawn` methods to simplify prefab spawning by assuring that the prefab is valid and applies any override prior to instantiating the `GameObject` and spawning the `NetworkObject` instance. (#2710) ### Fixed - Fixed issue where a client disconnected by a server-host would not receive a local notification. (#2789) - Fixed issue where a server-host could shutdown during a relay connection but periodically the transport disconnect message sent to any connected clients could be dropped. (#2789) - Fixed issue where a host could disconnect its local client but remain running as a server. (#2789) - Fixed issue where `OnClientDisconnectedCallback` was not being invoked under certain conditions. (#2789) - Fixed issue where `OnClientDisconnectedCallback` was always returning 0 as the client identifier. (#2789) - Fixed issue where if a host or server shutdown while a client owned NetworkObjects (other than the player) it would throw an exception. (#2789) - Fixed issue where setting values on a `NetworkVariable` or `NetworkList` within `OnNetworkDespawn` during a shutdown sequence would throw an exception. (#2789) - Fixed issue where a teleport state could potentially be overridden by a previous unreliable delta state. (#2777) - Fixed issue where `NetworkTransform` was using the `NetworkManager.ServerTime.Tick` as opposed to `NetworkManager.NetworkTickSystem.ServerTime.Tick` during the authoritative side's tick update where it performed a delta state check. (#2777) - Fixed issue where a parented in-scene placed NetworkObject would be destroyed upon a client or server exiting a network session but not unloading the original scene in which the NetworkObject was placed. (#2737) - Fixed issue where during client synchronization and scene loading, when client synchronization or the scene loading mode are set to `LoadSceneMode.Single`, a `CreateObjectMessage` could be received, processed, and the resultant spawned `NetworkObject` could be instantiated in the client's currently active scene that could, towards the end of the client synchronization or loading process, be unloaded and cause the newly created `NetworkObject` to be destroyed (and throw and exception). (#2735) - Fixed issue where a `NetworkTransform` instance with interpolation enabled would result in wide visual motion gaps (stuttering) under above normal latency conditions and a 1-5% or higher packet are drop rate. (#2713) - Fixed issue where you could not have multiple source network prefab overrides targeting the same network prefab as their override. (#2710) ### Changed - Changed the server or host shutdown so it will now perform a "soft shutdown" when `NetworkManager.Shutdown` is invoked. This will send a disconnect notification to all connected clients and the server-host will wait for all connected clients to disconnect or timeout after a 5 second period before completing the shutdown process. (#2789) - Changed `OnClientDisconnectedCallback` will now return the assigned client identifier on the local client side if the client was approved and assigned one prior to being disconnected. (#2789) - Changed `NetworkTransform.SetState` (and related methods) now are cumulative during a fractional tick period and sent on the next pending tick. (#2777) - `NetworkManager.ConnectedClientsIds` is now accessible on the client side and will contain the list of all clients in the session, including the host client if the server is operating in host mode (#2762) - Changed `NetworkSceneManager` to return a `SceneEventProgress` status and not throw exceptions for methods invoked when scene management is disabled and when a client attempts to access a `NetworkSceneManager` method by a client. (#2735) - Changed `NetworkTransform` authoritative instance tick registration so a single `NetworkTransform` specific tick event update will update all authoritative instances to improve perofmance. (#2713) - Changed `NetworkPrefabs.OverrideToNetworkPrefab` dictionary is no longer used/populated due to it ending up being related to a regression bug and not allowing more than one override to be assigned to a network prefab asset. (#2710) - Changed in-scene placed `NetworkObject`s now store their source network prefab asset's `GlobalObjectIdHash` internally that is used, when scene management is disabled, by clients to spawn the correct prefab even if the `NetworkPrefab` entry has an override. This does not impact dynamically spawning the same prefab which will yield the override on both host and client. (#2710) - Changed in-scene placed `NetworkObject`s no longer require a `NetworkPrefab` entry with `GlobalObjectIdHash` override in order for clients to properly synchronize. (#2710) - Changed in-scene placed `NetworkObject`s now set their `IsSceneObject` value when generating their `GlobalObjectIdHash` value. (#2710) - Changed the default `NetworkConfig.SpawnTimeout` value from 1.0s to 10.0s. (#2710)
395 lines
14 KiB
C#
395 lines
14 KiB
C#
using System;
|
|
using NUnit.Framework;
|
|
using Unity.Collections;
|
|
using Unity.Netcode.Transports.UTP;
|
|
using Unity.Networking.Transport;
|
|
|
|
namespace Unity.Netcode.EditorTests
|
|
{
|
|
public class BatchedSendQueueTests
|
|
{
|
|
private const int k_TestQueueCapacity = 16 * 1024;
|
|
private const int k_TestMessageSize = 1020;
|
|
private const int k_NumMessagesToFillQueue = k_TestQueueCapacity / (k_TestMessageSize + BatchedSendQueue.PerMessageOverhead);
|
|
|
|
private ArraySegment<byte> m_TestMessage;
|
|
|
|
private void AssertIsTestMessage(NativeArray<byte> data)
|
|
{
|
|
var reader = new DataStreamReader(data);
|
|
Assert.AreEqual(k_TestMessageSize, reader.ReadInt());
|
|
for (int i = 0; i < k_TestMessageSize; i++)
|
|
{
|
|
Assert.AreEqual(m_TestMessage.Array[i], reader.ReadByte());
|
|
}
|
|
}
|
|
|
|
[OneTimeSetUp]
|
|
public void InitializeTestMessage()
|
|
{
|
|
var data = new byte[k_TestMessageSize];
|
|
for (int i = 0; i < k_TestMessageSize; i++)
|
|
{
|
|
data[i] = (byte)i;
|
|
}
|
|
m_TestMessage = new ArraySegment<byte>(data);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_EmptyOnCreation()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
|
|
Assert.AreEqual(0, q.Length);
|
|
Assert.True(q.IsEmpty);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_NotCreatedAfterDispose()
|
|
{
|
|
var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
q.Dispose();
|
|
Assert.False(q.IsCreated);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_InitialCapacityLessThanMaximum()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
Assert.AreEqual(q.Capacity, BatchedSendQueue.MinimumMinimumCapacity);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_PushMessage_ReturnValue()
|
|
{
|
|
// Will fit a single test message, but not two (with overhead included).
|
|
var queueCapacity = (k_TestMessageSize * 2) + BatchedSendQueue.PerMessageOverhead;
|
|
|
|
using var q = new BatchedSendQueue(queueCapacity);
|
|
|
|
Assert.True(q.PushMessage(m_TestMessage));
|
|
Assert.False(q.PushMessage(m_TestMessage));
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_PushMessage_IncreasesLength()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
Assert.AreEqual(k_TestMessageSize + BatchedSendQueue.PerMessageOverhead, q.Length);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_PushMessage_SucceedsAfterConsume()
|
|
{
|
|
var messageLength = k_TestMessageSize + BatchedSendQueue.PerMessageOverhead;
|
|
var queueCapacity = messageLength * 2;
|
|
|
|
using var q = new BatchedSendQueue(queueCapacity);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
q.Consume(messageLength);
|
|
Assert.IsTrue(q.PushMessage(m_TestMessage));
|
|
Assert.AreEqual(queueCapacity, q.Length);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_PushMessage_GrowsDataIfNeeded()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
var messageLength = k_TestMessageSize + BatchedSendQueue.PerMessageOverhead;
|
|
|
|
Assert.AreEqual(q.Capacity, BatchedSendQueue.MinimumMinimumCapacity);
|
|
|
|
var numMessagesToFillMinimum = BatchedSendQueue.MinimumMinimumCapacity / messageLength;
|
|
for (int i = 0; i < numMessagesToFillMinimum; i++)
|
|
{
|
|
q.PushMessage(m_TestMessage);
|
|
}
|
|
|
|
Assert.AreEqual(q.Capacity, BatchedSendQueue.MinimumMinimumCapacity);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
Assert.AreEqual(q.Capacity, BatchedSendQueue.MinimumMinimumCapacity * 2);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_PushMessage_DoesNotGrowDataPastMaximum()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
|
|
for (int i = 0; i < k_NumMessagesToFillQueue; i++)
|
|
{
|
|
Assert.IsTrue(q.PushMessage(m_TestMessage));
|
|
}
|
|
|
|
Assert.AreEqual(q.Capacity, k_TestQueueCapacity);
|
|
Assert.IsFalse(q.PushMessage(m_TestMessage));
|
|
Assert.AreEqual(q.Capacity, k_TestQueueCapacity);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_PushMessage_TrimsDataAfterGrowing()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
var messageLength = k_TestMessageSize + BatchedSendQueue.PerMessageOverhead;
|
|
|
|
for (int i = 0; i < k_NumMessagesToFillQueue; i++)
|
|
{
|
|
Assert.IsTrue(q.PushMessage(m_TestMessage));
|
|
}
|
|
|
|
Assert.AreEqual(q.Capacity, k_TestQueueCapacity);
|
|
q.Consume(messageLength * (k_NumMessagesToFillQueue - 1));
|
|
Assert.IsTrue(q.PushMessage(m_TestMessage));
|
|
Assert.AreEqual(messageLength * 2, q.Length);
|
|
Assert.AreEqual(q.Capacity, BatchedSendQueue.MinimumMinimumCapacity * 2);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithMessages_ReturnValue()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(k_TestQueueCapacity, Allocator.Temp);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
var filled = q.FillWriterWithMessages(ref writer);
|
|
Assert.AreEqual(k_TestMessageSize + BatchedSendQueue.PerMessageOverhead, filled);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithMessages_NoopIfNoPushedMessages()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(k_TestQueueCapacity, Allocator.Temp);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(0, q.FillWriterWithMessages(ref writer));
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithMessages_NoopIfNotEnoughCapacity()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(2, Allocator.Temp);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(0, q.FillWriterWithMessages(ref writer));
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithMessages_SinglePushedMessage()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(k_TestQueueCapacity, Allocator.Temp);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
q.FillWriterWithMessages(ref writer);
|
|
AssertIsTestMessage(data);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithMessages_MultiplePushedMessages()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(k_TestQueueCapacity, Allocator.Temp);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
q.FillWriterWithMessages(ref writer);
|
|
|
|
var messageLength = k_TestMessageSize + BatchedSendQueue.PerMessageOverhead;
|
|
AssertIsTestMessage(data);
|
|
AssertIsTestMessage(data.GetSubArray(messageLength, messageLength));
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithMessages_PartialPushedMessages()
|
|
{
|
|
var messageLength = k_TestMessageSize + BatchedSendQueue.PerMessageOverhead;
|
|
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(messageLength, Allocator.Temp);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(messageLength, q.FillWriterWithMessages(ref writer));
|
|
AssertIsTestMessage(data);
|
|
|
|
q.Consume(messageLength);
|
|
|
|
writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(messageLength, q.FillWriterWithMessages(ref writer));
|
|
AssertIsTestMessage(data);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithMessages_StopOnSoftMaxBytes()
|
|
{
|
|
var smallMessage = new ArraySegment<byte>(new byte[10]);
|
|
var largeMessage = new ArraySegment<byte>(new byte[3000]);
|
|
|
|
var smallMessageSize = smallMessage.Count + BatchedSendQueue.PerMessageOverhead;
|
|
var largeMessageSize = largeMessage.Count + BatchedSendQueue.PerMessageOverhead;
|
|
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(largeMessageSize, Allocator.Temp);
|
|
|
|
q.PushMessage(smallMessage);
|
|
q.PushMessage(largeMessage);
|
|
q.PushMessage(smallMessage);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(smallMessageSize, q.FillWriterWithMessages(ref writer, 1000));
|
|
q.Consume(smallMessageSize);
|
|
|
|
writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(largeMessageSize, q.FillWriterWithMessages(ref writer, 1000));
|
|
q.Consume(largeMessageSize);
|
|
|
|
writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(smallMessageSize, q.FillWriterWithMessages(ref writer, 1000));
|
|
q.Consume(smallMessageSize);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithBytes_NoopIfNoData()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(k_TestQueueCapacity, Allocator.Temp);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(0, q.FillWriterWithBytes(ref writer));
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithBytes_WriterCapacityMoreThanLength()
|
|
{
|
|
var dataLength = k_TestMessageSize + BatchedSendQueue.PerMessageOverhead;
|
|
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(k_TestQueueCapacity, Allocator.Temp);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(dataLength, q.FillWriterWithBytes(ref writer));
|
|
AssertIsTestMessage(data);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithBytes_WriterCapacityLessThanLength()
|
|
{
|
|
var dataLength = k_TestMessageSize + BatchedSendQueue.PerMessageOverhead;
|
|
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(dataLength, Allocator.Temp);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(dataLength, q.FillWriterWithBytes(ref writer));
|
|
AssertIsTestMessage(data);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithBytes_WriterCapacityEqualToLength()
|
|
{
|
|
var dataLength = k_TestMessageSize + BatchedSendQueue.PerMessageOverhead;
|
|
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(dataLength, Allocator.Temp);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(dataLength, q.FillWriterWithBytes(ref writer));
|
|
AssertIsTestMessage(data);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_FillWriterWithBytes_MaxBytesGreaterThanCapacity()
|
|
{
|
|
var dataLength = k_TestMessageSize + BatchedSendQueue.PerMessageOverhead;
|
|
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
using var data = new NativeArray<byte>(dataLength, Allocator.Temp);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
var writer = new DataStreamWriter(data);
|
|
Assert.AreEqual(dataLength, q.FillWriterWithBytes(ref writer, dataLength * 2));
|
|
AssertIsTestMessage(data);
|
|
Assert.False(writer.HasFailedWrites);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_Consume_LessThanLength()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
var messageLength = k_TestMessageSize + BatchedSendQueue.PerMessageOverhead;
|
|
q.Consume(messageLength);
|
|
Assert.AreEqual(messageLength, q.Length);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_Consume_ExactLength()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
q.Consume(k_TestMessageSize + BatchedSendQueue.PerMessageOverhead);
|
|
Assert.AreEqual(0, q.Length);
|
|
Assert.True(q.IsEmpty);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_Consume_MoreThanLength()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
|
|
q.PushMessage(m_TestMessage);
|
|
|
|
q.Consume(k_TestQueueCapacity);
|
|
Assert.AreEqual(0, q.Length);
|
|
Assert.True(q.IsEmpty);
|
|
}
|
|
|
|
[Test]
|
|
public void BatchedSendQueue_Consume_TrimsDataOnEmpty()
|
|
{
|
|
using var q = new BatchedSendQueue(k_TestQueueCapacity);
|
|
|
|
for (int i = 0; i < k_NumMessagesToFillQueue; i++)
|
|
{
|
|
q.PushMessage(m_TestMessage);
|
|
}
|
|
|
|
Assert.AreEqual(q.Capacity, k_TestQueueCapacity);
|
|
q.Consume(k_TestQueueCapacity);
|
|
Assert.AreEqual(q.Capacity, BatchedSendQueue.MinimumMinimumCapacity);
|
|
}
|
|
}
|
|
}
|