Jump to content
  • Advertisement
Sign in to follow this  
umen242

Good Strategy Or Pattern In A Sync Single Threaded Server To Broadcast Massage To All And Self?

This topic is 715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im have some confusion and i need some high level direction can be in any language 
basically i need some pattern or algorithm that order the way massages are   received in to the server and then broadcast to all connection that are connected .   
with options     
  
 1. to know to whom the massage is send to .    
 2. send to my self      
 
 
*pseudo code:*  
In most  async servers ther are 3 main callbacks:
 
    onOpenConnection(connection strcut) 
    onRecivedRequest(connection strcut,massage) 
    onSendResponse()
    onCloseConnection(connection strcut)
 
 
what i have now is that i keep all connection that are initiated on : *onOpenConnection* method into map 
something like    
*pseudo code:* :   
 
    PlayersMap<connection , player > 
 
then on `onRecivedRequest(connection strcut)` i collect all recived massages 
into Player vector so it can be none blocked . 
*pseudo code:*   
 
    Player1.vector.insert(massage)
 
 
but my problem is in the `onSendResponse()`
now that i have all the connected players and each player has its massages   request vector , how i iterate in efficient way throw all the players and broadcast them all recent massages in none blocking way ,   
 
*pseudo code:*   
Now i have in the  *onSendResponse()* :   
 
    For(int i =0 ;i< PlayersMap.lenght ; i++)
    {
          player  = PlayersMap
          For(int j =0 ;j< PlayersMap.lenght ; j++)
          {
              
               InnerPlayer  = PlayersMap[jj];
               MassageString = InnerPlayer.vector.front()   
               sendResponseTo(player,MassageString)    
          }
    
    }
    
 
What it does it iterate through all connected players (outer loop ) 
and send each of them the massage which is on top of the vector ( FIFO ) 
of each player , 
result is that every player get updates of the world . 
 
This approach presents problems 
1.clearly this is not the most officiant way to do this . 
2.I need way to clear the massage vector after each "All players iterations"
Remember that massages are keep getting on high rate all the time . 
 
Thanks  

Share this post


Link to post
Share on other sites
Advertisement
First: There is no more efficient way of "iterating through all players" than, well, iterating through all players.
The most efficient way of iterating over X is to keep X in a vector (for cache reasons,) but in general, that level of optimization usually has no measurable impact on the program for < 10,000 connections.
If you are really sending the same updates to everybody (not filtering out their own responses, for example) then you can have a single buffer that receives all the response data, and then loop through all connected clients and send the same data buffer.
However, you typically mix up some per-player-specific data with some for-everybody data, so it's more common that you generate a packet for each player with exactly the data they need.
In fact, it's often the case that most of the "networking" work goes in figuring out what, exactly, each player should be hearing about (doing regions-of-interest, and such,) rather than the basic shuffling of bytes in buffers.
This goes to another point, which will save a lot more resources on the network (which is typically much more limited than the CPU:) You'll want to pack all the information you have for a particular user into a single network message, when you send it. Thus, in your inner loop, you should concatenate all the messages that actually go to that user, into a single buffer (prefix each with "type and size" information) and then send that one buffer after the inner loop.

Share this post


Link to post
Share on other sites

Great this is what i did sort of , just not pack all info and send in buffer to the same client , i just send small chancks of data .
The main reasone is that allot of info can be changes and packing in the same network buffer can be large and get to the client maybe with missing data . i don't know yet . 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!