Photon Fusion 2.1.1

NetBitBuffer Struct Reference

Represents a buffer for reading and writing bits. More...

Inherits INetBitWriteStream.

Classes

struct  Offset
 Represents an offset within a NetBitBuffer. More...

Public Member Functions

bool CanRead (int bits)
 Checks if the specified number of bits can be read from the buffer.
bool CanReadUInt32VarLength ()
 Reads a 32-bit unsigned integer value with variable length from the buffer.
bool CanWrite (int bits)
 Checks if the specified number of bits can be written to the buffer.
bool CheckBitCount (int count)
 Checks if the specified number of bits can be read or written without exceeding the buffer length.
void Clear (int? clearLength=null)
 Clears the buffer by setting all bits to zero.
byte * GetDataPointer ()
 Gets a pointer to the current data position in the buffer.
void PadToByteBoundary ()
 Pads the buffer to the next byte boundary by writing zero bits if necessary.
byte * PadToByteBoundaryAndGetPtr ()
 Pads the buffer to the next byte boundary and returns a pointer to the current data position.
bool PeekBoolean ()
 Peeks at the next boolean value in the buffer without advancing the read position.
bool ReadBoolean ()
 Reads a boolean value from the buffer.
byte ReadByte (int bits=8)
 Reads a byte value from the buffer.
void ReadBytesAligned (byte[] buffer, int length)
 Reads an array of bytes from the buffer, ensuring byte alignment.
void ReadBytesAligned (Span< byte > buffer)
 Reads a block of bytes from the buffer, ensuring byte alignment.
void ReadBytesAligned (void *buffer, int length)
 Reads a block of bytes from the buffer, ensuring byte alignment.
double ReadDouble ()
 Reads a double-precision floating-point value from the buffer.
short ReadInt16 (int bits=16)
 Reads a 16-bit signed integer value from the buffer.
int ReadInt32 (int bits=32)
 Reads a 32-bit signed integer value from the buffer.
int ReadInt32VarLength ()
 Reads a 32-bit signed integer value with variable length from the buffer.
int ReadInt32VarLength (int blockSize)
 Reads a 32-bit signed integer value with variable length from the buffer.
long ReadInt64 (int bits=64)
 Reads a 64-bit signed integer value from the buffer.
long ReadInt64VarLength (int blockSize)
 Reads a 64-bit signed integer value with variable length from the buffer.
float ReadSingle ()
 Reads a single-precision floating-point value from the buffer.
string ReadString ()
 Reads a string from the buffer using UTF-8 encoding.
string ReadString (Encoding encoding)
 Reads a string from the buffer using the specified encoding.
ushort ReadUInt16 (int bits=16)
 Reads a 16-bit unsigned integer value from the buffer.
uint ReadUInt32 (int bits=32)
 Reads a 32-bit unsigned integer value from the buffer.
uint ReadUInt32VarLength ()
 Reads a 32-bit unsigned integer value with variable length from the buffer.
uint ReadUInt32VarLength (int blockSize)
 Reads a 32-bit unsigned integer value with variable length from the buffer.
ulong ReadUInt64 (int bits=64)
 Reads a 64-bit unsigned integer value from the buffer.
ulong ReadUInt64VarLength (int blockSize)
 Reads a 64-bit unsigned integer value with variable length from the buffer.
void ReplaceDataFromBlockWithTemp (int tempSize)
 Replaces the current data block with a temporary block of the specified size.
void SeekToByteBoundary ()
 Advances the buffer offset to the next byte boundary.
void SetBufferLengthBytes (ulong *buffer, int lenghtInBytes)
 Sets the buffer length in bytes and updates the internal length in bits.
bool TryReadBoolean (out bool result)
bool TryReadInt32VarLength (int blockSize, out int result)
bool TryReadInt32VarLength (out int result)
bool? TryReadInt32VarLengthBoolPrefixed (out int result)
bool TryReadUInt32 (out uint result)
 Tries to read a 32-bit unsigned integer value from the buffer.
bool? TryReadUInt32BoolPrefixed (out uint result)
 Attempts to read a boolean prefixed 32-bit unsigned integer value from the buffer.
