Forum Multiplayer Race condition/Concurrency in callbacks

Discussion and help relating to the PlayerIO Multiplayer API.

Race condition/Concurrency in callbacks

Postby KVADRO » February 10th, 2019, 9:43 pm

Hi, the question is: GotMessage callback will be called sequentially for all income messages, or, for example, for two received messages there will be two concurrent invocation of GotMessage callback? (Server Side)
KVADRO
 
Posts: 16
Joined: May 6th, 2018, 8:22 pm

Re: Race condition/Concurrency in callbacks

Postby qugengames » February 10th, 2019, 10:19 pm

KVADRO wrote:Hi, the question is: GotMessage callback will be called sequentially for all income messages, or, for example, for two received messages there will be two concurrent invocation of GotMessage callback? (Server Side)


Hi bro, the messages should arrive in order as PlayerIO uses TCP which guarantees receiving order, which UDP doesn't.

As per the GotMessage execution, please take into account that it's multithreaded so one thing is to literally receive the message by the client and the other is the execution of GotMessage, yes it could happen that 2 GotMessages concurrently gets executed, so you should implement a Queue system where you add the message to a List<> or HashSet<> and process it later on another gameloop of your code,

Don't forget to use the lock feature so you handle concurrency on your list while other parts of your code are using that list.
This is the way I do it:

Code: Select all

// Stack where I add the message storing the BasePlayer so I know who actually sent the message,
// that way I can validate the client in case they lie about a game data so I don't rely fully on what they say.
private Stack<KeyValuePair<BasePlayer, PlayerIO.GameLibrary.Message>> server_StackNETMessagePending =
      new Stack<KeyValuePair<BasePlayer, PlayerIO.GameLibrary.Message>>();

public void Server_GotMessage(
          BasePlayer player,
          PlayerIO.GameLibrary.Message m)
{
            lock (server_StackNETMessagePending)
            {
                 server_StackNETMessagePending.Push(
                      new KeyValuePair<BasePlayer, PlayerIO.GameLibrary.Message>(player, m));
            }
  }


Later in another GameLoop part of your code you can process the message by using Pop():
Code: Select all
var keyPair = server_StackNETMessagePending.Pop();
var theMessage = keyPair.Value;
var thePlayerWhoSentTheMessage = keyPair.Key;

// #### Never trust the player, validate thePlayerWhoSentTheMessage !!


I'm working actively on a 2D Moba using PlayerIO and Unity,
If you have questions let me know,

Thanks
Last edited by qugengames on February 10th, 2019, 10:24 pm, edited 1 time in total.
qugengames
 
Posts: 27
Joined: April 24th, 2018, 11:19 pm

Re: Race condition/Concurrency in callbacks

Postby KVADRO » February 10th, 2019, 10:24 pm

qugengames thanks man for explanations and, especially, for code example!
KVADRO
 
Posts: 16
Joined: May 6th, 2018, 8:22 pm


Return to Multiplayer



cron