Show / Hide Table of Contents

Class NetDataReader

Inheritance
object
NetDataReader
NetPacketReader
Namespace: LiteNetLib.Utils
Assembly: LiteNetLib.dll
Syntax
public class NetDataReader

Constructors

NetDataReader()

Declaration
public NetDataReader()

NetDataReader(NetDataWriter)

Declaration
public NetDataReader(NetDataWriter writer)
Parameters
Type Name Description
NetDataWriter writer

NetDataReader(byte[])

Declaration
public NetDataReader(byte[] source)
Parameters
Type Name Description
byte[] source

NetDataReader(byte[], int, int)

Declaration
public NetDataReader(byte[] source, int offset, int maxSize)
Parameters
Type Name Description
byte[] source
int offset
int maxSize

Fields

_data

Declaration
protected byte[] _data
Field Value
Type Description
byte[]

_dataSize

Declaration
protected int _dataSize
Field Value
Type Description
int

_offset

Declaration
protected int _offset
Field Value
Type Description
int

_position

Declaration
protected int _position
Field Value
Type Description
int

Properties

AvailableBytes

Gets the number of bytes remaining to be read.

Declaration
public int AvailableBytes { get; }
Property Value
Type Description
int

EndOfData

Gets a value indicating whether the Position has reached the end of the data.

Declaration
public bool EndOfData { get; }
Property Value
Type Description
bool

IsNull

Gets a value indicating whether the internal data buffer is null.

Declaration
public bool IsNull { get; }
Property Value
Type Description
bool

Position

Gets the current read position within the buffer.

Declaration
public int Position { get; }
Property Value
Type Description
int

RawData

Gets the internal byte array containing the raw network data.

Declaration
public byte[] RawData { get; }
Property Value
Type Description
byte[]

RawDataSize

Gets the total size of the RawData buffer.

Declaration
public int RawDataSize { get; }
Property Value
Type Description
int

UserDataOffset

Gets the starting offset of the user payload within the RawData.

Declaration
public int UserDataOffset { get; }
Property Value
Type Description
int

UserDataSize

Gets the size of the user payload excluding the initial UserDataOffset.

Declaration
public int UserDataSize { get; }
Property Value
Type Description
int

Methods

Clear()

Clears the reader state and releases the reference to the internal buffer.

Declaration
public void Clear()

Get(out bool)

Reads a bool and assigns it to result.

Declaration
public void Get(out bool result)
Parameters
Type Name Description
bool result

Get(out byte)

Reads a byte and assigns it to result.

Declaration
public void Get(out byte result)
Parameters
Type Name Description
byte result

Get(out char)

Reads a char and assigns it to result.

Declaration
public void Get(out char result)
Parameters
Type Name Description
char result

Get(out double)

Reads a double and assigns it to result.

Declaration
public void Get(out double result)
Parameters
Type Name Description
double result

Get(out Guid)

Reads a Guid and assigns it to result.

Declaration
public void Get(out Guid result)
Parameters
Type Name Description
Guid result

Get(out short)

Reads a short and assigns it to result.

Declaration
public void Get(out short result)
Parameters
Type Name Description
short result

Get(out int)

Reads an int and assigns it to result.

Declaration
public void Get(out int result)
Parameters
Type Name Description
int result

Get(out long)

Reads a long and assigns it to result.

Declaration
public void Get(out long result)
Parameters
Type Name Description
long result

Get(out IPEndPoint)

Deserializes an IPEndPoint and assigns it to the result parameter.

Declaration
public void Get(out IPEndPoint result)
Parameters
Type Name Description
IPEndPoint result

The deserialized IPEndPoint output.

Get(out sbyte)

Reads an sbyte and assigns it to result.

Declaration
public void Get(out sbyte result)
Parameters
Type Name Description
sbyte result

Get(out float)

Reads a float and assigns it to result.

Declaration
public void Get(out float result)
Parameters
Type Name Description
float result

Get(out string)

Reads a string and assigns it to result.

Declaration
public void Get(out string result)
Parameters
Type Name Description
string result

Get(out string, int)

Reads a string with a length limit and assigns it to result.

Declaration
public void Get(out string result, int maxLength)
Parameters
Type Name Description
string result
int maxLength

Get(out ushort)

Reads a ushort and assigns it to result.

Declaration
public void Get(out ushort result)
Parameters
Type Name Description
ushort result

Get(out uint)

Reads a uint and assigns it to result.

Declaration
public void Get(out uint result)
Parameters
Type Name Description
uint result

