Search Unity

UnityNetworking: RPCs are NOT received in the order they are sent

Discussion in 'Multiplayer' started by Moonstorm, May 13, 2014.

  1. Moonstorm

    Moonstorm

    Joined:
    Jul 26, 2012
    Posts:
    23
    I have been banging my head on this problem for a few hours now and I've come to the conclusion that RPCs in Unity Networking are NOT guaranteed to arrive in the order they are sent. I've created a very small example project to illustrate my point (Github: https://github.com/project-arkana/pingpong).

    Examine the following code:
    Code (csharp):
    1.  
    2.  
    3.  // Called on Server when a Client connects
    4.     void OnPlayerConnected(NetworkPlayer player)
    5.     {
    6.         // Sent to all players, including the server
    7.         networkView.RPC("Ping", RPCMode.All);
    8.     }
    9.  
    10.     [RPC]
    11.     void Ping()
    12.     {
    13.         Debug.LogError(Time.frameCount + " :: PING!");
    14.         if (Network.isServer)
    15.         {
    16.             // Second RPC sent out to all players from the server
    17.             networkView.RPC("Pong", RPCMode.All);
    18.         }
    19.     }
    20.  
    21.     [RPC]
    22.     void Pong()
    23.     {
    24.         Debug.LogError(Time.frameCount + " :: PONG!");
    25.     }
    26.  
    27.  
    When a client connects to a server, the "OnPlayerConnected()" method is invoked on the server. It then sends out two RPCs in sequential order: Ping and then Pong.

    However, the client receives them as Pong and then Ping.

    The documentation clearly states:

    Is this a serious networking bug that exists in Unity Networking? Can someone help me figure out what's going on here?
     
    Last edited: May 13, 2014
  2. Uncasid

    Uncasid

    Joined:
    Oct 5, 2012
    Posts:
    193
    I have been thinking about this one a bit, sorry for the slow reply:

    Here is what is happening:
    1. Player connects, method "OnPlayerConnected" is called on the server.
    2. Server sends a RPC to self / client with "ping"
    3. Server / client get ping RPC, only server processes the RPC
    4. Server responds with a pong RPC call to both client and server.
    5. Both client and server process pong RPC

    My guess is that unity does not send a packet to the server, but instead directly calls the RPC method. It then makes an RPC call for pong, which is sent. This would appear in order on the server, but possibly not on the client. I would suggest adding latency to test this and see if it might be the case.

    Btw, try using RPCMode.AllBuffered, see if that changes the behavior.
     
  3. Uncasid

    Uncasid

    Joined:
    Oct 5, 2012
    Posts:
    193
  4. Moonstorm

    Moonstorm

    Joined:
    Jul 26, 2012
    Posts:
    23
    Thanks for replying! You are a very brave soul indeed =)

    I checked out the answers link you posted -- I don't think this has anything to do with the NetworkView's synchronization state. I can get this issue on NetworkViews that are totally not syncing any state at all. You are definitely right in that the Server's executing both Ping and Pong locally without sending a network request. What's odd to me is that they're being called on the same frame in a specific order but being received in exactly the opposite order.

    In fact, for experiments sake, I went ahead and created an RPC line from A-Z, where RPC "A" calls RPC "B" calls RPC "C".. etc... in the same fashion as the example above. Surely enough, the clients all receive the RPCs in *reverse* order (Z to A) without fail.

    It seems like if you have an RPC call that calls another RPC and they're performed on the same frame, the receiving clients will get messages out of order. It's a very annoying bug and the only fix I can see right now is to delay the second RPC by a frame so that they truly come in order again.

    The only other solution is to try making sure that you never make an RPC method that calls other RPC methods. That's an *extremely* annoying constraint for my system.
     
  5. Uncasid

    Uncasid

    Joined:
    Oct 5, 2012
    Posts:
    193
    Interesting. I will do some experimenting with this. Would you mind posting a test project? That way I ensure I am working in the same environment you are.
     
  6. Moonstorm

    Moonstorm

    Joined:
    Jul 26, 2012
    Posts:
    23
  7. Uncasid

    Uncasid

    Joined:
    Oct 5, 2012
    Posts:
    193
    I will check this out tonight when I get off work, thanks for posting it!