Proper way to use FastBufferWriter, can't find much in doc.

I’m trying to figure out what is the best way to use FastBufferWriter for network message, especially for network refresh that can happen each network tick. Ideally I would he able to reuse the same buffer multiple times.

I know there is Allocator.Temp and Allocator.Persitant, but nothing is explained about how they each work internally.

  1. If i declare a class member as FastBufferWriter Persistent does that mean i can reuse it each refresh?

  2. Do i need to call Dispose on it before rewriting or would that clear the memory? If not then how do i reset the writer for writing without reallocating memory (I would always send the same data size).

  3. Then if I use persistent does that mean I need to manually Dispose it when the class holding the FastBufferWriter get deleted or it will memory leak? Is OnDestroy the best place to do that?

  4. If I use a Temp allocator does that mean i never need to call Dispose? And when does it get cleared?

  5. Finally is there a fast way to copy a FastBufferReader into a FastBufferWriter, i see no function to create a new writer based on a reader that would be so useful for forwarding msg received by server to another client.

1 Like

I would also be interested in answers to these questions.

1 Like

So I figured out 4 and 5. But I’m still not using Persistant as I have no idea how it works. Would like an answer to my questions 1-3.

#4 For Temp, you DO need to call Dispose after the msg is sent.

#5 This is how i forward a msg (convert Reader to Writer)

public void Forward(string type, ulong target, FastBufferReader reader, NetworkDelivery delivery)
{
        reader.Seek(0); //Reset reader
        reader.ReadValueSafe(out ulong header); //Ignore header
        byte[] bytes = new byte[reader.Length - reader.Position];
        reader.ReadBytesSafe(ref bytes, reader.Length - reader.Position);
        FastBufferWriter writer = new FastBufferWriter(bytes.Length, Allocator.Temp);
        writer.WriteBytesSafe(bytes, bytes.Length);
        network.NetworkManager.CustomMessagingManager.SendNamedMessage(type, target, writer, delivery);
        writer.Dispose();
}

A built-in function to do this would probably be ideal though so you don’t need so much allocation to do it.

1 Like

Thanks for sharing! Small note: You could use a using statement so that the writer is automatically disposed when the method is exited.

    public void Forward(string type, ulong target, FastBufferReader reader, NetworkDelivery delivery)
    {
        reader.Seek(0); //Reset reader
        reader.ReadValueSafe(out ulong header); //Ignore header
        byte[] bytes = new byte[reader.Length - reader.Position];
        reader.ReadBytesSafe(ref bytes, reader.Length - reader.Position);
        using FastBufferWriter writer = new FastBufferWriter(bytes.Length, Allocator.Temp);
        writer.WriteBytesSafe(bytes, bytes.Length);
        network.NetworkManager.CustomMessagingManager.SendNamedMessage(type, target, writer, delivery);
    }