bool TryReadUInt32VarLength (int blockSize, out uint result)
 Reads a 32-bit unsigned integer value with variable length from the buffer.
bool TryReadUInt32VarLength (out uint result)
 Reads a 32-bit unsigned integer value with variable length from the buffer.
bool? TryReadUInt32VarLengthBoolPrefixed (out uint result)
bool TryReadUInt64VarLength (int blockSize, out ulong result)
bool? TryReadUInt64VarLengthBoolPrefixed (int blockSize, out ulong result)
void Write (ulong value, int bits)
 Writes a value to the buffer with a specified number of bits.
bool WriteBoolean (bool value)
 Writes a boolean value to the buffer.
void WriteByte (byte value, int bits=8)
 Writes a byte value to the buffer.
void WriteBytesAligned (byte[] buffer, int length)
 Writes an array of bytes to the buffer, ensuring byte alignment.
void WriteBytesAligned (Span< byte > buffer)
 Writes a block of bytes to the buffer, ensuring byte alignment.
void WriteBytesAligned (void *buffer, int length)
 Writes a block of bytes to the buffer, ensuring byte alignment.
void WriteDouble (double value)
 Writes a double-precision floating-point value to the buffer.
void WriteInt16 (short value, int bits=16)
 Writes a 16-bit signed integer value to the buffer.
void WriteInt32 (int value, int bits=32)
 Writes a 32-bit signed integer value to the buffer.
void WriteInt32AtOffset (int value, int offset, int bits)
 Writes a 32-bit integer value at a specified offset in the buffer.
void WriteInt32VarLength (int value)
 Writes a 32-bit signed integer value with variable length to the buffer.
void WriteInt32VarLength (int value, int blockSize)
 Writes a 32-bit signed integer value with variable length to the buffer.
void WriteInt64 (long value, int bits=64)
 Writes a 64-bit signed integer value to the buffer.
void WriteInt64VarLength (long value, int blockSize)
 Writes a 64-bit signed integer value with variable length to the buffer.
void WriteSingle (float value)
 Writes a single-precision floating-point value to the buffer.
void WriteSlow (ulong value, int bits)
 Writes a value to the buffer with a specified number of bits, handling cases where the value spans multiple words.
void WriteString (string value)
 Writes a string to the buffer using UTF-8 encoding.
void WriteString (string value, Encoding encoding)
 Writes a string to the buffer using the specified encoding.
void WriteUInt16 (ushort value, int bits=16)
 Writes a 16-bit unsigned integer value to the buffer.
void WriteUInt32 (uint value, int bits=32)
 Writes a 32-bit unsigned integer value to the buffer.
void WriteUInt32VarLength (uint value)
 Writes a 32-bit unsigned integer value with variable length to the buffer.
void WriteUInt32VarLength (uint value, int blockSize)
 Writes a 32-bit unsigned integer value with variable length to the buffer.
void WriteUInt64 (ulong value, int bits=64)
 Writes a 64-bit unsigned integer value to the buffer.
void WriteUInt64AtOffset (ulong value, int offset, int bits)
 Writes a 64-bit unsigned integer value at a specified offset in the buffer.
void WriteUInt64VarLength (ulong value, int blockSize)
 Writes a 64-bit unsigned integer value with variable length to the buffer.

Static Public Member Functions

static NetBitBufferAllocate (int group, int size)
 Allocates a new NetBitBuffer with the specified group and size.
static string Dump (NetBitBuffer *buffer, int columns=16)
 Dumps the buffer to a hex string.
static Offset GetOffset (NetBitBuffer *buffer)
 Gets the current offset of the specified buffer.
static void Release (NetBitBuffer *buffer)
 Releases the specified buffer.
static void ReleaseRef (ref NetBitBuffer *buffer)
 Releases the reference to the specified buffer and sets it to null.

Public Attributes

NetAddress Address
 The address of the buffer.

Properties

readonly int BytesRemaining [get]
 Gets the number of bytes remaining in the buffer.
ulong * Data [get]
 Gets or sets a pointer to the data in the buffer.
readonly bool Done [get, set]
 Gets a value indicating whether the buffer has been fully read.
