#Rewind DataStreamWriter

1 messages · Page 1 of 1 (latest)

dreamy quiver
#

Is it possible to rewind data stream writer?
I`m trying to do a code like this:

    {
        Assert.IsTrue(peer.Connection.IsCreated);

        DataStreamWriter writer = default(DataStreamWriter);

        for (int it = 0; it < peer.AreaOfInterest.Length; ++it)
        {
            ref NetworkedPeerView view = ref UnsafeUtility.ArrayElementAsRef<NetworkedPeerView>(peer.AreaOfInterest.GetUnsafePtr(), it);

            if (!writer.IsCreated)
            {
                NetworkDriver.BeginSend(peer.Connection, out writer);
            }

            int writePosition = writer.LengthInBits;

            /* TryWrite Entity to buffer*/

            if (writer.HasFailedWrites)
            {
                writer.Rewind(writePosition);
                writer.HasFailedWrites = false;

                NetworkDriver.EndSend(writer);

                NetworkDriver.BeginSend(peer.Connection, out writer);

                /* TryWrite Entity to new buffer */
            }
        }

        Debug.Log(peer.AreaOfInterest.Length);
    }```

I need to rewind to last position and remove failed writes if an entity fails to write to buffer, allocate another buffer and try again
#

How do i remove the HasFailedWrites flag and rewind position on buffer?

#

Is there any unsafe code for it

#
    {
        Assert.IsTrue(peer.Connection.IsCreated);

        if (peer.AreaOfInterest.Length > 0)
        {
            DataStreamWriter writer;

            NetworkDriver.BeginSend(peer.Connection, out writer);

            for (int it = 0; it < peer.AreaOfInterest.Length; ++it)
            {
                ref NetworkedPeerView view = ref UnsafeUtility.ArrayElementAsRef<NetworkedPeerView>(peer.AreaOfInterest.GetUnsafePtr(), it);

                DataStreamWriter copy = writer;

                /* TryWrite Entity to buffer*/

                if (writer.HasFailedWrites)
                {
                    writer = copy;

                    NetworkDriver.EndSend(writer);

                    NetworkDriver.BeginSend(peer.Connection, out writer);

                    /* TryWrite Entity to new buffer */
                }
            }

            NetworkDriver.EndSend(writer);
        }

        Debug.Log(peer.AreaOfInterest.Length);
    }```

Is it viable? can i copy DataStreamWriter and restore like this?
olive shoal
#

Copying the DataStreamWriter to a temp variable and restoring it by copying back - like you do int the second example - is the intended way to rewind. It will clear HasFailedWrites and restore the length you had at that point. The data in the buffer will not be touched by writes happening after you copy to the temp variable since it will be in an unused area of the internal buffer.

dreamy quiver
#

Thank you