Get(out ulong)

Reads a ulong and assigns it to result.

Declaration
public void Get(out ulong result)
Parameters
Type Name Description
ulong result

GetArray<T>()

Reads an array of objects implementing INetSerializable.

Declaration
public T[] GetArray<T>() where T : INetSerializable, new()
Returns
Type Description
T[]

A new array of type T.

Type Parameters
Name Description
T

A type with a parameterless constructor implementing INetSerializable.

GetArray<T>(Func<T>)

Reads an array of objects implementing INetSerializable using a specific constructor.

Declaration
public T[] GetArray<T>(Func<T> constructor) where T : class, INetSerializable
Parameters
Type Name Description
Func<T> constructor

The factory delegate used to create instances.

Returns
Type Description
T[]

A new array of type T.

Type Parameters
Name Description
T

A class type implementing INetSerializable.

GetArray<T>(ushort)

Reads an array of values by performing a direct memory copy.

Declaration
public T[] GetArray<T>(ushort size)
Parameters
Type Name Description
ushort size

The size of a single element in bytes.

Returns
Type Description
T[]

A new array of type T.

Type Parameters
Name Description
T

The element type.

GetBool()

Reads a bool value from the current position.

Declaration
public bool GetBool()
Returns
Type Description
bool

true if the byte is 1; otherwise, false.

GetBoolArray()

Reads an array of bool values.

Declaration
public bool[] GetBoolArray()
Returns
Type Description
bool[]

GetByte()

Reads the next byte from the buffer.

Declaration
public byte GetByte()
Returns
Type Description
byte

GetBytes(byte[], int)

Copies a specified number of bytes into a destination array starting at index 0.

Declaration
public void GetBytes(byte[] destination, int count)
Parameters
Type Name Description
byte[] destination

The array to copy data into.

int count

The number of bytes to read.

GetBytes(byte[], int, int)

Copies a specified number of bytes into a destination array at a specific offset.

Declaration
public void GetBytes(byte[] destination, int start, int count)
Parameters
Type Name Description
byte[] destination

The array to copy data into.

int start

The starting index in the destination array.

int count

The number of bytes to read.

GetBytesSegment(int)

Gets an ArraySegment<T> of bytes from the current position.

Declaration
public ArraySegment<byte> GetBytesSegment(int count)
Parameters
Type Name Description
int count

The number of bytes to include in the segment.

Returns
Type Description
ArraySegment<byte>

An ArraySegment<T> wrapping the internal buffer.

GetBytesWithLength()

Reads a byte array prefixed with its ushort length.

Declaration
public byte[] GetBytesWithLength()
Returns
Type Description
byte[]

A new byte array.

GetChar()

Reads a char value as a 2-byte ushort.

Declaration
public char GetChar()
Returns
Type Description
char

GetDouble()

Reads a double value using unmanaged memory access.

Declaration
public double GetDouble()
Returns
Type Description
double

GetDoubleArray()

Reads an array of double values.

Declaration
public double[] GetDoubleArray()
Returns
Type Description
double[]

GetEnum<T>()

Reads an enum value of type T from the internal data buffer at the current position.
Advances the position by the size of T.

Declaration
public T GetEnum<T>() where T : unmanaged, Enum
Returns
Type Description
T

The enum value read from the buffer.

Type Parameters
Name Description
T

An unmanaged enum type to read.

GetFloat()

Reads a float value using unmanaged memory access.

Declaration
public float GetFloat()
Returns
Type Description
float

GetFloatArray()

Reads an array of float values.

Declaration
public float[] GetFloatArray()
Returns
Type Description
float[]

GetGuid()

Reads a 16-byte Guid.

Declaration
public Guid GetGuid()
Returns
Type Description
Guid

The deserialized Guid.

GetIPEndPoint()

Reads an IPEndPoint from the current Position.

Declaration
public IPEndPoint GetIPEndPoint()
Returns
Type Description
IPEndPoint

The deserialized IPEndPoint.

Remarks

Reads a byte to determine the AddressFamily (0 for IPv4, 1 for IPv6), followed by the address bytes and a 2-byte ushort port.

GetInt()

Reads an int value using unmanaged memory access.

Declaration
public int GetInt()
Returns
Type Description
int

GetIntArray()

Reads an array of int values.

Declaration
public int[] GetIntArray()
Returns
Type Description
int[]

GetLargeString()

Reads a string prefixed with a 4-byte int length header.

Declaration
public string GetLargeString()
Returns
Type Description
string