readonly bool DoneOrOverflow [get, set]
 Gets a value indicating whether the buffer is done or has overflowed.
readonly bool IsOnEvenByte [get, set]
 Gets a value indicating whether the buffer is aligned to an even byte boundary.
readonly int LengthBits [get]
 Gets the length of the buffer in bits.
int LengthBytes [get]
 Gets or sets the length of the buffer in bytes.
readonly bool MoreToRead [get, set]
 Gets a value indicating whether there is more data to read in the buffer.
int OffsetBits [get, set]
 Gets or sets the current offset in bits.
int OffsetBitsUnsafe [get, set]
 Gets or sets the current offset in bits without any safety checks.
readonly int OffsetBytes [get]
 Gets the current offset in bytes.
readonly bool Overflow [get, set]
 Gets a value indicating whether the buffer has overflowed.

Detailed Description

Represents a buffer for reading and writing bits.

Member Function Documentation

◆ Allocate()

NetBitBuffer * Allocate ( int group,
int size )
static

Allocates a new NetBitBuffer with the specified group and size.

Parameters
groupThe group identifier for the buffer.
sizeThe size of the buffer in bytes.
Returns
A pointer to the allocated NetBitBuffer.

◆ CanRead()

bool CanRead ( int bits)

Checks if the specified number of bits can be read from the buffer.

Parameters
bitsThe number of bits to check.
Returns
true if the specified number of bits can be read; otherwise, false.

◆ CanReadUInt32VarLength()

bool CanReadUInt32VarLength ( )

Reads a 32-bit unsigned integer value with variable length from the buffer.

Returns
The 32-bit unsigned integer value read from the buffer.

◆ CanWrite()

bool CanWrite ( int bits)

Checks if the specified number of bits can be written to the buffer.

Parameters
bitsThe number of bits to check.
Returns
true if the specified number of bits can be written; otherwise, false.

◆ CheckBitCount()

bool CheckBitCount ( int count)

Checks if the specified number of bits can be read or written without exceeding the buffer length.

Parameters
countThe number of bits to check.
Returns
true if the specified number of bits can be read or written; otherwise, false.

◆ Dump()

string Dump ( NetBitBuffer * buffer,
int columns = 16 )
static

Dumps the buffer to a hex string.

Parameters
buffer
columns
Returns

◆ GetDataPointer()

byte * GetDataPointer ( )

Gets a pointer to the current data position in the buffer.

Returns
A pointer to the current data position in the buffer.

◆ GetOffset()

Offset GetOffset ( NetBitBuffer * buffer)
static

Gets the current offset of the specified buffer.

Parameters
bufferThe buffer to get the offset from.
Returns
An Offset struct representing the current offset.

◆ PadToByteBoundaryAndGetPtr()

byte * PadToByteBoundaryAndGetPtr ( )

Pads the buffer to the next byte boundary and returns a pointer to the current data position.

Returns
A pointer to the current data position in the buffer after padding to the byte boundary.

◆ PeekBoolean()

bool PeekBoolean ( )

Peeks at the next boolean value in the buffer without advancing the read position.

Returns
The boolean value peeked from the buffer.

◆ ReadBoolean()

bool ReadBoolean ( )

Reads a boolean value from the buffer.

Returns
The boolean value read from the buffer.

◆ ReadByte()

byte ReadByte ( int bits = 8)

Reads a byte value from the buffer.

Parameters
bitsThe number of bits to read. Default is 8.
Returns
The byte value read from the buffer.

◆ ReadBytesAligned() [1/3]

void ReadBytesAligned ( byte[] buffer,
int length )

Reads an array of bytes from the buffer, ensuring byte alignment.

Parameters
bufferThe array to store the read bytes.
lengthThe number of bytes to read.

◆ ReadBytesAligned() [2/3]

void ReadBytesAligned ( Span< byte > buffer)

Reads a block of bytes from the buffer, ensuring byte alignment.

Parameters
bufferA pointer to the block of bytes to store the read data.

◆ ReadBytesAligned() [3/3]

void ReadBytesAligned ( void * buffer,
int length )

Reads a block of bytes from the buffer, ensuring byte alignment.

