I've been getting some weird turn outs and I would like, if possible for you, the guys who know the system well enough, to explain to me a little about how the server works when it comes to accessing specific object from multiply client calls, and the situation is this, as an example:
A user is logging in to the system, and since he's the only one connected (at the zone level), I put him into an array that will hold the users objects of connected users.
A second and a third users connected, and since one user is already connected, I would have liked to assign one of the new users the old user to play with.
When I assign an old user (already in array of users) to a new user, I eliminate the user object in the users array, so, when putting this into effect in this example, the second logged in user will log in, find the old user in the list, and so will start to play with him, I will eliminate the old user object from my array, and so, when the third user logs in, and sees the array is empty, and so he goes into it as a waiting users.
Could there be a situation when a message is coming from both users at the same time, and so both of them actually see the array as holding the waiting users, or does the server work in a way that will process the calling from the clients one by one, so under no circumstances could one user see the array as still populated since the user before him got to it a millisecond before or less, but still, his call was not processed while the call of the first of the two new users have been working on the server??
I know I'm not very good at describing these situations, maybe some code examples will be good here (remember, this is not an actual running application, it's just an example to make my point clear):
on server zone init:
Code: Select all
var users;
function init(){
users = new Array();
}
Code: Select all
function handleRequest(cmd, params, user, fromRoom){
if(cmd == "play"){
if(users.length == 0){
users.push(user);
}else{
//get user id.
//send id to the newly logged player, and id of the new
//player to the old waiting player.
delete users[0];
}
}
}
And the question is, supposing 2 users have called the "play" request at the same time, or at least, the server receives them at the same time, does the server process one request at a time, so no matter what, one user would be the "first" and "only" user working on the "users" array, or does it work on more then one request at a time, and thus actually we have the possibility of 2 users "thinking" that there is a user waiting and both are being sent the play message, and thus, when a forth player logs in, instead of finding player 3 in the waiting list, the list is empty and he get into the list himself...
I hope this is clear enough for an answer.
Yuval Lahav.