-
Notifications
You must be signed in to change notification settings - Fork 450
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8759d97
fix: corrected an off-by-one copying the netvar buffer into NetworkVa…
ShadauxCat da03f55
Fixed standards issues
ShadauxCat ac2e4f4
Fixed it correctly this time.
ShadauxCat 676af56
Merge branch 'develop' into fix/off_by_one_netvar_buffer_copy
ShadauxCat 833552e
Merge branch 'develop' into fix/off_by_one_netvar_buffer_copy
ShadauxCat a8df0d1
fix
NoelStephensUnity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
com.unity.multiplayer.mlapi/Tests/Runtime/NetworkVarBufferCopyTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
com.unity.multiplayer.mlapi/Tests/Runtime/NetworkVarBufferCopyTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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