Skip to content

Refactor NetDataReader for safer, faster reads#593

Open
FUTURE-SL wants to merge 4 commits intoRevenantX:masterfrom
FUTURE-SL:reader-update
Open

Refactor NetDataReader for safer, faster reads#593
FUTURE-SL wants to merge 4 commits intoRevenantX:masterfrom
FUTURE-SL:reader-update

Conversation

@FUTURE-SL
Copy link
Contributor

Rework NetDataReader to improve safety, performance and API clarity. Adds bounds-checked helpers (EnsureAvailable, ThrowNotEnoughData), validates SetSource/SetPosition inputs and changes SetSource to use an endOffset. Replaces BitConverter/Buffer.BlockCopy usages with Span/MemoryMarshal-based GetUnmanaged/PeekUnmanaged/TryGetUnmanaged helpers and uses ReadOnlySpan/ReadOnlyMemory for remaining bytes. Streamlines Get/Peek/TryGet methods (value-returning overloads, generic unmanaged array handling), improves string and IPEndPoint parsing, and introduces typed TryGet and Peek methods for common primitives.

Rework NetDataReader to improve safety, performance and API clarity. Adds bounds-checked helpers (EnsureAvailable, ThrowNotEnoughData), validates SetSource/SetPosition inputs and changes SetSource to use an endOffset. Replaces BitConverter/Buffer.BlockCopy usages with Span/MemoryMarshal-based GetUnmanaged/PeekUnmanaged/TryGetUnmanaged helpers and uses ReadOnlySpan/ReadOnlyMemory for remaining bytes. Streamlines Get/Peek/TryGet methods (value-returning overloads, generic unmanaged array handling), improves string and IPEndPoint parsing, and introduces typed TryGet and Peek methods for common primitives.
Add System.Runtime.CompilerServices.Unsafe (v6.0.0) to the project and update NetDataReader.cs to use Unsafe.ReadUnaligned for unmanaged reads instead of MemoryMarshal.Read/TryRead. Also adjust TryGetUnmanaged to validate size and advance position correctly, and fix the generic Deserialize loop to create/deserialize the item before assigning it to the result array. Changes touch LiteNetLib.csproj and Utils/NetDataReader.cs.
Remove the System.Runtime.CompilerServices.Unsafe package reference from the csproj and replace uses of Unsafe.ReadUnaligned<T> in NetDataReader with a new private unsafe ReadUnmanaged<T>() method (uses fixed pointer dereference and AggressiveInlining). This preserves the previous unaligned reads while eliminating the external Unsafe dependency and keeping behavior unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant