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

NetworkTransport.Recieve - flexible buffer size

Discussion in 'Scripting' started by TomPo, Apr 19, 2019.

  1. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    Hi
    I know that 2019 is comming with new Networking but for now is not stable and still in beta so...

    What I'm using is:
    Code (CSharp):
    1. byte[] recBuffer = new byte[1024];
    2. int dataSize;
    3.  
    4. NetworkEventType type = NetworkTransport.Receive
    5. (out hostID, out conID, out channelID, recBuffer, BufferSize, out dataSize, out error);
    But there is a big problem with this:
    - even if we will receive 10 bytes of data we are using 1024 buffer what is pointless
    - if data will be bigger than cons buffersize then whole event will be dropped

    Question:
    Is there some way to change buffer size on-the-fly according to data received?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Well, you don't need to be creating new buffer arrays every time you receive. You can just reuse the old one. So you have 1 buffer array of the maximum size possible to receive.

    Then you can create a new array of the size you actually need, and do an Array.Copy of just the data received.
     
  3. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    OK... but it means that still you have to deal with const size of that buffer right?
    Sure I can make it once and then clear the byte[] array but the problem remains the same.
    You have to predict the biggest possible message can possibly come in the future and you using for example 1024 to receive 10 byte message, right?
    That's why I asked the question if is possible to change buffer size depending on the size of data received.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Why would you clear the buffer array?

    You create a single buffer array of the maximum size. When you receive new data that new data occupies indexes 0 through receivedSize -1 of this buffer. Next time you receive new data you just write it to the very same buffer array, again occupying indexes 0 through receivedSize -1 of the buffer array. You never need to make a new buffer array, you never need to clear the buffer array. It is just 1kb of memory you're reusing here.

    If you need to keep the data from the buffer array after you would overwrite it with new data, you just Array.Copy to another array of just the length of the data, not the length of the entire buffer.

    You can't change the buffer size depending on the size of the data received because you don't know the size of the data received until you've already written it to the buffer. So to your actual question, the answer is no, but your question is a bit silly.
     
  5. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    Thanks for the explanation.

    How to manage this using NT.Receive with LLAPI?
    To not create new byte[] each frame?
    Code (CSharp):
    1. byte[] recBuffer = new byte[BufferSize];
    2. NetworkEventType type = NetworkTransport.Receive(out int hostID, out int conID, out int channelID, recBuffer, BufferSize, out int dataSize, out byte error);
    3. if(error >0) return;
    4. switch(type){}