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

    Class DataStreamUnsafeUtility

    DataStream (Reader/Writer) unsafe utilities used to do pointer operations on streams.

    These are added to the DataStreamWriter/DataStreamReader classes as extensions, so you need to add using Unity.Collections.LowLevel.Unsafe at the top of file where you need to access these functions.

    Since these are unsafe C# operations care must be taken when using them, it can easily crash the editor/player.

    Every time data is written directly to the data stream buffer you must call WriteBytesWithUnsafePointer afterwards with the length of the data written so that the stream class can internally keep track of how much of the internal buffer has been written to.

    The functions have read/write access check variants which utilize the job system atomic safety handle. The ENABLE_UNITY_COLLECTIONS_CHECKS define needs to be used for this to work. For more information see Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.

    Example of typical usage:

    // Manually write some numbers into a data stream from a source buffer.
    var data = new DataStreamWriter(4, Allocator.Temp);
    unsafe
    {
        var ptr = data.GetUnsafePtr();
        var sourceData = new NativeArray<byte>(4, Allocator.Temp);
        sourceData[0] = 42;
        sourceData[1] = 42;
        sourceData[2] = 42;
        sourceData[3] = 42;
        UnsafeUtility.MemCpy(ptr, sourceData.GetUnsafePtr(), sourceData.Length);
        data.WriteBytesWithUnsafePointer(sourceData.Length);
    }
    Inheritance
    Object
    DataStreamUnsafeUtility
    Namespace: Unity.Networking.Transport.LowLevel.Unsafe
    Syntax
    public static class DataStreamUnsafeUtility

    Methods

    CreateReaderFromExistingData(Byte*, Int32)

    Declaration
    public static DataStreamReader CreateReaderFromExistingData(byte *data, int length)
    Parameters
    Type Name Description
    Byte* data
    Int32 length
    Returns
    Type Description
    DataStreamReader

    GetUnsafeBufferPointerWithoutChecks(DataStreamWriter)

    Get the byte* pointer to the buffer backing the DataStreamWriter. Does not check the safety handle for read/write access.

    Declaration
    public static byte *GetUnsafeBufferPointerWithoutChecks(this DataStreamWriter strm)
    Parameters
    Type Name Description
    DataStreamWriter strm
    Returns
    Type Description
    Byte*

    Pointer to the data stream buffer.

    GetUnsafePtr(DataStreamWriter)

    Get the byte* pointer to the start of the buffer backing the DataStreamWriter. A safety check is done to see if you have write access to the buffer.

    Declaration
    public static byte *GetUnsafePtr(this DataStreamWriter strm)
    Parameters
    Type Name Description
    DataStreamWriter strm
    Returns
    Type Description
    Byte*

    Pointer to the data stream buffer.

    GetUnsafeReadOnlyPtr(DataStreamReader)

    Get the byte* pointer to the start of the buffer backing the DataStreamReader. A safety check is done to make sure you only have read access to the buffer.

    Declaration
    public static byte *GetUnsafeReadOnlyPtr(this DataStreamReader strm)
    Parameters
    Type Name Description
    DataStreamReader strm
    Returns
    Type Description
    Byte*

    Pointer to the data stream buffer.

    GetUnsafeReadOnlyPtr(DataStreamWriter)

    Get the byte* pointer to the start of the buffer backing the DataStreamWriter. A safety check is done to make sure you only have read access to the buffer.

    Declaration
    public static byte *GetUnsafeReadOnlyPtr(this DataStreamWriter strm)
    Parameters
    Type Name Description
    DataStreamWriter strm
    Returns
    Type Description
    Byte*

    Pointer to the data stream buffer.

    WriteBytesWithUnsafePointer(DataStreamWriter, Int32)

    Signal how many bytes have been written to the buffer used by the data stream using one of the unsafe pointer getters.

    Declaration
    public static void WriteBytesWithUnsafePointer(this DataStreamWriter strm, int length)
    Parameters
    Type Name Description
    DataStreamWriter strm
    Int32 length

    Amount of data written to the buffer.

    Exceptions
    Type Condition
    ArgumentOutOfRangeException

    If the length specified brings the total length to a value higher than the capacity of the buffer.

    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