Class NetworkBuffer
A buffer that can be used at the bit level
Implements
Inherited Members
Namespace: MLAPI.Serialization
Assembly: Unity.Multiplayer.MLAPI.Runtime.dll
Syntax
public class NetworkBuffer : Stream, IDisposable
Constructors
NetworkBuffer()
A buffer that supports writing data smaller than a single byte. This buffer also has a built-in compression algorithm that can (optionally) be used to write compressed data.
Declaration
public NetworkBuffer()
NetworkBuffer(byte[])
A buffer that supports writing data smaller than a single byte. This buffer also has a built-in compression algorithm that can (optionally) be used to write compressed data. NOTE: when using a pre-allocated buffer, the buffer will not grow!
Declaration
public NetworkBuffer(byte[] target)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | target | Pre-allocated buffer to write to |
NetworkBuffer(int)
A buffer that supports writing data smaller than a single byte. This buffer also has a built-in compression algorithm that can (optionally) be used to write compressed data.
Declaration
public NetworkBuffer(int capacity)
Parameters
| Type | Name | Description |
|---|---|---|
| int | capacity |
NetworkBuffer(int, float)
A buffer that supports writing data smaller than a single byte. This buffer also has a built-in compression algorithm that can (optionally) be used to write compressed data.
Declaration
public NetworkBuffer(int capacity, float growthFactor)
Parameters
| Type | Name | Description |
|---|---|---|
| int | capacity | Initial capacity of buffer in bytes. |
| float | growthFactor | Factor by which buffer should grow when necessary. |
NetworkBuffer(float)
A buffer that supports writing data smaller than a single byte. This buffer also has a built-in compression algorithm that can (optionally) be used to write compressed data.
Declaration
public NetworkBuffer(float growthFactor)
Parameters
| Type | Name | Description |
|---|---|---|
| float | growthFactor | Factor by which buffer should grow when necessary. |
Properties
BitAligned
Whether or not the current BitPosition is evenly divisible by 8. I.e. whether or not the BitPosition is at a byte boundary.
Declaration
public bool BitAligned { get; }
Property Value
| Type | Description |
|---|---|
| bool |
BitLength
Length of data (in bits) that is considered to be written to the buffer.
Declaration
public ulong BitLength { get; }
Property Value
| Type | Description |
|---|---|
| ulong |
BitPosition
Bit offset into the buffer that new data will be written to.
Declaration
public ulong BitPosition { get; set; }
Property Value
| Type | Description |
|---|---|
| ulong |
CanRead
Whether or not buffeer supports reading. (Always true)
Declaration
public override bool CanRead { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Overrides
CanSeek
Whether or not seeking is supported by this buffer. (Always true)
Declaration
public override bool CanSeek { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Overrides
CanWrite
Whether or not this buffer can accept new data. NOTE: this will return true even if only fewer than 8 bits can be written!
Declaration
public override bool CanWrite { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Overrides
Capacity
Current buffer size. The buffer will not be resized (if possible) until Position is equal to Capacity and an attempt to write data is made.
Declaration
public long Capacity { get; set; }
Property Value
| Type | Description |
|---|---|
| long |
GrowthFactor
Factor by which buffer should grow when necessary.
Declaration
public float GrowthFactor { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
HasDataToRead
Whether or not or there is any data to be read from the buffer.
Declaration
public bool HasDataToRead { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Length
The current length of data considered to be "written" to the buffer.
Declaration
public override long Length { get; }
Property Value
| Type | Description |
|---|---|
| long |
Overrides
Position
The index that will be written to when any call to write data is made to this buffer.
Declaration
public override long Position { get; set; }
Property Value
| Type | Description |
|---|---|
| long |
Overrides
Resizable
Whether or not the buffer will grow the buffer to accomodate more data.
Declaration
public bool Resizable { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
CopyFrom(NetworkBuffer, int, bool)
Copys the bits from the provided NetworkBuffer
Declaration
public void CopyFrom(NetworkBuffer buffer, int dataCount, bool copyBits)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkBuffer | buffer | The buffer to copy from |
| int | dataCount | The amount of data evel |
| bool | copyBits | Whether or not to copy at the bit level rather than the byte level |
CopyFrom(Stream, int)
Copy data from another stream
Declaration
public void CopyFrom(Stream s, int count = -1)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | s | Stream to copy from |
| int | count | How many bytes to read. Set to value less than one to read until ReadByte returns -1 |
CopyTo(Stream, int)
Copies internal buffer to stream
Declaration
public void CopyTo(Stream stream, int count = -1)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream to copy to |
| int | count | The maximum amount of bytes to copy. Set to value less than one to copy the full length |
CopyUnreadFrom(Stream, int)
Copies urnead bytes from the source stream
Declaration
public void CopyUnreadFrom(Stream s, int count = -1)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | s | The source stream to copy from |
| int | count | The max amount of bytes to copy |
Flush()
Flush buffer. This does nothing since data is written directly to a byte buffer.
Declaration
public override void Flush()
Overrides
GetBuffer()
Get the internal buffer being written to by this buffer.
Declaration
public byte[] GetBuffer()
Returns
| Type | Description |
|---|---|
| byte[] |
PadBuffer()
Writes zeros to fill the last byte
Declaration
public void PadBuffer()
PeekByte()
Peeks a byte without advancing the position
Declaration
public int PeekByte()
Returns
| Type | Description |
|---|---|
| int | The peeked byte |
Read(byte[], int, int)
Read a subset of the buffer buffer and write the contents to the supplied buffer.
Declaration
public override int Read(byte[] buffer, int offset, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | Buffer to copy data to. |
| int | offset | Offset into the buffer to write data to. |
| int | count | How many bytes to attempt to read. |
Returns
| Type | Description |
|---|---|
| int | Amount of bytes read. |
Overrides
ReadBit()
Read a single bit from the buffer.
Declaration
public bool ReadBit()
Returns
| Type | Description |
|---|---|
| bool | A bit in bool format. (True represents 1, False represents 0) |
ReadByte()
Read a byte from the buffer. This takes into account possible byte misalignment.
Declaration
public override int ReadByte()
Returns
| Type | Description |
|---|---|
| int | A byte from the buffer or, if a byte can't be read, -1. |
Overrides
Seek(long, SeekOrigin)
Set position in buffer to read from/write to.
Declaration
public override long Seek(long offset, SeekOrigin origin)
Parameters
| Type | Name | Description |
|---|---|---|
| long | offset | Offset from position origin. |
| SeekOrigin | origin | How to calculate offset. |
Returns
| Type | Description |
|---|---|
| long | The new position in the buffer that data will be written to. |
Overrides
SetLength(long)
Set length of data considered to be "written" to the buffer.
Declaration
public override void SetLength(long value)
Parameters
| Type | Name | Description |
|---|---|---|
| long | value | New length of the written data. |
Overrides
SkipPadBits()
Reads zeros until the the buffer is byte aligned
Declaration
public void SkipPadBits()
ToArray()
Creates a copy of the internal buffer. This only contains the used bytes
Declaration
public byte[] ToArray()
Returns
| Type | Description |
|---|---|
| byte[] | A copy of used bytes in the internal buffer |
ToString()
Returns hex encoded version of the buffer
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | Hex encoded version of the buffer |
Overrides
Write(byte[])
Write data from the given buffer to the internal buffer.
Declaration
public void Write(byte[] buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | Buffer to write from. |
Write(byte[], int, int)
Write data from the given buffer to the internal buffer.
Declaration
public override void Write(byte[] buffer, int offset, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | Buffer to write from. |
| int | offset | Offset in given buffer to start reading from. |
| int | count | Amount of bytes to read copy from given buffer to buffer. |
Overrides
WriteBit(bool)
Write a single bit to the buffer
Declaration
public void WriteBit(bool bit)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | bit | Value of the bit. True represents 1, False represents 0 |
WriteByte(byte)
Write byte value to the internal buffer.
Declaration
public override void WriteByte(byte value)
Parameters
| Type | Name | Description |
|---|---|---|
| byte | value | The byte value to write. |