The deserialized string.

GetLong()

Reads a long value using unmanaged memory access.

Declaration
public long GetLong()
Returns
Type Description
long

GetLongArray()

Reads an array of long values.

Declaration
public long[] GetLongArray()
Returns
Type Description
long[]

GetNullableUnmanaged<T>()

Reads a nullable value of type T from the internal byte buffer at the current position, first reading a bool indicating whether the value is present, and then reading the value itself if it exists.
Advances the position by 1 byte for the presence flag plus the size of T if the value is present.

Declaration
public T? GetNullableUnmanaged<T>() where T : unmanaged
Returns
Type Description
T?

The nullable value of type T read from the buffer. Returns null if the presence flag indicates no value.

Type Parameters
Name Description
T

An unmanaged value type to read from the buffer.

Exceptions
Type Condition
IndexOutOfRangeException

GetRemainingBytes()

Reads all remaining bytes and returns them as a new array.

Declaration
public byte[] GetRemainingBytes()
Returns
Type Description
byte[]

A new byte array containing the remaining data.

Remarks

This method performs a heap allocation and advances the Position to the end of the data.

GetRemainingBytesMemory()

Returns a ReadOnlyMemory<T> of bytes containing all remaining data.

Declaration
public ReadOnlyMemory<byte> GetRemainingBytesMemory()
Returns
Type Description
ReadOnlyMemory<byte>

A ReadOnlyMemory<T> from the current Position to the end of the buffer.

GetRemainingBytesSegment()

Gets an ArraySegment<T> containing all remaining bytes.

Declaration
public ArraySegment<byte> GetRemainingBytesSegment()
Returns
Type Description
ArraySegment<byte>

An ArraySegment<T> from the current position to the end of the data.

GetRemainingBytesSpan()

Returns a ReadOnlySpan<T> of bytes containing all remaining data.

Declaration
public ReadOnlySpan<byte> GetRemainingBytesSpan()
Returns
Type Description
ReadOnlySpan<byte>

A ReadOnlySpan<T> from the current Position to the end of the buffer.

GetSByte()

Reads the next sbyte from the buffer.

Declaration
public sbyte GetSByte()
Returns
Type Description
sbyte

GetSBytesWithLength()

Reads an sbyte array prefixed with its ushort length.

Declaration
public sbyte[] GetSBytesWithLength()
Returns
Type Description
sbyte[]

A new sbyte array.

GetShort()

Reads a short value using unmanaged memory access.

Declaration
public short GetShort()
Returns
Type Description
short

GetShortArray()

Reads an array of short values.

Declaration
public short[] GetShortArray()
Returns
Type Description
short[]

GetString()

Reads a string from the current position.

Declaration
public string GetString()
Returns
Type Description
string

The deserialized string.

GetString(int)

Reads a string with a maximum character limit.

Declaration
public string GetString(int maxLength)
Parameters
Type Name Description
int maxLength

The maximum allowed character count.

Returns
Type Description
string

The deserialized string, or Empty if the character count exceeds maxLength.

Remarks

Note that maxLength limits the number of characters, not the total size in bytes.

GetStringArray()

Reads an array of string values.

Declaration
public string[] GetStringArray()
Returns
Type Description
string[]

A new string array.

Remarks

Reads a 2-byte ushort length header followed by each string element.

GetStringArray(int)

Reads an array of string values with a maximum character limit per element.

Declaration
public string[] GetStringArray(int maxStringLength)
Parameters
Type Name Description
int maxStringLength

The maximum number of characters allowed per string.

Returns
Type Description
string[]

A new string array.

Remarks

Strings exceeding maxStringLength are returned as Empty.

GetUInt()

Reads a uint value using unmanaged memory access.

Declaration
public uint GetUInt()
Returns
Type Description
uint

GetUIntArray()

Reads an array of uint values.

Declaration
public uint[] GetUIntArray()
Returns
Type Description
uint[]

GetULong()

Reads a ulong value using unmanaged memory access.

Declaration
public ulong GetULong()
Returns
Type Description
ulong

GetULongArray()

Reads an array of ulong values.

Declaration
public ulong[] GetULongArray()
Returns
Type Description
ulong[]

GetUShort()

Reads a ushort value using unmanaged memory access.

Declaration
public ushort GetUShort()
Returns
Type Description
ushort

GetUShortArray()

Reads an array of ushort values.

Declaration
public ushort[] GetUShortArray()
Returns
Type Description
ushort[]

