Archived

This topic is now archived and is closed to further replies.

fireking

send messages to multiple clients

Recommended Posts

im facing a fork in the road in development of effecient server code. lets say a client is gonna shoot a fireball, and only the people in his view is gonna see this (its a 2d tile based game, so no trouble finding those people). Ok, now i could send that to the server, and the server could update the clients in his view. BUT! we would be using a for loop. This for loop would keep looping until its sent the data to each client that needs to be updated. Here is the problem, the server has to update in a loop (well at least mine does), and that for loop could be vital time missed. So im figureing im gonna need some type of loop-based solution. One loop goes, it sends to the first client, another loop goes, it sends to the next client, another loop goes, it sends to the next client, and so on. As opposed to one loop goes, it sends to all the clients in the for loop, then continues on through the loop.
myloop
    update the server
    client sent a fireball
        for each client in client''s view
            send fireball
 
thats the BAD way to do it, but im looking for a BETTER way to do it
my loop
    update the server
    client sent a fireball
        send to next client in client''s view
            send fireball
//loop returns
my loop
    update the server
    already sent fireball, still sending
        send to next client in client''s view
            send fireball
//loop returns
 
something like that. But the thing is i dont know how to actually implement this with code. I guess you could call this non-blocking server sorting or something. Where a for loop is gonna block, but i need another method that wont block. i guess the cop-out method would be this
my loop
    update the server
    client sent fireball
        for each client in client''s view
            update the server
            send fireball
 
note: update server checks for incoming messages (and handles them), incoming connections, disconnecting players, and socket errors on clients(disconnects them). so anyone? --Fireking Owner/Leader Genetics 3rd Dimension Development

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It''s a loop. Live with it. If sending data to all the clients is going to lower your framerate that much, you should reduce the number of players, or increase the power of the server, because no matter how you code it you still have to send the data to the clients.

Share this post


Link to post
Share on other sites
I agree with the Anon Poster, live with the loop it''s not a problem.

Are you using UDP? Because if you are then Send does''t take very long.

With TCP it can take a while, because it waits for the client to acknowldged it receive the data.

In that case if you are worried about the Send blocking, you can try using non-blocking/asynchronous sockets, or even threads.

Share this post


Link to post
Share on other sites
its not about frame rate, this is a server app!!!!!

the server is non-blocking

--Fireking

Owner/Leader
Genetics 3rd Dimension Development

Share this post


Link to post
Share on other sites
also the reason im concerned is what if a client sends data to the server while the sever is sending data to the clients, the server wont update till it gets back around to the loop, wouldn''t that possibly cause out of sync-ness?

--Fireking

Owner/Leader
Genetics 3rd Dimension Development

Share this post


Link to post
Share on other sites
also the reason im concerned is what if a client sends data to the server while the sever is sending data to the clients, the server wont update till it gets back around to the loop, wouldn't that possibly cause out of sync-ness?


If the server is non-blocking, then you have nothing to worry about. The time it takes to send to all the clients is negligable compared to the time it will take to send data over the network.

Also, not sending data to all the clients at the sametime will almost certainly cause out of sync problems.

e.g. At same time:
Server gets a "Client A creates a fireball" message
Server sends results to Clients A and B
Server gets a "Client B runs away" message
OK

Not at same time:
Server gets a "Client A creates a fireball" message
Server sends results to Client A
Server gets a "Client B runs away" message (and is now out of range of the fireball.)

PROBLEM: Did Client B get hit by the fireball?




[edited by - Xiol on October 17, 2002 5:17:51 AM]

Share this post


Link to post
Share on other sites
Hi,

What about having a dedicated Send Message Thread which checkd for messages to send and sends them and removes them from the send queue... ie

Main Process Thread:

update the server
client sent a fireball
Gain exclucive access to the Queue (thread safe)
for each client in client''s view
Queue fireball message for this client
Set QueueHasData Object
Return normal access to the queue


Processing Thread:

Loop
Pause thread untill QueueHasData
Gain exclucive access to the Queue (thread safe)
Get Next Queued Item
If Last Item In Queue then
Clear QueueHasData Object
Return normal access to the queue
Send The Message Retrieved from the Queue
end loop



This way if the tcp sends do hang in any way then the main server processing thread will still be executing etc.. You will need to lookup threads and "CriticalSection" objects if you dont have experiance in them

hope that helps
~tim

Share this post


Link to post
Share on other sites
quote:
Original post by fireking
its not about frame rate, this is a server app!!!!!

the server is non-blocking



even a server has a frame rate, for example, HL servers by default run @ 30fps (you can change this using an external program however)

Share this post


Link to post
Share on other sites
quote:
Original post by Xiol
also the reason im concerned is what if a client sends data to the server while the sever is sending data to the clients, the server wont update till it gets back around to the loop, wouldn''t that possibly cause out of sync-ness?


If the server is non-blocking, then you have nothing to worry about. The time it takes to send to all the clients is negligable compared to the time it will take to send data over the network.

Also, not sending data to all the clients at the sametime will almost certainly cause out of sync problems.

e.g. At same time:
Server gets a "Client A creates a fireball" message
Server sends results to Clients A and B
Server gets a "Client B runs away" message
OK

Not at same time:
Server gets a "Client A creates a fireball" message
Server sends results to Client A
Server gets a "Client B runs away" message (and is now out of range of the fireball.)

PROBLEM: Did Client B get hit by the fireball?




[edited by - Xiol on October 17, 2002 5:17:51 AM]


well if thats the concern, i have nothing to worry about

the game is designed in a manner where your situation could not happen. the battles are turn based, and if a player used a fire ball, then it would either hit or miss according to the skill level. The monster does get a chance to dodge it, but thats determined by the server depending on the situation. If two players were fighting, it would be the same situation. The server would determine if the fire ball actually hit the client. For instance client A chooses "reflect magic", and his turn is over. Client B casts fire ball but client A is in a state where magic is reflected so client b is hit with his own fire ball.

--Fireking

Owner/Leader
Genetics 3rd Dimension Development

Share this post


Link to post
Share on other sites