Parameters
bufferA pointer to the block of bytes to store the read data.
lengthThe number of bytes to read.

◆ ReadDouble()

double ReadDouble ( )

Reads a double-precision floating-point value from the buffer.

Returns
The double-precision floating-point value read from the buffer.

◆ ReadInt16()

short ReadInt16 ( int bits = 16)

Reads a 16-bit signed integer value from the buffer.

Parameters
bitsThe number of bits to read. Default is 16.
Returns
The 16-bit signed integer value read from the buffer.

◆ ReadInt32()

int ReadInt32 ( int bits = 32)

Reads a 32-bit signed integer value from the buffer.

Parameters
bitsThe number of bits to read. Default is 32.
Returns
The 32-bit signed integer value read from the buffer.

◆ ReadInt32VarLength() [1/2]

int ReadInt32VarLength ( )

Reads a 32-bit signed integer value with variable length from the buffer.

Returns
The 32-bit signed integer value read from the buffer.

◆ ReadInt32VarLength() [2/2]

int ReadInt32VarLength ( int blockSize)

Reads a 32-bit signed integer value with variable length from the buffer.

Parameters
blockSizeThe block size in bits.
Returns
The 32-bit signed integer value read from the buffer.

◆ ReadInt64()

long ReadInt64 ( int bits = 64)

Reads a 64-bit signed integer value from the buffer.

Parameters
bitsThe number of bits to read. Default is 64.
Returns
The 64-bit signed integer value read from the buffer.

◆ ReadInt64VarLength()

long ReadInt64VarLength ( int blockSize)

Reads a 64-bit signed integer value with variable length from the buffer.

Parameters
blockSizeThe block size in bits.
Returns
The 64-bit signed integer value read from the buffer.

◆ ReadSingle()

float ReadSingle ( )

Reads a single-precision floating-point value from the buffer.

Returns
The single-precision floating-point value read from the buffer.

◆ ReadString() [1/2]

string ReadString ( )

Reads a string from the buffer using UTF-8 encoding.

Returns
The string read from the buffer.

◆ ReadString() [2/2]

string ReadString ( Encoding encoding)

Reads a string from the buffer using the specified encoding.

Parameters
encodingThe encoding to use.
Returns
The string read from the buffer.

◆ ReadUInt16()

ushort ReadUInt16 ( int bits = 16)

Reads a 16-bit unsigned integer value from the buffer.

Parameters
bitsThe number of bits to read. Default is 16.
Returns
The 16-bit unsigned integer value read from the buffer.

◆ ReadUInt32()

uint ReadUInt32 ( int bits = 32)

Reads a 32-bit unsigned integer value from the buffer.

Parameters
bitsThe number of bits to read. Default is 32.
Returns
The 32-bit unsigned integer value read from the buffer.

◆ ReadUInt32VarLength() [1/2]

uint ReadUInt32VarLength ( )

Reads a 32-bit unsigned integer value with variable length from the buffer.

Returns
The 32-bit unsigned integer value read from the buffer.

◆ ReadUInt32VarLength() [2/2]

uint ReadUInt32VarLength ( int blockSize)

Reads a 32-bit unsigned integer value with variable length from the buffer.

Parameters
blockSizeThe block size in bits.
Returns
The 32-bit unsigned integer value read from the buffer.

◆ ReadUInt64()

ulong ReadUInt64 ( int bits = 64)

Reads a 64-bit unsigned integer value from the buffer.

Parameters
bitsThe number of bits to read. Default is 64.
Returns
The 64-bit unsigned integer value read from the buffer.

◆ ReadUInt64VarLength()

ulong ReadUInt64VarLength ( int blockSize)

Reads a 64-bit unsigned integer value with variable length from the buffer.

Parameters
blockSizeThe block size in bits.
Returns
The 64-bit unsigned integer value read from the buffer.

◆ Release()

void Release ( NetBitBuffer * buffer)
static

Releases the specified buffer.

Parameters
bufferThe buffer to release.

◆ ReleaseRef()

void ReleaseRef ( ref NetBitBuffer * buffer)
static

Releases the reference to the specified buffer and sets it to null.

Parameters
bufferA reference to the buffer to release.

