Forum Multiplayer Please help to understand server side multiplayer concepts

Discussion and help relating to the PlayerIO Multiplayer API.

Please help to understand server side multiplayer concepts

Postby KVADRO » February 13th, 2019, 6:18 pm

Hi,
i still try to understand how multithreading works on server side.

A far as i understand each player has a 'event pipeline' with dedicated thread. And, all events from this player are put to the queue, for example, ->[Disconnected, Message, Message, Connected]-> (FIFO), and, relevant room callbacks are called sequentially for this player. So, no next event will be fired for player if currently some event is processing for this player. Am i right?

Question is: I know that two GotMessage callbacks may be called for two different players simultaneously, but is it possible that two GotMessage callbacks will be called for one player simultaneously? For example, on server side i acquire lock, and block other threads, during that player will send two messages, so server will receive these messages and what would be the result? Server will call GotMessage for first message, in this function we'll try to acquire lock but thread will be blocked (because lock was acquired earlier), and simultaneously call another GotMessage for second message, and thread will be blocked too. After some time, lock will be released, and scheduler may decide to continue GotMessage witch was called for second message, so, messages will be processed out of order. Is that possible?

Brief code example:
Code: Select all
private object m_lock = new object();

private void Test()
{
    lock(m_lock)
    {
        // Do something
    }
}

public override void GameStarted()
{
    Test();
}

public override void GotMessage(Player _player, Message _message)
{
    Test();
}
KVADRO
 
Posts: 16
Joined: May 6th, 2018, 8:22 pm

Re: Please help to understand server side multiplayer concep

Postby robscherer123 » February 13th, 2019, 8:36 pm

While I can't say for sure how the internals are set up for events, in my several years of running my game on the PlayerIO server I would say it seems like it is possible that two messages from the same player could potentially fire at the same time. Henrik may have to correct my if I'm wrong though. Understanding all of the multi-threading based stuff took me a good while to wrap my head around years back, as everything seems to be very heavily multi-threaded inside the server. So my assumption would be that yes, I think that may be possible, but can't say for certain.

As a side note, I'm not sure exactly your reason for locking, but I've sometimes had issues with "lock" throwing errors in the errorLog because of threads hanging. If the reason for the lock has to do with lists or arrays, I would try to use ConcurrentDictionaries or the like, as they've work really well for me. That is, if that applies to your situation.
robscherer123
Paid Member
 
Posts: 304
Joined: December 12th, 2012, 8:46 pm

Re: Please help to understand server side multiplayer concep

Postby KVADRO » February 15th, 2019, 11:16 am

robscherer123
Thanks for your answer

Henrik
Could you please clarify?
KVADRO
 
Posts: 16
Joined: May 6th, 2018, 8:22 pm

Re: Please help to understand server side multiplayer concep

Postby atilla1 » February 15th, 2019, 4:10 pm

I know a bit more about the internals, and nearly everything is asynchronous. I would highly recommend writing your code with that in mind. In a way, there is an event pipeline although it depends on what exactly you're asking for. You should rely on your own, as it will be less confusing in the long run. You can effectively design your own using room types and authentication connection type with distinct permissions.

To be technical though, sockets are accepted asynchronously and packets are read asynchronously per connection, and while messages are queued prior to being processed on both sides, it's much safer and elegant design practice to assume that they could be received simultaneously. You can create your own queue with minimal effort, and process it at whatever rate you like.
atilla1
 
Posts: 4
Joined: September 7th, 2012, 9:48 pm

Re: Please help to understand server side multiplayer concep

Postby Henrik » March 12th, 2019, 4:57 am

No, there is no event pipeline server-side, everything is asynchronous. All the callbacks from all PlayerIO calls inside the game server are also asynchronous, for example saving to BigDB.

If there are pieces of your game logic that needs to be synchronous, think about it very carefully and use locking sparingly. Any time you use locks, you run the risk of some poor guy is just stuck waiting for the lock until the CPU timer is up, and then that thread is aborted. I'm pretty sure we log those to the game's ErrorLog, so check there if you're getting them.
Henrik
.IO
 
Posts: 1822
Joined: January 4th, 2010, 1:53 pm


Return to Multiplayer