Struct DataStreamWriter | Unity Transport | 0.2.4-preview.0
docs.unity3d.com
    Show / Hide Table of Contents

    Struct DataStreamWriter

    Data streams can be used to serialize data over the network. The DataStreamWriter and DataStreamReader classes work together to serialize data for sending and then to deserialize when receiving.

    Namespace: Unity.Networking.Transport
    Syntax
    public struct DataStreamWriter : IDisposable
    Remarks

    The reader can be used to deserialize the data from a writer, writing data to a writer and reading it back can be done like this:

    using (var dataWriter = new DataStreamWriter(16, Allocator.Persistent))
    {
        dataWriter.Write(42);
        dataWriter.Write(1234);
        // Length is the actual amount of data inside the writer,
        // Capacity is the total amount.
        var dataReader = new DataStreamReader(dataWriter, 0, dataWriter.Length);
        var context = default(DataStreamReader.Context);
        var myFirstInt = dataReader.ReadInt(ref context);
        var mySecondInt = dataReader.ReadInt(ref context);
    }

    The writer needs to be Disposed (here done by wrapping usage in using statement) because it uses native memory which needs to be freed.

    There are a number of functions for various data types. Each write call returns a Deferred* variant for that particular type and this can be used as a marker to overwrite the data later on, this is particularly useful when the size of the data is written at the start and you want to write it at the end when you know the value.

    using (var data = new DataStreamWriter(16, Allocator.Persistent))
    {
        // My header data
        var headerSizeMark = data.Write((ushort)0);
        var payloadSizeMark = data.Write((ushort)0);
        data.Write(42);
        data.Write(1234);
        var headerSize = data.Length;
        // Update header size to correct value
        headerSizeMark.Update((ushort)headerSize);
        // My payload data
        byte[] someBytes = Encoding.ASCII.GetBytes("some string");
        data.Write(someBytes, someBytes.Length);
        // Update payload size to correct value
        payloadSizeMark.Update((ushort)(data.Length - headerSize));
    }

    It's possible to get a more direct access to the buffer inside the reader/writer, in an unsafe way. See DataStreamUnsafeUtility

    Constructors

    DataStreamWriter(Int32, Allocator)

    Declaration
    public DataStreamWriter(int capacity, Allocator allocator)
    Parameters
    Type Name Description
    Int32 capacity
    Allocator allocator

    Properties

    Capacity

    The total size of the data buffer, see Length for the size of space used in the buffer. Capacity can be changed after the writer has been created.

    Declaration
    public int Capacity { get; set; }
    Property Value
    Type Description
    Int32
    Exceptions
    Type Condition
    InvalidOperationException

    Thrown if the given capacity is smaller than the current buffer usage.

    IsCreated

    True if there is a valid data buffer present. This would be false if the writer was created with no arguments.

    Declaration
    public bool IsCreated { get; }
    Property Value
    Type Description
    Boolean

    Length

    The size of the buffer used. See Capacity for the total size.

    Declaration
    public int Length { get; }
    Property Value
    Type Description
    Int32

    LengthInBits

    The size of the buffer used in bits. See Length for the length in bytes.

    Declaration
    public int LengthInBits { get; }
    Property Value
    Type Description
    Int32

    Methods

    Clear()

    Moves the write position to the start of the data buffer used.

    Declaration
    public void Clear()

    CopyTo(Int32, Int32, NativeArray<Byte>)

    Copy data from the writer to the given NativeArray, the data size (start to length) must not exceed the total size of the array or an exception will be thrown.

    Declaration
    public void CopyTo(int start, int length, NativeArray<byte> dest)
    Parameters
    Type Name Description
    Int32 start
    Int32 length
    NativeArray<Byte> dest

    CopyTo(Int32, Int32, ref Byte[])

    Copy data from the writer to the given managed byte array, the data size (start to length) must not exceed the total size of the byte array or an exception will be thrown.

    Declaration
    public void CopyTo(int start, int length, ref byte[] dest)
    Parameters
    Type Name Description
    Int32 start
    Int32 length
    Byte[] dest

    Dispose()

    The writer uses unmanaged memory for its data buffer. Dispose needs to be called to free this resource.

    Declaration
    public void Dispose()

    Flush()

    Declaration
    public void Flush()

    GetNativeSlice(Int32, Int32)

    Create a NativeSlice with access to the raw data in the writer, the data size (start to length) must not exceed the total size of the array or an exception will be thrown.

    Declaration
    public NativeSlice<byte> GetNativeSlice(int start, int length)
    Parameters
    Type Name Description
    Int32 start
    Int32 length
    Returns
    Type Description
    NativeSlice<Byte>

    Write(Byte)

    Declaration
    public DataStreamWriter.DeferredByte Write(byte value)
    Parameters
    Type Name Description
    Byte value
    Returns
    Type Description
    DataStreamWriter.DeferredByte

    Write(Byte[], Int32)

    Copy byte array into the writers data buffer, up to the given length or the complete size if no length (or -1) is given.

    Declaration
    public void Write(byte[] value, int length = -1)
    Parameters
    Type Name Description
    Byte[] value

    Source byte array

    Int32 length

    Length to copy, omit this to copy all the byte array

    Write(Int16)

    Declaration
    public DataStreamWriter.DeferredShort Write(short value)
    Parameters
    Type Name Description
    Int16 value
    Returns
    Type Description
    DataStreamWriter.DeferredShort

    Write(Int32)

    Declaration
    public DataStreamWriter.DeferredInt Write(int value)
    Parameters
    Type Name Description
    Int32 value
    Returns
    Type Description
    DataStreamWriter.DeferredInt

    Write(Single)

    Declaration
    public DataStreamWriter.DeferredFloat Write(float value)
    Parameters
    Type Name Description
    Single value
    Returns
    Type Description
    DataStreamWriter.DeferredFloat

    Write(UInt16)

    Declaration
    public DataStreamWriter.DeferredUShort Write(ushort value)
    Parameters
    Type Name Description
    UInt16 value
    Returns
    Type Description
    DataStreamWriter.DeferredUShort

    Write(UInt32)

    Declaration
    public DataStreamWriter.DeferredUInt Write(uint value)
    Parameters
    Type Name Description
    UInt32 value
    Returns
    Type Description
    DataStreamWriter.DeferredUInt

    Write(UInt64)

    Declaration
    public void Write(ulong value)
    Parameters
    Type Name Description
    UInt64 value

    WriteBytes(Byte*, Int32)

    Declaration
    public void WriteBytes(byte *data, int bytes)
    Parameters
    Type Name Description
    Byte* data
    Int32 bytes

    WriteNetworkByteOrder(Int16)

    Declaration
    public DataStreamWriter.DeferredShortNetworkByteOrder WriteNetworkByteOrder(short value)
    Parameters
    Type Name Description
    Int16 value
    Returns
    Type Description
    DataStreamWriter.DeferredShortNetworkByteOrder

    WriteNetworkByteOrder(Int32)

    Declaration
    public DataStreamWriter.DeferredIntNetworkByteOrder WriteNetworkByteOrder(int value)
    Parameters
    Type Name Description
    Int32 value
    Returns
    Type Description
    DataStreamWriter.DeferredIntNetworkByteOrder

    WriteNetworkByteOrder(UInt16)

    Declaration
    public DataStreamWriter.DeferredUShortNetworkByteOrder WriteNetworkByteOrder(ushort value)
    Parameters
    Type Name Description
    UInt16 value
    Returns
    Type Description
    DataStreamWriter.DeferredUShortNetworkByteOrder

    WriteNetworkByteOrder(UInt32)

    Declaration
    public DataStreamWriter.DeferredUIntNetworkByteOrder WriteNetworkByteOrder(uint value)
    Parameters
    Type Name Description
    UInt32 value
    Returns
    Type Description
    DataStreamWriter.DeferredUIntNetworkByteOrder

    WritePackedFloat(Single, NetworkCompressionModel)

    Declaration
    public void WritePackedFloat(float value, NetworkCompressionModel model)
    Parameters
    Type Name Description
    Single value
    NetworkCompressionModel model

    WritePackedFloatDelta(Single, Single, NetworkCompressionModel)

    Declaration
    public void WritePackedFloatDelta(float value, float baseline, NetworkCompressionModel model)
    Parameters
    Type Name Description
    Single value
    Single baseline
    NetworkCompressionModel model

    WritePackedInt(Int32, NetworkCompressionModel)

    Declaration
    public void WritePackedInt(int value, NetworkCompressionModel model)
    Parameters
    Type Name Description
    Int32 value
    NetworkCompressionModel model

    WritePackedIntDelta(Int32, Int32, NetworkCompressionModel)

    Declaration
    public void WritePackedIntDelta(int value, int baseline, NetworkCompressionModel model)
    Parameters
    Type Name Description
    Int32 value
    Int32 baseline
    NetworkCompressionModel model

    WritePackedStringDelta(NativeString64, NativeString64, NetworkCompressionModel)

    Declaration
    public void WritePackedStringDelta(NativeString64 str, NativeString64 baseline, NetworkCompressionModel model)
    Parameters
    Type Name Description
    NativeString64 str
    NativeString64 baseline
    NetworkCompressionModel model

    WritePackedUInt(UInt32, NetworkCompressionModel)

    Declaration
    public void WritePackedUInt(uint value, NetworkCompressionModel model)
    Parameters
    Type Name Description
    UInt32 value
    NetworkCompressionModel model

    WritePackedUIntDelta(UInt32, UInt32, NetworkCompressionModel)

    Declaration
    public void WritePackedUIntDelta(uint value, uint baseline, NetworkCompressionModel model)
    Parameters
    Type Name Description
    UInt32 value
    UInt32 baseline
    NetworkCompressionModel model

    WriteString(NativeString64)

    Declaration
    public void WriteString(NativeString64 str)
    Parameters
    Type Name Description
    NativeString64 str

    Extension Methods

    DataStreamUnsafeUtility.GetUnsafePtr(DataStreamWriter)
    DataStreamUnsafeUtility.GetUnsafeReadOnlyPtr(DataStreamWriter)
    DataStreamUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(DataStreamWriter)
    DataStreamUnsafeUtility.WriteBytesWithUnsafePointer(DataStreamWriter, Int32)
    In This Article
    • Constructors
      • DataStreamWriter(Int32, Allocator)
    • Properties
      • Capacity
      • IsCreated
      • Length
      • LengthInBits
    • Methods
      • Clear()
      • CopyTo(Int32, Int32, NativeArray<Byte>)
      • CopyTo(Int32, Int32, ref Byte[])
      • Dispose()
      • Flush()
      • GetNativeSlice(Int32, Int32)
      • Write(Byte)
      • Write(Byte[], Int32)
      • Write(Int16)
      • Write(Int32)
      • Write(Single)
      • Write(UInt16)
      • Write(UInt32)
      • Write(UInt64)
      • WriteBytes(Byte*, Int32)
      • WriteNetworkByteOrder(Int16)
      • WriteNetworkByteOrder(Int32)
      • WriteNetworkByteOrder(UInt16)
      • WriteNetworkByteOrder(UInt32)
      • WritePackedFloat(Single, NetworkCompressionModel)
      • WritePackedFloatDelta(Single, Single, NetworkCompressionModel)
      • WritePackedInt(Int32, NetworkCompressionModel)
      • WritePackedIntDelta(Int32, Int32, NetworkCompressionModel)
      • WritePackedStringDelta(NativeString64, NativeString64, NetworkCompressionModel)
      • WritePackedUInt(UInt32, NetworkCompressionModel)
      • WritePackedUIntDelta(UInt32, UInt32, NetworkCompressionModel)
      • WriteString(NativeString64)
    • Extension Methods
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023