Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Weird Behaviour with FastBufferReader

Discussion in 'Netcode for GameObjects' started by herrmutig, Jun 21, 2022.

  1. herrmutig

    herrmutig

    Joined:
    Nov 30, 2020
    Posts:
    22
    Hi!

    I was trying out the custom messaging system where I used the FastBufferWriter/Reader to send a json-formatted string to the client.

    The writing Side looks like this:

    Code (CSharp):
    1.        
    2. SendPlayerInformationResult()
    3. {
    4.        string json = JsonUtility.ToJson(playerInformation);
    5.         byte[] bytes = Encoding.ASCII.GetBytes(json);
    6.  
    7.         Debug.Log("Try to send ("+ bytes.Length + "): "  + json);
    8.  
    9.         using var writer = new FastBufferWriter(sizeof(byte) * bytes.Length, Unity.Collections.Allocator.Temp);
    10.      
    11.      
    12.         writer.WriteBytesSafe(bytes);
    13.         NetworkManager
    14.         .Singleton
    15.         .CustomMessagingManager
    16. .SendNamedMessageToAll(nameof(S2CNewPlayerInformationResult_CustomMessage), writer);
    17. }
    18.  

    The Reading side looks like this:

    Code (CSharp):
    1.  
    2. S2CNewPlayerInformationResult_CustomMessage(ulong clientId, FastBufferReader r)
    3. {    
    4.         byte[] bytes = new byte[r.Length - 8 ];
    5.         r.ReadBytesSafe(ref bytes, r.Length - 8);
    6.  
    7.         Debug.Log(r.Length);
    8.  
    9.         string json = Encoding.ASCII.GetString(bytes);
    10. }
    11.  
    Now my question:
    If you check the Reading Side you'll see that I wrote "r.ReadBytesSafe(ref bytes, r.Length - 8);"
    I did this because for some reason the FastBufferReader adds an additonal length of 8 to the actual string defined in the writing side.

    Respectively when I send data with 100 characters, the reading side will have 108. If I do not remove those addtional 8 I will get an Overflow Exception. If I remove them then it works as it should.

    Is there a reason behind that behaviour or just buggy or am I doing something completely wrong? :D.

    Thanks for your replies! If something is unclear, let me know. Thank you!
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    When you write something variable size like a string you can't rely on the length of the FastBufferReader because Netcode for GameObjects writes other meta information into that reader. Instead first write the length of the string into the writer and then on the receiving side read the length and after that read that many bytes.
     
  3. herrmutig

    herrmutig

    Joined:
    Nov 30, 2020
    Posts:
    22
    Hi Luke!

    Thanks for the fast reply.

    What if you don't know beforehand how long the string is going to be? For example sending some information only the server knows, but clients need to get informed about that.

    On such a case is it better to define a fixed length for the buffer?
     
  4. IndieMarc

    IndieMarc

    Joined:
    Jan 16, 2017
    Posts:
    183
    Would be great to update the documentation on this, as no where it explains how FastBufferReader and Writer are structured, I was just trying to copy a FastBufferReader into a FastBufferWriter and it's really unclear how to do this.

    This code seems to work:
    Code (CSharp):
    1. reader.Seek(0); //Reset reader
    2. reader.ReadValueSafe(out ulong hash); //Read header
    3. byte[] bytes = new byte[reader.Length - 8]; //Header is ulong (8 bytes)
    4. reader.ReadBytesSafe(ref bytes, reader.Length - 8); //Header is ulong (8 bytes)
    5. FastBufferWriter writer = new FastBufferWriter(bytes.Length, Allocator.Temp);
    6. writer.WriteBytesSafe(bytes, bytes.Length);
    But this doesn't, even though in theory it's supposed to be the same

    Code (CSharp):
    1. reader.Seek(0); //Reset reader
    2. byte[] bytes = new byte[reader.Length - 8]; //Header is ulong (8 bytes)
    3. reader.ReadBytesSafe(ref bytes, reader.Length - 8, 8); //Header is ulong (8 bytes), offset is also 8 to skip header
    4. FastBufferWriter writer = new FastBufferWriter(bytes.Length, Allocator.Temp);
    5. writer.WriteBytesSafe(bytes, bytes.Length);
    So there is something unclear about how these work. Would be really good to have some doc on this.

    Is the header for readerbuffer always a ulong ?
     
    Last edited: Aug 17, 2022
  5. wechat_os_Qy0xkipWLGYk6K41qQOqJGO8s

    wechat_os_Qy0xkipWLGYk6K41qQOqJGO8s

    Joined:
    May 24, 2023
    Posts:
    8
    i want use easy API to send custom messsage ……
     
  6. wechat_os_Qy0xkipWLGYk6K41qQOqJGO8s

    wechat_os_Qy0xkipWLGYk6K41qQOqJGO8s

    Joined:
    May 24, 2023
    Posts:
    8
    i dont know how to use fastBuffferwrite and reader …… ,how it work……


    i hope like this

    A a = new A();
    FastBufferWriter bw;
    bw.WriteValue<A>(a);

    manager.CustomMessagingManager.SendNamedMessage("CustomMessaging", NetworkManager.ServerClientId, bw, NetworkDelivery.Reliable);


    [System.Serializable]
    public class A
    {
    public float s = 23;

    public string bb1 = "123asdfasdfaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    public string bb2 = "23445";

    public string bb3 = "ertwer";

    public string bb4 = "tyru5";

    }
     
  7. wechat_os_Qy0xkipWLGYk6K41qQOqJGO8s

    wechat_os_Qy0xkipWLGYk6K41qQOqJGO8s

    Joined:
    May 24, 2023
    Posts:
    8
    Net code is too young

    Perhaps the API is not concise enough,

    Or examples without an introduction,

    Or maybe I'm not smart enough,

    For some examples or issues, Google cannot help me

    A lowly programmer from China
     
  8. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
  9. IndieMarc

    IndieMarc

    Joined:
    Jan 16, 2017
    Posts:
    183
    I made a free asset to help get started with Netcode. Many people told me this asset really helped them because i created a lot of premade functions and examples to help with stuff that aren't clear.

    https://assetstore.unity.com/packag...-plus-multiplayer-networking-and-demos-248538
     
  10. wechat_os_Qy0xkipWLGYk6K41qQOqJGO8s

    wechat_os_Qy0xkipWLGYk6K41qQOqJGO8s

    Joined:
    May 24, 2023
    Posts:
    8

    sometime
    i want to send custome message to client or server

    like login or register send some data to server or ger some data form server
    not rpc
    rpc can send single id to client data

    https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/message-system/rpc-params/


    i think login custome message Better than RPC


    I believe net code that potential is not just about synchronizing with multiple people

    For example, chatting, logging in, calling
     
  11. wechat_os_Qy0xkipWLGYk6K41qQOqJGO8s

    wechat_os_Qy0xkipWLGYk6K41qQOqJGO8s

    Joined:
    May 24, 2023
    Posts:
    8
  12. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    For these types of things you need to use Game Services, not multiplayer frameworks. NGO is a multiplayer framework. Unity Game Services are what you're looking for :D