SFS2X handling response synchronization from different Threads

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

arun8483
Posts: 33
Joined: 15 Dec 2010, 02:35
Contact:

SFS2X handling response synchronization from different Threads

Postby arun8483 » 01 Sep 2017, 06:47

A separate running Thread that is handling Rooms and Users in queue to pick 2 players after client initiates search request
The following is the scenario which I am really concered about and not quite understand wheather or not I am in the right path

1) lets say Player A and Player B sent search request to Lobby_1 room. Both User objects are added to ConcurrentLinkedQueue which is already mapped to Room ( Looby_1 )
so this condition "if ( queueSize >= 2 )" satisfies and its about to call onSearchSuccess on lobby object sending Player A and Player B as params.
Since this is separate thread running concurrently all the time, Lets say Player A quits/disconnects just before this function call "lobby.onSearchSuccess(player1, player2);"
since Player A disconnect is happening before executing onSearchSuccess on Lobby, Player A recieves onSearchSuccess and wait for the game to begin forever.
If onSearchSuccess execute first and then disconnect, I could let Player A know that Player B is disconnected

2) Is adding Room and User objects to Collection objects and handling them inside a separate Thread is recommended? is there any problem in doing so ?

I hope I made my problem clear.

How to solve this problem. I don't think introducing synchronization in not the right approach in multiplayer games

Code: Select all

 public void run() {
        if(!isRunning())  {
            setRunning(true);
        }
        while( isRunning() ) {
            for(Map.Entry<Room, ConcurrentLinkedQueue<User>> entry : LOBBY_MAP.entrySet()) {
                Room room = entry.getKey();
                ConcurrentLinkedQueue<User> queue = entry.getValue();
                //check queue size
                int queueSize = queue.size();
                //minimum queue size must be 2 or greater than 2
                if ( queueSize >= 2 ) {
                    logger.debug("QUEUE SIZE: " +  queueSize);
                    User player1 = queue.poll();
                    User player2 = queue.poll();
                    //check if both players are not null
                    if( player1 != null && player2 != null ) {
                        Lobby lobby = (Lobby) room.getExtension();
                        lobby.onSearchSuccess(player1, player2);                       
                    }
                }
            }
           
        }
    }


public class UserDisconnectHandler  extends BaseServerEventHandler {

    public void handleServerEvent(ISFSEvent isfse) throws SFSException {
        if(isfse.getType() == SFSEventType.USER_DISCONNECT ) {
            User user = (User) isfse.getParameter(SFSEventParam.USER);
            List<Room> rooms = (List<Room>) isfse.getParameter(SFSEventParam.JOINED_ROOMS);           
            if( rooms != null ) {
                for(Room room : rooms) {
                    if( !room.isGame() ) {
                        SearchWorker.getInstance().removeUser(room, user);
         //handle users qualified after search success
                    }
                }
            }
        }   
    }
   
}
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFS2X handling response synchronization from different Threads

Postby Lapo » 01 Sep 2017, 08:23

Hi,
Since this is separate thread running concurrently all the time, Lets say Player A quits/disconnects just before this function call "lobby.onSearchSuccess(player1, player2);"
since Player A disconnect is happening before executing onSearchSuccess on Lobby, Player A recieves onSearchSuccess and wait for the game to begin forever.
If onSearchSuccess execute first and then disconnect, I could let Player A know that Player B is disconnected


There is a bit of confusion here, because you started by saying that Player A is the one who is disconnecting, but at the end you say it's B.
Which is which? :D

Also what is not clear is... are Player A and B in the same Room during the search?
If so they will receive an event if one them is disconnected. This in turn should solve the problem entirely, since the event will be received before or after the "OnSearchSuccess" based on when the disconnection occurred.

If they are not in the same Room, or the Room is configured not to send User events, then you will have to create such event system by yourself via server code.
I don't think it's too difficult. Once you have matched Users you should know who is seeing whom and therefore, when you detect a disconnection from server side, you should be able to signal it to the client(s).

Sorry for being a little too generic in my reply but I am not sure I fully understand the scenario here.
Maybe you can clarify further...

thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
arun8483
Posts: 33
Joined: 15 Dec 2010, 02:35
Contact:

Re: SFS2X handling response synchronization from different Threads

Postby arun8483 » 01 Sep 2017, 10:05

Lets take a general case Thread 1 and Thread 2 running concurrently...
1) Both players are in the same Room
2) I have attached a screenshot that explains the scenario.
scenario.png
(28 KiB) Not downloaded yet

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 45 guests