Hello everyone,
I'm struggling with some infrastructural logic hopefully you guys can give me a hand.
Given a single thread approach to networking, the sending and receiving is happening in the same loop. ( I'm using ENet ). The idea is I have a game thread that pulls from a message queue pushed by receive events and a loop inside of my network thread that pulls messages out of a queue pushed by my game thread.
Psuedo code:
NetworkThread()
{
Messages[] messagesToSend;
while(true)
{
Poll(event);
if(event type == Receive)
Game.Messages.Enqueue(event);
//this loop is going to block my entire network until its done
foreach(m = messagesToSend.dequeue())
{
send(m)
}
}
}
GameThread()
{
Messages[] msgs;
Player[] players;
while(true)
{
while(m = msgs.dequeue())
{
//do something with message
if(m is PlayerMovedPacket)
{
players[m.id].Move(m.position);
players[m.id].isDirty = true;
}
}
//with 200 players connected this is going to add 40,000 packets to the message queue to send out per tick
if(100ms has passed) // tick, send move packets out to everyone
{
foreach(Player sender in players)
{
if(p.isDirty) //if player has moved
{
Packet packet = PlayerMovedPacket(sender);
//send 'sender' movement to all other players
foreach(Player receiver in players)
{
Network.messagesToSend.enqueue(receiver,packet);
}
p.isDirty = false;
}
}
}
}
}
Given the example above my send loop has to send every packet out faster than the 100ms otherwise the message queue will just build up and the send loop will never finish resulting in blocking the entire network.
One solution I've thought of is not having my game thread add anymore ‘tick’ related messages to the queue while the send loop is running but I'm not even sure thats a good solution.
Thanks