MidLevel / MLAPI.Relay

Open source relay for UNET. Works with the HLAPI, MLAPI and all other UNET projects.
MIT License
91 stars 30 forks source link

Out of bounds exception #8

Open genaray opened 3 years ago

genaray commented 3 years ago

Was already posted on the discord... theres a weird exception going on when using the relay... It happens completly randomly but is game breaking. https://media.discordapp.net/attachments/563033158480691211/803724646826377226/unknown.png?width=1440&height=178

timmeh4242 commented 3 years ago

We're seeing this as well, and also inconsistently.

ajaybirla commented 3 years ago

Hi All,

I'm not getting this issue any more after doing the below changes in the Send function.

Note: I'm not sure if this is the correct way to fix this issue but it worked for me.

public bool Send (int hostId, int connectionId, int channelId, byte [] buffer, int size, out byte error)
{
    if (!Enabled) return NetworkTransport.Send (hostId, connectionId, channelId, buffer, size, out error);

    ++size;

    if (!isClient)
    {
        size += 8;

        // ********************************************************
        // Birla: Resizing 'buffer' array as 'buffer.Length < size'
        // ********************************************************
        if (buffer.Length < size)
        {
            Debug.LogErrorFormat ("<color=cyan>Resizing 'buffer' array as buffer.Length ({0}) < size ({1})</color>", buffer.Length, size);

            byte [] tempBuffer = new byte [size];

            Buffer.BlockCopy (buffer, 0, tempBuffer, 0, buffer.Length);

            buffer = tempBuffer;
        }
        // ********************************************************

        int connectionIdOffset = size - 9;

        for (byte i = 0; i < sizeof (ulong); i++) buffer [connectionIdOffset + i] = ((byte) ((ulong) connectionId >> (i * 8)));
    }

    buffer [size - 1] = (byte) MessageType.Data;

    return NetworkTransport.Send (hostId, relayConnectionId, channelId, buffer, size, out error);
}