Maximum data size in RPC

What is the maximum size of data that can be sent in RPC? I created a class consisting of List. I serialize it to a string, pass this string from the client as a parameter to RPC, and deserialize the string on the server. If this List consists of 0, then I can transfer 1024 values. 2048 is already throwing an error on the server side. If, instead of zeros, i fill in List from the loop with step 1 (0,1,2,3 etc), then an error is generated when 512 values are transferred.
What are the restrictions?
And how can i calculate whether a specific string will fit into RPC?
And how does this restriction work when the server receives several RPCs at the same time?
8676540--1169463--upload_2022-12-20_18-15-38.png

What error are you seeing?

I was able to send an array of 10k ints, but for 20k I got the ‘Writing past the end of the buffer’ error. Changing transport settings didn’t seem to make any difference.

I am getting this error on server:

KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <695d1cc93cca45069c528c15c9fdd749>:0)
Unity.Netcode.NetworkManager.TransportIdToClientId (System.UInt64 transportId) (at D:/Software/Projects/Squad_War/RPC_Testing_Server/Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkManager.cs:1329)
Unity.Netcode.NetworkManager.HandleRawTransportPoll (Unity.Netcode.NetworkEvent networkEvent, System.UInt64 clientId, System.ArraySegment`1[T] payload, System.Single receiveTime) (at D:/Software/Projects/Squad_War/RPC_Testing_Server/Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkManager.cs:1389)
Unity.Netcode.NetworkManager.OnNetworkEarlyUpdate () (at D:/Software/Projects/Squad_War/RPC_Testing_Server/Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkManager.cs:1221)
Unity.Netcode.NetworkManager.NetworkUpdate (Unity.Netcode.NetworkUpdateStage updateStage) (at D:/Software/Projects/Squad_War/RPC_Testing_Server/Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkManager.cs:1196)
Unity.Netcode.NetworkUpdateLoop.RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage updateStage) (at D:/Software/Projects/Squad_War/RPC_Testing_Server/Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkUpdateLoop.cs:149)
Unity.Netcode.NetworkUpdateLoop+NetworkEarlyUpdate+<>c.<CreateLoopSystem>b__0_0 () (at D:/Software/Projects/Squad_War/RPC_Testing_Server/Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkUpdateLoop.cs:172)

I have this class on client and server NetworkObject:

    [Serializable]
    public class Test
    {
        public List<int> ints;
        public Test(List<int> ints)
        {
            this.ints = ints;
        }
    }

This RPC on client NetworkObject:

    [ServerRpc]
    private void TestServerRpc(string data)
    {

    }

This RPC on server NetworkObject:

    [ServerRpc]
    private void TestServerRpc(string data)
    {
        Test test = JsonUtility.FromJson<Test>(data);
        Debug.Log(test.ints.Count);
    }

And on the client NetworkObject, I have a method that calls this code:

        List<int> ints = new List<int>();
        for (int i = 0; i < 2048; i++)
        {
            ints.Add(0);
        }
        Test test = new Test(ints);
        string json = JsonUtility.ToJson(test);
        TestServerRpc(json);

As a result, when looping to 1024, everything works fine, and when looping to 2048, it gives the above error on the server.

UPD:
I raised the Message Buffer Size on NetworkManager from 5120 to 10240 and the error went away. Tried to send 4096 ints and it came back again. What are the limits for this size?

Using your code in isolation I was able to send 5k ints, at 10k I got the writing past the end of the buffer again.

Have you tried running pre.5 and the Unity transport, in case changing the Max Payload Size makes any difference.

In case you grabbed Unity Transport and not the Netcode wrapper, add the package using this:

com.unity.netcode.adapter.utp

Some info here: https://docs-multiplayer.unity3d.com/docs/transport-utp/about-transport-utp

Ah yeah, in the editor under Unity Transport open up Connection Data and add the address to Server Listen Address. It can normally be left blank but it’s bugged in pre.5.

Yes, if I quickly find and fix the problem myself, then I delete the comment)

On topic, I seem to have switched to Unity Transport, and everything works. The error now occurs in the client:

Payload of size 16439 larger than configured 'Max Payload Size' (6144).
UnityEngine.Debug:LogError (object)
Unity.Netcode.UnityTransport:Send (ulong,System.ArraySegment`1<byte>,Unity.Netcode.NetworkDelivery) (at Library/PackageCache/com.unity.netcode.adapter.utp@1.0.0-pre.5/Runtime/UnityTransport.cs:760)
Unity.Netcode.NetworkManager/NetworkManagerMessageSender:Send (ulong,Unity.Netcode.NetworkDelivery,Unity.Netcode.FastBufferWriter) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkManager.cs:157)
Unity.Netcode.MessagingSystem:ProcessSendQueues () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Messaging/MessagingSystem.cs:493)
Unity.Netcode.NetworkManager:OnNetworkPostLateUpdate () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkManager.cs:1265)
Unity.Netcode.NetworkManager:NetworkUpdate (Unity.Netcode.NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkManager.cs:1202)
Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkUpdateLoop.cs:149)
Unity.Netcode.NetworkUpdateLoop/NetworkPostLateUpdate/<>c:<CreateLoopSystem>b__0_0 () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkUpdateLoop.cs:232)

It’s much more informative. Now I can experiment with the sizes. But I’m still wondering how the Server will behave when it accepts two RPCs of the maximum size at the same time. Will it queue a second RPC? Or will this RPC just disappear? I can’t figure out yet how to simulate this situation to test.

I also got a strange warning on the server and client when a PlayerObject spawns:

[Netcode] Could not get NetworkObject for the NetworkBehaviour. Are you missing a NetworkObject component?
UnityEngine.Debug:LogWarning (object)
Unity.Netcode.NetworkLog:LogWarning (string) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Logging/NetworkLog.cs:18)
Unity.Netcode.NetworkBehaviour:get_NetworkObject () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkBehaviour.cs:292)
Unity.Netcode.NetworkBehaviour:get_NetworkBehaviourId () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkBehaviour.cs:315)
Unity.Netcode.NetworkBehaviour:VariableUpdate (ulong) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkBehaviour.cs:494)
Unity.Netcode.NetworkBehaviourUpdater:NetworkBehaviourUpdate (Unity.Netcode.NetworkManager) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkBehaviourUpdater.cs:36)
Unity.Netcode.NetworkManager:OnNetworkManagerTick () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkManager.cs:1285)
Unity.Netcode.NetworkTickSystem:UpdateTick (double,double) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Timing/NetworkTickSystem.cs:96)
Unity.Netcode.NetworkManager:OnNetworkPreUpdate () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkManager.cs:1252)
Unity.Netcode.NetworkManager:NetworkUpdate (Unity.Netcode.NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkManager.cs:1199)
Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkUpdateLoop.cs:149)
Unity.Netcode.NetworkUpdateLoop/NetworkPreUpdate/<>c:<CreateLoopSystem>b__0_0 () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.5/Runtime/Core/NetworkUpdateLoop.cs:196)

With all this, there is only one NetworkBehaviour script on the scene and it hangs on the only player where there is a NetworkObject

I don’t think I can offer you any help on those, hopefully someone else will chime in.

1 Like

In any case, your advice helped me move forward. Thank you.

1 Like

On Unet, max message size without fragmentation is 1084 bytes, and fragmented max size is 59392 bytes before overflow buffer errors

refer to this to determine sizes of the data payload,