question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Write past end of buffer error when sending byte[] using SendNamedMessage

See original GitHub issue

Description

This bug is about Custom Messages. When trying to send a byte[] Using SendNamedMessage you get a “Write past end of buffer error”

Reproduce Steps

  1. Write a script that sends a simple byte[] to a client.
  2. The FastBufferArray will write perfectly fine, but The SendNamedMessage will get an “Write past end of buffer error”;

Actual Outcome

SendNamedMessage with a byte[] will give an error

Expected Outcome

The written FastBufferArray will be send without issue

My code:

public void SendChunkMessage(byte[] messageContent, ulong clientID)
    {
        int writeSize = FastBufferWriter.GetWriteSize(messageContent);
        FastBufferWriter writer = new FastBufferWriter(writeSize, Allocator.Temp);
        CustomMessagingManager customMessagingManager = NetworkManager.CustomMessagingManager;
        using (writer)
        {
            writer.TryBeginWrite(writeSize);
            writer.WriteBytes(messageContent, writeSize, 0);
            Debug.Log("Written to FastBufferWriter");
            if (IsServer)
            {
                customMessagingManager.SendNamedMessage("Chunk", clientID, writer);
            }
        }
        Debug.Log("Send to client");
    }

My error: image

Environment

  • OS: Windows 10 pro
  • Unity Version: 2022.2.0b16
  • Netcode Version: 1.1.0

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ShadauxCatcommented, May 1, 2023

Very belated response… Sorry about that.

The default value of -1 is not actually intended to be an “infinite growth” value. Note the documentation:

Maximum size the buffer can grow to. If less than size, buffer cannot grow.

The default of -1 is always going to be less than the size. Constructing without a third argument isn’t intended to create an infinite-growth buffer - the default behavior is a non-growing buffer that won’t exceed the size you give it.

0reactions
TheCaveOfWonderscommented, Feb 4, 2023

(at least I believe that is what the issue is…or it is a bug and the default -1 doesn’t automatically adjust)

@NoelStephensUnity I believe the default -1 is bugged and doesn’t automatically adjust the size.

It does adjust the size (this is pretty thoroughly tested both by unit tests and by usage throughout the SDK) but only up to a set maximum. If you’re sending with Unreliable delivery, the max is 1300 bytes, because unreliable delivery, at the transport and even kernel level, has to fit into a single UDP datagram. FastBufferWriter won’t expand beyond that size because the delivery method won’t allow it - if it did, what would happen is that instead of the error you’re seeing, you’d see a more confusing error later on, or possibly your message would be silently lost as the network wouldn’t be able to deliver it.

As Noel said, if you switch to ReliableFragmentedSequenced, this should allow the buffer to grow large enough to fit your data.

Oh sorry my comment was unrelated to this actual issue, it was specifically about the default -1 value. Just tested it in my project when I saw Noel’s message, I tried it again now just to see (in case it was bugged before cause it had never worked for me).

This works fine (the size of values is 16)

  using (FastBufferWriter writer = new FastBufferWriter(8, Allocator.Temp, 256))
                {
                    writer.WriteValueSafe(values);
                    NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage(SharedConstants.Network.Tech.SyncTech, senderClientId, writer);
                }

This won’t work and will throw a “Write past end of buffer” exception. (the size of values is 16)

  using (FastBufferWriter writer = new FastBufferWriter(8, Allocator.Temp))
                {
                    writer.WriteValueSafe(values);
                    NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage(SharedConstants.Network.Tech.SyncTech, senderClientId, writer);
                }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue sending dynamic data. Possibly bad design on my ...
OverflowException: Writing past the end of the buffer. Here's my code: Code (CSharp):. public void SendNamedMessage(CustomNamedMessage ...
Read more >
Error buffer_update: Attempted to write buffer (24576 bytes) ...
Issue description. E 0:00:02:0352 buffer_update: Attempted to write buffer (24576 bytes) past the end. <C++ 错误> Condition ...
Read more >
How to send a byte[] with buffer to a server?
I try to send a byte[] with an ssl socket and a buffer to my server, but doesn't succeed the buffer. I search...
Read more >
Attempting to read from outside the buffer error when high ...
Hi, I am making a multiplayer game which works perfectly fine 95% of the time however occasionally the game will start having higher...
Read more >
Struct FastBufferWriter | Netcode for GameObjects | 1.0.2
Returns an array representation of the underlying byte buffer. ... Instead, attempting to write past the marked position in release builds ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found