Skip to content

fix: corrected NetworkVariable WriteField/WriteDelta/ReadField/ReadDelta dropping the last byte if unaligned. #1008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ private void NetworkVariableUpdate(ulong clientId, int behaviourIndex)
else
{
NetworkVariableFields[k].WriteDelta(buffer);
buffer.PadBuffer();
}

if (!m_NetworkVariableIndexesToResetSet.Contains(k))
Expand Down Expand Up @@ -656,11 +657,10 @@ internal static void HandleNetworkVariableDeltas(List<INetworkVariable> networkV
PerformanceDataManager.Increment(ProfilerConstants.NetworkVarDeltas);

ProfilerStatManager.NetworkVarsRcvd.Record();
(stream as NetworkBuffer).SkipPadBits();

if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
{
(stream as NetworkBuffer).SkipPadBits();

if (stream.Position > (readStartPos + varSize))
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
Expand Down Expand Up @@ -816,6 +816,7 @@ internal static void WriteNetworkVariableData(List<INetworkVariable> networkVari
else
{
networkVariableList[j].WriteField(stream);
writer.WritePadBits();
Copy link
Contributor

@mattwalsh-unity mattwalsh-unity Aug 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Did we find a bug here? Or is there an encoding change? I'm actually using this PR to educate myself on where we write padding.

Copy link
Collaborator Author

@ShadauxCat ShadauxCat Aug 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the issue here was not what I initially thought... it was that NetworkList was writing 3 bits at the very end of the buffer when it wrote EventType.Clear, and when there's an unaligned write left at the end of the buffer, the Position value ends up being one byte too small to get those last 3 bits. This just makes sure the buffer is aligned after the write.

There's no specific case in WriteField that's doing this right now, but my figuring is, if the bug has happened in WriteDelta, we should future-proof WriteField against it, too... otherwise WriteField is a landmine waiting to explode some day.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like on our offline convo about just banishing bitwise writing (or having a wrapper which lets you bitwise write but then it pads it for you) to prevent more weird fragile cases like this and because we've learned from other netcode folks the wisdom of not being over-the-top-miserly on space. In the meantime, if this shores us up, great

}
}
}
Expand Down Expand Up @@ -855,6 +856,7 @@ internal static void SetNetworkVariableData(List<INetworkVariable> networkVariab
long readStartPos = stream.Position;

networkVariableList[j].ReadField(stream);
reader.SkipPadBits();

if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System.Collections;
using System.IO;
using NUnit.Framework;
using UnityEngine.TestTools;

namespace Unity.Netcode.RuntimeTests
{
public class NetworkVarBufferCopyTest : BaseMultiInstanceTest
{
public class DummyNetVar : INetworkVariable
{
private const int k_DummyValue = 0x13579BDF;
public bool DeltaWritten;
public bool FieldWritten;
public bool DeltaRead;
public bool FieldRead;
public bool Dirty = true;

public string Name { get; internal set; }

public NetworkChannel GetChannel()
{
return NetworkChannel.NetworkVariable;
}

public void ResetDirty()
{
Dirty = false;
}

public bool IsDirty()
{
return Dirty;
}

public bool CanClientWrite(ulong clientId)
{
return true;
}

public bool CanClientRead(ulong clientId)
{
return true;
}

public void WriteDelta(Stream stream)
{
using (var writer = PooledNetworkWriter.Get(stream))
{
writer.WriteBits((byte)1, 1);
writer.WriteInt32(k_DummyValue);
}

DeltaWritten = true;
}

public void WriteField(Stream stream)
{
using (var writer = PooledNetworkWriter.Get(stream))
{
writer.WriteBits((byte)1, 1);
writer.WriteInt32(k_DummyValue);
}

FieldWritten = true;
}

public void ReadField(Stream stream)
{
using (var reader = PooledNetworkReader.Get(stream))
{
reader.ReadBits(1);
Assert.AreEqual(k_DummyValue, reader.ReadInt32());
}

FieldRead = true;
}

public void ReadDelta(Stream stream, bool keepDirtyDelta)
{
using (var reader = PooledNetworkReader.Get(stream))
{
reader.ReadBits(1);
Assert.AreEqual(k_DummyValue, reader.ReadInt32());
}

DeltaRead = true;
}

public void SetNetworkBehaviour(NetworkBehaviour behaviour)
{
// nop
}
}

public class DummyNetBehaviour : NetworkBehaviour
{
public DummyNetVar NetVar;
}
protected override int NbClients => 1;

[UnitySetUp]
public override IEnumerator Setup()
{
yield return StartSomeClientsAndServerWithPlayers(useHost: true, nbClients: NbClients,
updatePlayerPrefab: playerPrefab =>
{
var dummyNetBehaviour = playerPrefab.AddComponent<DummyNetBehaviour>();
});
}

[UnityTest]
public IEnumerator TestEntireBufferIsCopiedOnNetworkVariableDelta()
{
// This is the *SERVER VERSION* of the *CLIENT PLAYER*
var serverClientPlayerResult = new MultiInstanceHelpers.CoroutineResultWrapper<NetworkObject>();
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation(
x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId,
m_ServerNetworkManager, serverClientPlayerResult));

// This is the *CLIENT VERSION* of the *CLIENT PLAYER*
var clientClientPlayerResult = new MultiInstanceHelpers.CoroutineResultWrapper<NetworkObject>();
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation(
x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId,
m_ClientNetworkManagers[0], clientClientPlayerResult));

var serverSideClientPlayer = serverClientPlayerResult.Result;
var clientSideClientPlayer = clientClientPlayerResult.Result;

var serverComponent = (serverSideClientPlayer).GetComponent<DummyNetBehaviour>();
var clientComponent = (clientSideClientPlayer).GetComponent<DummyNetBehaviour>();

var waitResult = new MultiInstanceHelpers.CoroutineResultWrapper<bool>();

yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForCondition(
() => clientComponent.NetVar.DeltaRead == true,
waitResult,
maxFrames: 120));

if (!waitResult.Result)
{
Assert.Fail("Failed to send a delta within 120 frames");
}
Assert.True(serverComponent.NetVar.FieldWritten);
Assert.True(serverComponent.NetVar.DeltaWritten);
Assert.True(clientComponent.NetVar.FieldRead);
Assert.True(clientComponent.NetVar.DeltaRead);
}
}
}

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