Hi guys !
My first message here ! Sorry for the title but I had no idea what to write xD
So I'm here to learn from your critics. I'm actually doing a small video games. It's multiplayer & online game. It's going well but I have some questions for the design of the communication between the client and the server. The game is working like this :
The server handle a party of 4 players maximum. When a message is sent to the server, the answer is automatically broadcast to all players in the party.
The flow to start a game is : Main menu => create game => wait for other players if wanted or not => start the game
For each action from the client, the client needs the confirmation from the server. (i.e player1 want to move to a location he said to the server "I want to move there", the server answer the current (if move is not accepted) or the new position and broadcast it to all players in the party). A party can have 4 players maximum
Actually I'm dealing it like this:
Client : 2 threadsMain thread (MainClient) doing this :
while(run)
{
Event();
Dequeue_and_treat_messages();
Update();
Display();
}
The second thread (ConnectionHandler) handle the connection with the server and do this:
while(run)
{
Receive_message()
Queue(message);
}
And for the serverOne thread for each client (ClientHandler) doing :
while(run)
{
Receive_message();
Treat_message();
}
and when the client start a game a new thread is created (GameHandler) to handle the game update doing:
while(run)
{
Update_game();
}
That's mean in the "worst" case a maximum of 5 threads are created for one party one the server. But it will require mutex on all data to be thread safe in each data modification. To avoid this only one thread will manage the received message from the client. I'm here to get your feedback to improve this part.
I know I have different options like:
- Use selector when the Owner of the party start the game to merge all connection into one thread but it could complexify the code and it could be horrible to handle the disconnection in game or the end of the game to re-create thread for each client etc...
- My second option is to use message queue like for the client. It could be:
For each client
while(run)
{
Received_Message();
if(in_game)
{
Queue(message);
}
else //in menu
{
Treat_message();
}
}
and the GameHandler
while(run)
{
Deque_message();
Update();
}
It could work but it's not really elegant. And the problem I have with this solution is for each message received the "if" is done. I would like to avoid this.
- Third solution, use the second solution with functor but i'm not really comfortable with it.
If you have any idea / comments / what ever, feel free !
Cheers !