Skip to content

Reader + Writer update#598

Merged
RevenantX merged 23 commits intoRevenantX:masterfrom
FUTURE-SL:reader-update-new
Mar 25, 2026
Merged

Reader + Writer update#598
RevenantX merged 23 commits intoRevenantX:masterfrom
FUTURE-SL:reader-update-new

Conversation

@FUTURE-SL
Copy link
Copy Markdown
Contributor

No description provided.

Replace the previous bounds check in EnsureAvailable with an unsigned comparison against a local available variable and extract the throw into a separate ThrowNotEnoughData method. The new helper is marked NoInlining and includes XML documentation. This reduces duplicated throw logic in hot paths and makes the bounds-check semantics clearer and more JIT-friendly.
Correct a typo in the XML documentation of LiteNetLib/Utils/NetDataReader.cs by replacing an invalid <see cre="class"/> attribute with <see langword="class"/>. This fixes documentation warnings and ensures the class reference renders correctly.
Replace direct instantiation and obj.Deserialize(this) with calls to existing Get(out ...) overloads in both struct and class INetSerializable Get<T> methods. This removes duplicated deserialization code and centralizes behavior without changing functionality.
Introduce IPv4Size and IPv6Size constants and consolidate IPv4/IPv6 branching in GetIPEndPoint. Read the IP version flag, compute the address size, ensure buffer availability, construct the IPAddress from a ReadOnlySpan, and advance the position. Removes duplicated code and magic numbers while preserving existing behavior.
Call EnsureAvailable(1) before reading from the internal buffer in NetDataReader.GetByte to prevent out-of-range reads and provide consistent error handling. The method still increments the read position after returning the byte.
Use checked() when computing byteLength and call EnsureAvailable(byteLength) before taking a span. This prevents silent integer overflow on length * sizeof(T) and verifies there are enough bytes available for MemoryMarshal.Cast and subsequent array creation in GetUnmanagedArray<T>().
Introduce a GuidSize constant (16) and use it in GetGuid() instead of hard-coded 16s. This improves readability and avoids repeated magic numbers when ensuring availability, constructing the Guid from the data span, and advancing the read position.
Introduce IPv4Size (4), IPv6Size (16) and GuidSize (16) constants in NetDataWriter.cs and replace magic numbers with these constants in Put(Guid) and when determining addressSize for IPEndPoint. This improves readability and maintainability by avoiding literal size values.
Add EnsureAvailable checks to both GetStringArray overloads to verify there are enough bytes for the array string lengths before reading (uses checked(length * sizeof(ushort)) to catch overflows). Also rename the local array variable from `arr` to `result` and return it, cleaning up the methods and improving safety against buffer overruns.
Replace direct calculation (_dataSize - _position) with the AvailableBytes property in GetRemainingBytesSpan and GetRemainingBytesMemory. This deduplicates logic, improves clarity, and ensures both methods use the same availability check when returning the remaining data slice before advancing the position.
Avoid allocating a new byte[] when no data remains by returning Array.Empty<byte>() for size == 0. Also rename the temporary buffer to result for clarity; behavior for non-zero reads is unchanged (copy remaining bytes, advance position to end). This reduces allocations for empty reads.
Replace the previous platform-specific unsafe read logic in GetEnum<T>() with a direct call to GetUnmanaged<T>(). This centralizes unmanaged enum reading, removes the NET8 conditional (Unsafe.ReadUnaligned / fixed pointer) and reduces duplicated unsafe buffer-reading code without changing behavior.
Replace casting from GetByte() with a direct call to GetSByte() in LiteNetLib/Utils/NetDataReader.cs. This ensures signed byte values are read using the dedicated reader method (preserving correct sign/semantics) and keeps the reading logic consistent.
Move GetByte and GetSByte methods within LiteNetLib/Utils/NetDataReader.cs so they are grouped with other primitive read methods (placed before GetBool). This is a non-functional refactor to improve code organization and readability; no logic changes were made.
Refactor PeekSByte, PeekBool and PeekChar to call existing PeekByte()/PeekUShort() instead of directly accessing _data or PeekUnmanaged<char>(). This centralizes byte/ushort read logic (and any bounds/validation behavior) to reduce duplication and potential subtle bugs while preserving external behavior.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates LiteNetLib’s binary reader/writer utilities to replace magic numbers with named size constants, tighten bounds checks, and refactor several read/try-read paths for consistency and reduced duplication.

Changes:

  • Introduced IPv4Size/IPv6Size/GuidSize constants and applied them in reader/writer logic.
  • Refactored NetDataReader bounds checking (EnsureAvailable) to a fast-path with a non-inlined throw helper and added TryGetUnmanaged<T>().
  • Improved several read APIs (e.g., GetByte now validates availability, rollback behavior in TryGetStringArray, overflow-checked byte counts for unmanaged arrays).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
LiteNetLib/Utils/NetDataWriter.cs Replaces hard-coded sizes for GUID/IP serialization with named constants.
LiteNetLib/Utils/NetDataReader.cs Refactors bounds checking and multiple getters/peekers/try-getters; adds TryGetUnmanaged<T>() and applies size constants.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread LiteNetLib/Utils/NetDataReader.cs Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@RevenantX RevenantX merged commit 8925f01 into RevenantX:master Mar 25, 2026
1 check was pending
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.

3 participants