GetUnmanagedArray<T>()

Reads an array of unmanaged values prefixed by a ushort length.

Declaration
public T[] GetUnmanagedArray<T>() where T : unmanaged
Returns
Type Description
T[]

A new array of type T.

Type Parameters
Name Description
T

An unmanaged type.

GetUnmanaged<T>()

Reads a value of type T from the internal byte buffer at the current position, advancing the position by the size of T.

Declaration
public T GetUnmanaged<T>() where T : unmanaged
Returns
Type Description
T

The value of type T read from the buffer.

Type Parameters
Name Description
T

An unmanaged value type to read from the buffer.

Exceptions
Type Condition
IndexOutOfRangeException

Get<T>()

Deserializes a struct that implements INetSerializable.

Declaration
public T Get<T>() where T : struct, INetSerializable
Returns
Type Description
T

The deserialized struct.

Type Parameters
Name Description
T

A struct type implementing INetSerializable.

Get<T>(Func<T>)

Deserializes a class that implements INetSerializable using a provided constructor.

Declaration
public T Get<T>(Func<T> constructor) where T : class, INetSerializable
Parameters
Type Name Description
Func<T> constructor

The factory delegate used to instantiate the class.

Returns
Type Description
T

A new instance of T.

Type Parameters
Name Description
T

A class type implementing INetSerializable.

Get<T>(out T)

Deserializes a struct that implements INetSerializable.

Declaration
public void Get<T>(out T result) where T : struct, INetSerializable
Parameters
Type Name Description
T result

The deserialized struct output.

Type Parameters
Name Description
T

A struct type implementing INetSerializable.

Get<T>(out T, Func<T>)

Deserializes a class that implements INetSerializable using a provided constructor.

Declaration
public void Get<T>(out T result, Func<T> constructor) where T : class, INetSerializable
Parameters
Type Name Description
T result

The deserialized class instance output.

Func<T> constructor

A factory delegate used to instantiate the class.

Type Parameters
Name Description
T

A class type implementing INetSerializable.

PeekBool()

Reads the bool at the current position without advancing the Position.

Declaration
public bool PeekBool()
Returns
Type Description
bool

PeekByte()

Reads the byte at the current position without advancing the Position.

Declaration
public byte PeekByte()
Returns
Type Description
byte

PeekChar()

Reads the char at the current position without advancing the Position.

Declaration
public char PeekChar()
Returns
Type Description
char

PeekDouble()

Reads the double at the current position without advancing the Position.

Declaration
public double PeekDouble()
Returns
Type Description
double

PeekFloat()

Reads the float at the current position without advancing the Position.

Declaration
public float PeekFloat()
Returns
Type Description
float

PeekInt()

Reads the int at the current position without advancing the Position.

Declaration
public int PeekInt()
Returns
Type Description
int

PeekLong()

Reads the long at the current position without advancing the Position.

Declaration
public long PeekLong()
Returns
Type Description
long

PeekSByte()

Reads the sbyte at the current position without advancing the Position.

Declaration
public sbyte PeekSByte()
Returns
Type Description
sbyte

PeekShort()

Reads the short at the current position without advancing the Position.

Declaration
public short PeekShort()
Returns
Type Description
short

PeekString()

Reads a string without advancing the Position.

Declaration
public string PeekString()
Returns
Type Description
string

PeekString(int)

Reads a string with a character limit without advancing the Position.

Declaration
public string PeekString(int maxLength)
Parameters
Type Name Description
int maxLength

Maximum allowed character count.

Returns
Type Description
string
Remarks

Strings exceeding maxLength are returned as Empty.

PeekUInt()

Reads the uint at the current position without advancing the Position.

Declaration
public uint PeekUInt()
Returns
Type Description
uint

PeekULong()

Reads the ulong at the current position without advancing the Position.

Declaration
public ulong PeekULong()
Returns
Type Description
ulong

PeekUShort()

Reads the ushort at the current position without advancing the Position.

Declaration
public ushort PeekUShort()
Returns
Type Description
ushort

PeekUnmanaged<T>()

Reads an unmanaged value of type T at the current position without advancing the Position.

Declaration
public T PeekUnmanaged<T>() where T : unmanaged
Returns
Type Description
T

The value read from the buffer.

Type Parameters
Name Description
T

An unmanaged value type.

SetPosition(int)

Sets the current read Position to a specific index.

Declaration
public void SetPosition(int position)
Parameters
Type Name Description
int position

The index to move the Position to.

SetSource(NetDataWriter)

