Skip to content

feat: FastBufferReader/Writer IsInitialized property #1859

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 4 commits into from
Apr 5, 2022
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
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
- Added `UnityTransport` implementation and `com.unity.transport` package dependency (#1823)
- Added `NetworkVariableWritePermission` to `NetworkVariableBase` and implemented `Owner` client writable netvars. (#1762)
- `UnityTransport` settings can now be set programmatically. (#1845)
- `FastBufferWriter` and Reader IsInitialized property. (#1859)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal struct ReaderHandle
#endif
}

internal readonly unsafe ReaderHandle* Handle;
internal unsafe ReaderHandle* Handle;

/// <summary>
/// Get the current read position
Expand All @@ -39,6 +39,11 @@ public unsafe int Length
get => Handle->Length;
}

/// <summary>
/// Gets a value indicating whether the reader has been initialized and a handle allocated.
/// </summary>
public unsafe bool IsInitialized => Handle != null;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void CommitBitwiseReads(int amount)
{
Expand Down Expand Up @@ -196,6 +201,7 @@ public unsafe FastBufferReader(FastBufferWriter writer, Allocator allocator, int
public unsafe void Dispose()
{
UnsafeUtility.Free(Handle, Handle->Allocator);
Handle = null;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal struct WriterHandle
#endif
}

internal readonly unsafe WriterHandle* Handle;
internal unsafe WriterHandle* Handle;

private static byte[] s_ByteArrayCache = new byte[65535];

Expand Down Expand Up @@ -62,6 +62,11 @@ public unsafe int Length
get => Handle->Position > Handle->Length ? Handle->Position : Handle->Length;
}

/// <summary>
/// Gets a value indicating whether the writer has been initialized and a handle allocated.
/// </summary>
public unsafe bool IsInitialized => Handle != null;


[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void CommitBitwiseWrites(int amount)
Expand Down Expand Up @@ -111,6 +116,7 @@ public unsafe void Dispose()
UnsafeUtility.Free(Handle->BufferPointer, Handle->Allocator);
}
UnsafeUtility.Free(Handle, Handle->Allocator);
Handle = null;
}

/// <summary>
Expand Down Expand Up @@ -207,7 +213,7 @@ internal unsafe void Grow(int additionalSizeRequired)
/// When you know you will be writing multiple fields back-to-back and you know the total size,
/// you can call TryBeginWrite() once on the total size, and then follow it with calls to
/// WriteValue() instead of WriteValueSafe() for faster serialization.
///
///
Copy link
Collaborator

Choose a reason for hiding this comment

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

This change is unacceptable to me. The whitespace at the end of this comment line was critical to the blahdy blah blah blah...

(Thanks for cleaning this up.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha I don't think that was me but just Rider auto formatting 😆

/// Unsafe write operations will throw OverflowException in editor and development builds if you
/// go past the point you've marked using TryBeginWrite(). In release builds, OverflowException will not be thrown
/// for performance reasons, since the point of using TryBeginWrite is to avoid bounds checking in the following
Expand Down Expand Up @@ -253,7 +259,7 @@ public unsafe bool TryBeginWrite(int bytes)
/// When you know you will be writing multiple fields back-to-back and you know the total size,
/// you can call TryBeginWrite() once on the total size, and then follow it with calls to
/// WriteValue() instead of WriteValueSafe() for faster serialization.
///
///
/// Unsafe write operations will throw OverflowException in editor and development builds if you
/// go past the point you've marked using TryBeginWrite(). In release builds, OverflowException will not be thrown
/// for performance reasons, since the point of using TryBeginWrite is to avoid bounds checking in the following
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,34 @@ public void WhenCreatingAReaderFromAnEmptyBuffer_LengthIsZero()
}
}

[Test]
public void WhenCreatingNewFastBufferReader_IsInitializedIsTrue()
{
var array = new NativeArray<byte>(100, Allocator.Temp);
var reader = new FastBufferReader(array, Allocator.Temp);
Assert.AreEqual(true, reader.IsInitialized);
reader.Dispose();
array.Dispose();
}

[Test]
public void WhenDisposingFastBufferReader_IsInitializedIsFalse()
{
var array = new NativeArray<byte>(100, Allocator.Temp);
var reader = new FastBufferReader(array, Allocator.Temp);
reader.Dispose();
Assert.AreEqual(false, reader.IsInitialized);
array.Dispose();
Assert.AreEqual(false, reader.IsInitialized);
}

[Test]
public void WhenUsingDefaultFastBufferReader_IsInitializedIsFalse()
{
FastBufferReader writer = default;
Assert.AreEqual(false, writer.IsInitialized);
}

[Test]
public void WhenCallingReadByteWithoutCallingTryBeingReadFirst_OverflowExceptionIsThrown()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,29 @@ public void WhenCreatingNewFastBufferWriter_MaxCapacityIsCorrect()
writer.Dispose();
}

[Test]
public void WhenCreatingNewFastBufferWriter_IsInitializedIsTrue()
{
var writer = new FastBufferWriter(100, Allocator.Temp);
Assert.AreEqual(true, writer.IsInitialized);
writer.Dispose();
}

[Test]
public void WhenDisposingFastBufferWriter_IsInitializedIsFalse()
{
var writer = new FastBufferWriter(100, Allocator.Temp);
writer.Dispose();
Assert.AreEqual(false, writer.IsInitialized);
}

[Test]
public void WhenUsingDefaultFastBufferWriter_IsInitializedIsFalse()
{
FastBufferWriter writer = default;
Assert.AreEqual(false, writer.IsInitialized);
}

[Test]
public void WhenRequestingWritePastBoundsForNonGrowingWriter_TryBeginWriteReturnsFalse()
{
Expand Down