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