Reinitializes the reader using data from a NetDataWriter.

Declaration
public void SetSource(NetDataWriter dataWriter)
Parameters
Type Name Description
NetDataWriter dataWriter

The source NetDataWriter.

SetSource(byte[])

Reinitializes the reader using a byte array.

Declaration
public void SetSource(byte[] source)
Parameters
Type Name Description
byte[] source

The source byte array.

SetSource(byte[], int, int)

Reinitializes the reader using a segment of a byte array.

Declaration
public void SetSource(byte[] source, int offset, int maxSize)
Parameters
Type Name Description
byte[] source

The source byte array.

int offset

The starting index for reading.

int maxSize

The total number of bytes available to read from the source.

SkipBytes(int)

Advances the Position by the specified count.

Declaration
public void SkipBytes(int count)
Parameters
Type Name Description
int count

The number of bytes to skip.

TryGetBool(out bool)

Attempts to read a bool without throwing an exception.

Declaration
public bool TryGetBool(out bool result)
Parameters
Type Name Description
bool result

The deserialized bool, or false if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetByte(out byte)

Attempts to read a byte without throwing an exception.

Declaration
public bool TryGetByte(out byte result)
Parameters
Type Name Description
byte result

The deserialized byte, or 0 if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetBytesWithLength(out byte[])

Attempts to read a byte array with a length header without throwing an exception.

Declaration
public bool TryGetBytesWithLength(out byte[] result)
Parameters
Type Name Description
byte[] result

The deserialized byte array, or null if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetChar(out char)

Attempts to read a char without throwing an exception.

Declaration
public bool TryGetChar(out char result)
Parameters
Type Name Description
char result

The deserialized char, or '\0' if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetDouble(out double)

Attempts to read a double without throwing an exception.

Declaration
public bool TryGetDouble(out double result)
Parameters
Type Name Description
double result

The deserialized double, or 0 if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetFloat(out float)

Attempts to read a float without throwing an exception.

Declaration
public bool TryGetFloat(out float result)
Parameters
Type Name Description
float result

The deserialized float, or 0 if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetInt(out int)

Attempts to read an int without throwing an exception.

Declaration
public bool TryGetInt(out int result)
Parameters
Type Name Description
int result

The deserialized int, or 0 if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetLong(out long)

Attempts to read a long without throwing an exception.

Declaration
public bool TryGetLong(out long result)
Parameters
Type Name Description
long result

The deserialized long, or 0 if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetSByte(out sbyte)

Attempts to read an sbyte without throwing an exception.

Declaration
public bool TryGetSByte(out sbyte result)
Parameters
Type Name Description
sbyte result

The deserialized sbyte, or 0 if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetShort(out short)

Attempts to read a short without throwing an exception.

Declaration
public bool TryGetShort(out short result)
Parameters
Type Name Description
short result

The deserialized short, or 0 if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetString(out string)

Attempts to read a string without throwing an exception.

Declaration
public bool TryGetString(out string result)
Parameters
Type Name Description
string result

The deserialized string, or null if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetStringArray(out string[])

Attempts to read a string array without throwing an exception.

Declaration
public bool TryGetStringArray(out string[] result)
Parameters
Type Name Description
string[] result

The deserialized string array, or null if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetUInt(out uint)

Attempts to read a uint without throwing an exception.

Declaration
public bool TryGetUInt(out uint result)
Parameters
Type Name Description
uint result

The deserialized uint, or 0 if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetULong(out ulong)

Attempts to read a ulong without throwing an exception.

Declaration
public bool TryGetULong(out ulong result)
Parameters
Type Name Description
ulong result

The deserialized ulong, or 0 if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetUShort(out ushort)

Attempts to read a ushort without throwing an exception.

Declaration
public bool TryGetUShort(out ushort result)
Parameters
Type Name Description
ushort result

The deserialized ushort, or 0 if failed.

Returns
Type Description
bool

true if enough data was available; otherwise, false.

TryGetUnmanaged<T>(out T)

Attempts to read a value of type T from the internal byte buffer at the current position, advancing the position by the size of T if successful.

Declaration
public bool TryGetUnmanaged<T>(out T result) where T : unmanaged
Parameters
Type Name Description
T result

When this method returns, contains the value read from the buffer, or the default value if the read failed.

Returns
Type Description
bool

true if enough data was available to read the value; otherwise, false.

Type Parameters
Name Description
T

An unmanaged value type to read from the buffer.

In this article
Back to top Generated by DocFX