◆ ReplaceDataFromBlockWithTemp()

void ReplaceDataFromBlockWithTemp ( int tempSize)

Replaces the current data block with a temporary block of the specified size.

Parameters
tempSizeThe size of the temporary block in bytes.

◆ SetBufferLengthBytes()

void SetBufferLengthBytes ( ulong * buffer,
int lenghtInBytes )

Sets the buffer length in bytes and updates the internal length in bits.

Parameters
bufferA pointer to the buffer.
lenghtInBytesThe length of the buffer in bytes.

◆ TryReadInt32VarLength()

bool TryReadInt32VarLength ( out int result)
Parameters
result
Returns

◆ TryReadUInt32()

bool TryReadUInt32 ( out uint result)

Tries to read a 32-bit unsigned integer value from the buffer.

Parameters
resultWhen this method returns, contains the 32-bit unsigned integer value read from the buffer if successful, or 0 if unsuccessful.
Returns
true if the value was successfully read; otherwise, false.

◆ TryReadUInt32BoolPrefixed()

bool? TryReadUInt32BoolPrefixed ( out uint result)

Attempts to read a boolean prefixed 32-bit unsigned integer value from the buffer.

Parameters
resultWhen this method returns, contains the 32-bit unsigned integer value read from the buffer if successful, or 0 if unsuccessful.
Returns
true if the value was successfully read. false if there was not enough data in the buffer. null if the boolean prefix was false.

◆ TryReadUInt32VarLength() [1/2]

bool TryReadUInt32VarLength ( int blockSize,
out uint result )

Reads a 32-bit unsigned integer value with variable length from the buffer.

Parameters
blockSizeThe block size in bits.
resultThe 32-bit unsigned integer value read from the buffer.
Returns

◆ TryReadUInt32VarLength() [2/2]

bool TryReadUInt32VarLength ( out uint result)

Reads a 32-bit unsigned integer value with variable length from the buffer.

Parameters
resultThe 32-bit unsigned integer value read from the buffer.
Returns

◆ TryReadUInt64VarLength()

bool TryReadUInt64VarLength ( int blockSize,
out ulong result )
Parameters
blockSize
result
Returns

◆ Write()

void Write ( ulong value,
int bits )

Writes a value to the buffer with a specified number of bits.

Parameters
valueThe value to write.
bitsThe number of bits to write.

◆ WriteBoolean()

bool WriteBoolean ( bool value)

Writes a boolean value to the buffer.

Parameters
valueThe boolean value to write.
Returns
The boolean value that was written.

Implements INetBitWriteStream.

◆ WriteByte()

void WriteByte ( byte value,
int bits = 8 )

Writes a byte value to the buffer.

Parameters
valueThe byte value to write.
bitsThe number of bits to write. Default is 8.

◆ WriteBytesAligned() [1/3]

void WriteBytesAligned ( byte[] buffer,
int length )

Writes an array of bytes to the buffer, ensuring byte alignment.

Parameters
bufferThe array of bytes to write.
lengthThe number of bytes to write.

◆ WriteBytesAligned() [2/3]

void WriteBytesAligned ( Span< byte > buffer)

Writes a block of bytes to the buffer, ensuring byte alignment.

Parameters
bufferA pointer to the block of bytes to write.

Implements INetBitWriteStream.

◆ WriteBytesAligned() [3/3]

void WriteBytesAligned ( void * buffer,
int length )

Writes a block of bytes to the buffer, ensuring byte alignment.

Parameters
bufferA pointer to the block of bytes to write.
lengthThe number of bytes to write.

Implements INetBitWriteStream.

◆ WriteDouble()

void WriteDouble ( double value)

Writes a double-precision floating-point value to the buffer.

Parameters
valueThe double-precision floating-point value to write.

◆ WriteInt16()

void WriteInt16 ( short value,
int bits = 16 )

Writes a 16-bit signed integer value to the buffer.

Parameters
valueThe 16-bit signed integer value to write.
bitsThe number of bits to write. Default is 16.

◆ WriteInt32()

void WriteInt32 ( int value,
int bits = 32 )

Writes a 32-bit signed integer value to the buffer.

