Page 1 of 1
Threading in AS3
Posted: 24 Aug 2010, 12:48
by kaspi
Hello,
We've just launch our multiPlayer Game as beta version.
Until now, everything works fine, but we have a problem due to the connection. Sometimes, a player (who has a bad connection) can receive two messages (two plays) in row from the Server.
This can block this player to continue the game. (It is rare, but It is an issue we have to deal with).
As flash doesn't manage threads, we have thought of a solution that I want to share with you to know what do you think about It.
The solution is very simple : We'll create a movieClip "Lock" that has two frames, and when Flash receives a message from a server we'll check if lock.currentFrame == 1. If It's the case, Flash continue the treatement. Else We'll put the object receives from server in a queue (Array).
Then we'll listen to the ENTER_FRAME Event of the lock MovieClip. if It's 1, then we'll took the first element of our queue (shift) and relaunch treatement.
What do you think about this solution ?
Posted: 25 Aug 2010, 01:19
by BigFIsh
Until now, everything works fine, but we have a problem due to the connection. Sometimes, a player (who has a bad connection) can receive two messages (two plays) in row from the Server.
Let's try and tackle that problem first. How are you sending the 'play' message to the client and at what interval?
Posted: 27 Aug 2010, 09:03
by kaspi
I send a play message, when player plays, I don't know the interval. They are 4 players on the game.
Example : Player 1 plays, 2 plays, 3 plays and player 4 plays.
The problem occurs when player 4 receives all the 3 plays (or 2) in the same time (due to connection issue or other issues). When this occurs, I can not block the treatement until the first one has finished. In Java we can use synchronized or java.util.concurrent.locks.Lock, but Flash doesn't allow that.
So the 3 treatements run on the same time, and this cause problems.
Posted: 27 Aug 2010, 20:54
by BigFIsh
Right, I see.
So each player must receive all 'play' messages to continue.
Can you check your AdminTool for the outgoing/incoming dropped messages values? Just wondering if the 'play' message was dropped. See "Understanding dropped message" section of this
doc.
Furthermore, what is your <DeadChannelsPolicy> and <MaxAllowedDroppedPackets> settings (in config.xml)?
Posted: 28 Aug 2010, 02:07
by kaspi
The server values are :
<ClientMessagQueue>
<QueueSize>100</QueueSize>
<MaxAllowedDroppedPackets>10</MaxAllowedDroppedPackets>
</ClientMessagQueue>
<MaxIncomingQueue>5000</MaxIncomingQueue>
<DeadChannelsPolicy>strict</DeadChannelsPolicy>
<MaxMsgLen>4096</MaxMsgLen>
What do you recommand ?
Posted: 31 Aug 2010, 00:50
by kaspi
Any answer plz
Posted: 31 Aug 2010, 01:19
by BigFIsh
There is one thing that isn't clear: Do you want all your players to receive multiple 'play' messages or not? Or do you want all the 'play' messages to arrive (if this was the case, please check your AdminTool for dropped messages)?
Which SFS package are you using? (Lite, Basic, Pro?)
Posted: 31 Aug 2010, 14:03
by kaspi
We have 0 dropped message and SFS PRO version.
I'll explain the game, It is a card game played by 4 players (1,2,3,4)
When player 1 plays, all the three others receive his play, then player 2 plays, 3 and player 4.
This is the game and The things go fine in the most of the case, but the problem occurs when a player 4, for example, receives the plays of 1,2 and 3 in the same time,
that means : player 4 has a connection problem. He didn't receive the play of player 1, nor the play of 2 nor the play of the player 3. But the Server keeps trying to send all the message to player 4. When The server succeeds to send messages, It will send the 3 messages (plays) on the same time.
In AS3 we cannot manage these three threads and all these messages (plays) will execute three threads that's (delete, create, modify) the same variable ! and these can bloc the game !
My question was, is there any solution to this problem ? How to block the treatement of the play 2, play 3 until the play 1 is processed by Flash ?
Posted: 01 Sep 2010, 05:46
by BigFIsh
Sounds like you're handling the plays (and modifying the room variable) from client side.
Since you have SFS Pro version, why not handle the plays and modifying the room variables via server side? This is more secure than handling the plays via client side, since this can be prone to 'hacking' - and you won't need to rely on client not being able to receive all these messages or receiving them at the same time.
Posted: 02 Sep 2010, 10:46
by kaspi
All the treatement, modifications, plays issues are done in the server side. The only thing that is done in the client side, is GUI issues.
But, for example, if you want to show animation when player 1 plays. After this animation finished, you show the card played by the player 1. But, because of the slow connetion of player 4, the plays of both player 1 and 2 arrives on the same time. When the animation starts, the seconds play (of player 2 arrives) that's cause problem in the client side, the card of player 2 will be shown before the card of player 1 (The server has no problem), and in some conditions can cause some bugs. When you want to delete all the cards played when all the 4 players played, but in the same time a new one arrives, It can be deleted !!
If we have a method to do as Java lock in AS3, all these problem will be resolved. Because we would be able to telle Flash, wait until the treatement of play 1 is done before processing the treatement of play 2.
Posted: 02 Sep 2010, 22:31
by BigFIsh
After some discussion with google, I found an article about threading in AS3 (
http://blogs.adobe.com/aharui/2008/01/t ... ipt_3.html) - and it mentioned that it does not allow 'blocking' or 'yielding'. So, I don't think it's possible to lock a thread in AS3.
But, I'm sure you can still achieve what you need without having to block threads. It's not 100% clear how your game works - so I cannot easily suggest a possible workaround. You'll need to review your code, add validation (via client / server) to ensure that the plays are valid. You could get the client to send to the server 'Okay, I got this play - please send me the next play' - or you can validate the plays via the server and only send the valid plays to a client.
I can't really help you further with this as this forum is really for 'quick help' not for dedicated consulting, so you're on your own now.
You can however contact SmartFoxServer to ask for consulting services.