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.0.0-pre.6] - 2022-03-02 ### Added - NetworkAnimator now properly synchrhonizes all animation layers as well as runtime-adjusted weighting between them (#1765) - Added first set of tests for NetworkAnimator - parameter syncing, trigger set / reset, override network animator (#1735) ### Changed ### Fixed - Fixed an issue where sometimes the first client to connect to the server could see messages from the server as coming from itself. (#1683) - Fixed an issue where clients seemed to be able to send messages to ClientId 1, but these messages would actually still go to the server (id 0) instead of that client. (#1683) - Improved clarity of error messaging when a client attempts to send a message to a destination other than the server, which isn't allowed. (#1683) - Disallowed async keyword in RPCs (#1681) - Fixed an issue where Alpha release versions of Unity (version 2022.2.0a5 and later) will not compile due to the UNet Transport no longer existing (#1678) - Fixed messages larger than 64k being written with incorrectly truncated message size in header (#1686) (credit: @kaen) - Fixed overloading RPC methods causing collisions and failing on IL2CPP targets. (#1694) - Fixed spawn flow to propagate `IsSceneObject` down to children NetworkObjects, decouple implicit relationship between object spawning & `IsSceneObject` flag (#1685) - Fixed error when serializing ConnectionApprovalMessage with scene management disabled when one or more objects is hidden via the CheckObjectVisibility delegate (#1720) - Fixed CheckObjectVisibility delegate not being properly invoked for connecting clients when Scene Management is enabled. (#1680) - Fixed NetworkList to properly call INetworkSerializable's NetworkSerialize() method (#1682) - Fixed NetworkVariables containing more than 1300 bytes of data (such as large NetworkLists) no longer cause an OverflowException (the limit on data size is now whatever limit the chosen transport imposes on fragmented NetworkDelivery mechanisms) (#1725) - Fixed ServerRpcParams and ClientRpcParams must be the last parameter of an RPC in order to function properly. Added a compile-time check to ensure this is the case and trigger an error if they're placed elsewhere (#1721) - Fixed FastBufferReader being created with a length of 1 if provided an input of length 0 (#1724) - Fixed The NetworkConfig's checksum hash includes the NetworkTick so that clients with a different tickrate than the server are identified and not allowed to connect (#1728) - Fixed OwnedObjects not being properly modified when using ChangeOwnership (#1731) - Improved performance in NetworkAnimator (#1735) - Removed the "always sync" network animator (aka "autosend") parameters (#1746)
288 lines
10 KiB
C#
288 lines
10 KiB
C#
#if UNITY_UNET_PRESENT
|
|
#pragma warning disable 618 // disable is obsolete
|
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
|
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace Unity.Netcode.Transports.UNET
|
|
{
|
|
public class UNetTransport : NetworkTransport
|
|
{
|
|
public enum SendMode
|
|
{
|
|
Immediately,
|
|
Queued
|
|
}
|
|
|
|
private int m_UnreliableChannelId;
|
|
private int m_UnreliableSequencedChannelId;
|
|
private int m_ReliableChannelId;
|
|
private int m_ReliableSequencedChannelId;
|
|
private int m_ReliableFragmentedSequencedChannelId;
|
|
|
|
// Inspector / settings
|
|
public int MessageBufferSize = 1024 * 5;
|
|
public int MaxConnections = 100;
|
|
public int MaxSentMessageQueueSize = 128;
|
|
|
|
public string ConnectAddress = "127.0.0.1";
|
|
public int ConnectPort = 7777;
|
|
public int ServerListenPort = 7777;
|
|
|
|
public SendMode MessageSendMode = SendMode.Immediately;
|
|
|
|
// Runtime / state
|
|
private byte[] m_MessageBuffer;
|
|
private WeakReference m_TemporaryBufferReference;
|
|
|
|
// Lookup / translation
|
|
private int m_ServerConnectionId;
|
|
private int m_ServerHostId;
|
|
|
|
public override ulong ServerClientId => GetNetcodeClientId(0, 0, true);
|
|
|
|
internal NetworkManager NetworkManager;
|
|
|
|
protected void LateUpdate()
|
|
{
|
|
if (UnityEngine.Networking.NetworkTransport.IsStarted && MessageSendMode == SendMode.Queued)
|
|
{
|
|
#if UNITY_WEBGL
|
|
Debug.LogError("Cannot use queued sending mode for WebGL");
|
|
#else
|
|
if (NetworkManager != null && NetworkManager.IsServer)
|
|
{
|
|
foreach (var targetClient in NetworkManager.Singleton.ConnectedClientsList)
|
|
{
|
|
SendQueued(targetClient.ClientId);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SendQueued(NetworkManager.Singleton.LocalClientId);
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
|
|
public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDelivery networkDelivery)
|
|
{
|
|
GetUNetConnectionDetails(clientId, out byte hostId, out ushort connectionId);
|
|
|
|
byte[] buffer;
|
|
if (payload.Offset > 0)
|
|
{
|
|
// UNET cant handle this, do a copy
|
|
|
|
if (m_MessageBuffer.Length >= payload.Count)
|
|
{
|
|
buffer = m_MessageBuffer;
|
|
}
|
|
else
|
|
{
|
|
object bufferRef;
|
|
if (m_TemporaryBufferReference != null && ((bufferRef = m_TemporaryBufferReference.Target) != null) && ((byte[])bufferRef).Length >= payload.Count)
|
|
{
|
|
buffer = (byte[])bufferRef;
|
|
}
|
|
else
|
|
{
|
|
buffer = new byte[payload.Count];
|
|
m_TemporaryBufferReference = new WeakReference(buffer);
|
|
}
|
|
}
|
|
|
|
Buffer.BlockCopy(payload.Array, payload.Offset, buffer, 0, payload.Count);
|
|
}
|
|
else
|
|
{
|
|
buffer = payload.Array;
|
|
}
|
|
|
|
int channelId = -1;
|
|
switch (networkDelivery)
|
|
{
|
|
case NetworkDelivery.Unreliable:
|
|
channelId = m_UnreliableChannelId;
|
|
break;
|
|
case NetworkDelivery.UnreliableSequenced:
|
|
channelId = m_UnreliableSequencedChannelId;
|
|
break;
|
|
case NetworkDelivery.Reliable:
|
|
channelId = m_ReliableChannelId;
|
|
break;
|
|
case NetworkDelivery.ReliableSequenced:
|
|
channelId = m_ReliableSequencedChannelId;
|
|
break;
|
|
case NetworkDelivery.ReliableFragmentedSequenced:
|
|
channelId = m_ReliableFragmentedSequencedChannelId;
|
|
break;
|
|
}
|
|
|
|
if (MessageSendMode == SendMode.Queued)
|
|
{
|
|
#if UNITY_WEBGL
|
|
Debug.LogError("Cannot use queued sending mode for WebGL");
|
|
#else
|
|
UnityEngine.Networking.NetworkTransport.QueueMessageForSending(hostId, connectionId, channelId, buffer, payload.Count, out byte error);
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
UnityEngine.Networking.NetworkTransport.Send(hostId, connectionId, channelId, buffer, payload.Count, out byte error);
|
|
}
|
|
}
|
|
|
|
#if !UNITY_WEBGL
|
|
private void SendQueued(ulong clientId)
|
|
{
|
|
GetUNetConnectionDetails(clientId, out byte hostId, out ushort connectionId);
|
|
|
|
UnityEngine.Networking.NetworkTransport.SendQueuedMessages(hostId, connectionId, out _);
|
|
}
|
|
#endif
|
|
|
|
public override NetworkEvent PollEvent(out ulong clientId, out ArraySegment<byte> payload, out float receiveTime)
|
|
{
|
|
var eventType = UnityEngine.Networking.NetworkTransport.Receive(out int hostId, out int connectionId, out _, m_MessageBuffer, m_MessageBuffer.Length, out int receivedSize, out byte error);
|
|
|
|
clientId = GetNetcodeClientId((byte)hostId, (ushort)connectionId, false);
|
|
receiveTime = Time.realtimeSinceStartup;
|
|
|
|
var networkError = (NetworkError)error;
|
|
if (networkError == NetworkError.MessageToLong)
|
|
{
|
|
byte[] tempBuffer;
|
|
|
|
if (m_TemporaryBufferReference != null && m_TemporaryBufferReference.IsAlive && ((byte[])m_TemporaryBufferReference.Target).Length >= receivedSize)
|
|
{
|
|
tempBuffer = (byte[])m_TemporaryBufferReference.Target;
|
|
}
|
|
else
|
|
{
|
|
tempBuffer = new byte[receivedSize];
|
|
m_TemporaryBufferReference = new WeakReference(tempBuffer);
|
|
}
|
|
|
|
eventType = UnityEngine.Networking.NetworkTransport.Receive(out hostId, out connectionId, out _, tempBuffer, tempBuffer.Length, out receivedSize, out error);
|
|
payload = new ArraySegment<byte>(tempBuffer, 0, receivedSize);
|
|
}
|
|
else
|
|
{
|
|
payload = new ArraySegment<byte>(m_MessageBuffer, 0, receivedSize);
|
|
}
|
|
|
|
if (networkError == NetworkError.Timeout)
|
|
{
|
|
// In UNET. Timeouts are not disconnects. We have to translate that here.
|
|
eventType = NetworkEventType.DisconnectEvent;
|
|
}
|
|
|
|
// Translate NetworkEventType to NetEventType
|
|
switch (eventType)
|
|
{
|
|
case NetworkEventType.DataEvent:
|
|
return NetworkEvent.Data;
|
|
case NetworkEventType.ConnectEvent:
|
|
return NetworkEvent.Connect;
|
|
case NetworkEventType.DisconnectEvent:
|
|
return NetworkEvent.Disconnect;
|
|
case NetworkEventType.BroadcastEvent:
|
|
case NetworkEventType.Nothing:
|
|
default:
|
|
return NetworkEvent.Nothing;
|
|
}
|
|
}
|
|
|
|
public override bool StartClient()
|
|
{
|
|
m_ServerHostId = UnityEngine.Networking.NetworkTransport.AddHost(new HostTopology(GetConfig(), 1), 0, null);
|
|
m_ServerConnectionId = UnityEngine.Networking.NetworkTransport.Connect(m_ServerHostId, ConnectAddress, ConnectPort, 0, out byte error);
|
|
return (NetworkError)error == NetworkError.Ok;
|
|
}
|
|
|
|
public override bool StartServer()
|
|
{
|
|
var topology = new HostTopology(GetConfig(), MaxConnections);
|
|
UnityEngine.Networking.NetworkTransport.AddHost(topology, ServerListenPort, null);
|
|
return true;
|
|
}
|
|
|
|
public override void DisconnectRemoteClient(ulong clientId)
|
|
{
|
|
GetUNetConnectionDetails(clientId, out byte hostId, out ushort connectionId);
|
|
|
|
UnityEngine.Networking.NetworkTransport.Disconnect((int)hostId, (int)connectionId, out byte error);
|
|
}
|
|
|
|
public override void DisconnectLocalClient()
|
|
{
|
|
UnityEngine.Networking.NetworkTransport.Disconnect(m_ServerHostId, m_ServerConnectionId, out byte error);
|
|
}
|
|
|
|
public override ulong GetCurrentRtt(ulong clientId)
|
|
{
|
|
GetUNetConnectionDetails(clientId, out byte hostId, out ushort connectionId);
|
|
|
|
return (ulong)UnityEngine.Networking.NetworkTransport.GetCurrentRTT((int)hostId, (int)connectionId, out byte error);
|
|
}
|
|
|
|
public override void Shutdown()
|
|
{
|
|
UnityEngine.Networking.NetworkTransport.Shutdown();
|
|
}
|
|
|
|
public override void Initialize(NetworkManager networkManager = null)
|
|
{
|
|
NetworkManager = networkManager;
|
|
|
|
m_MessageBuffer = new byte[MessageBufferSize];
|
|
|
|
UnityEngine.Networking.NetworkTransport.Init();
|
|
}
|
|
|
|
private ulong GetNetcodeClientId(byte hostId, ushort connectionId, bool isServer)
|
|
{
|
|
if (isServer)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return (connectionId | (ulong)hostId << 16) + 1;
|
|
}
|
|
|
|
private void GetUNetConnectionDetails(ulong clientId, out byte hostId, out ushort connectionId)
|
|
{
|
|
if (clientId == 0)
|
|
{
|
|
hostId = (byte)m_ServerHostId;
|
|
connectionId = (ushort)m_ServerConnectionId;
|
|
}
|
|
else
|
|
{
|
|
hostId = (byte)((clientId - 1) >> 16);
|
|
connectionId = (ushort)((clientId - 1));
|
|
}
|
|
}
|
|
|
|
private ConnectionConfig GetConfig()
|
|
{
|
|
var connectionConfig = new ConnectionConfig();
|
|
|
|
m_UnreliableChannelId = connectionConfig.AddChannel(QosType.Unreliable);
|
|
m_UnreliableSequencedChannelId = connectionConfig.AddChannel(QosType.UnreliableSequenced);
|
|
m_ReliableChannelId = connectionConfig.AddChannel(QosType.Reliable);
|
|
m_ReliableSequencedChannelId = connectionConfig.AddChannel(QosType.ReliableSequenced);
|
|
m_ReliableFragmentedSequencedChannelId = connectionConfig.AddChannel(QosType.ReliableFragmentedSequenced);
|
|
|
|
connectionConfig.MaxSentMessageQueueSize = (ushort)MaxSentMessageQueueSize;
|
|
|
|
return connectionConfig;
|
|
}
|
|
}
|
|
}
|
|
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
|
|
#pragma warning restore 618 // restore is obsolete
|
|
#endif
|