Parameters
valueThe 32-bit signed integer value to write.
bitsThe number of bits to write. Default is 32.

Implements INetBitWriteStream.

◆ WriteInt32AtOffset()

void WriteInt32AtOffset ( int value,
int offset,
int bits )

Writes a 32-bit integer value at a specified offset in the buffer.

Parameters
valueThe 32-bit integer value to write.
offsetThe offset in bits where the value should be written.
bitsThe number of bits to write.

◆ WriteInt32VarLength() [1/2]

void WriteInt32VarLength ( int value)

Writes a 32-bit signed integer value with variable length to the buffer.

Parameters
valueThe 32-bit signed integer value to write.

Implements INetBitWriteStream.

◆ WriteInt32VarLength() [2/2]

void WriteInt32VarLength ( int value,
int blockSize )

Writes a 32-bit signed integer value with variable length to the buffer.

Parameters
valueThe 32-bit signed integer value to write.
blockSizeThe block size in bits.

Implements INetBitWriteStream.

◆ WriteInt64()

void WriteInt64 ( long value,
int bits = 64 )

Writes a 64-bit signed integer value to the buffer.

Parameters
valueThe 64-bit signed integer value to write.
bitsThe number of bits to write. Default is 64.

◆ WriteInt64VarLength()

void WriteInt64VarLength ( long value,
int blockSize )

Writes a 64-bit signed integer value with variable length to the buffer.

Parameters
valueThe 64-bit signed integer value to write.
blockSizeThe block size in bits.

◆ WriteSingle()

void WriteSingle ( float value)

Writes a single-precision floating-point value to the buffer.

Parameters
valueThe single-precision floating-point value to write.

◆ WriteSlow()

void WriteSlow ( ulong value,
int bits )

Writes a value to the buffer with a specified number of bits, handling cases where the value spans multiple words.

Parameters
valueThe value to write.
bitsThe number of bits to write.

◆ WriteString() [1/2]

void WriteString ( string value)

Writes a string to the buffer using UTF-8 encoding.

Parameters
valueThe string to write.

◆ WriteString() [2/2]

void WriteString ( string value,
Encoding encoding )

Writes a string to the buffer using the specified encoding.

Parameters
valueThe string to write.
encodingThe encoding to use.

◆ WriteUInt16()

void WriteUInt16 ( ushort value,
int bits = 16 )

Writes a 16-bit unsigned integer value to the buffer.

Parameters
valueThe 16-bit unsigned integer value to write.
bitsThe number of bits to write. Default is 16.

◆ WriteUInt32()

void WriteUInt32 ( uint value,
int bits = 32 )

Writes a 32-bit unsigned integer value to the buffer.

Parameters
valueThe 32-bit unsigned integer value to write.
bitsThe number of bits to write. Default is 32.

◆ WriteUInt32VarLength() [1/2]

void WriteUInt32VarLength ( uint value)

Writes a 32-bit unsigned integer value with variable length to the buffer.

Parameters
valueThe 32-bit unsigned integer value to write.

◆ WriteUInt32VarLength() [2/2]

void WriteUInt32VarLength ( uint value,
int blockSize )

Writes a 32-bit unsigned integer value with variable length to the buffer.

Parameters
valueThe 32-bit unsigned integer value to write.
blockSizeThe block size in bits.

◆ WriteUInt64()

void WriteUInt64 ( ulong value,
int bits = 64 )

Writes a 64-bit unsigned integer value to the buffer.

Parameters
valueThe 64-bit unsigned integer value to write.
bitsThe number of bits to write. Default is 64.

◆ WriteUInt64AtOffset()

void WriteUInt64AtOffset ( ulong value,
int offset,
int bits )

Writes a 64-bit unsigned integer value at a specified offset in the buffer.

Parameters
valueThe 64-bit unsigned integer value to write.
offsetThe offset in bits where the value should be written.
bitsThe number of bits to write.

◆ WriteUInt64VarLength()

void WriteUInt64VarLength ( ulong value,
int blockSize )

Writes a 64-bit unsigned integer value with variable length to the buffer.

Parameters
valueThe 64-bit unsigned integer value to write.
blockSizeThe block size in bits.

Implements INetBitWriteStream.