I'm developing a chess game online.
My idea is to make the server as the place where the data communicate from one player to the other.
So here is my plan to continue:
When two players connect the server(with TCP), the server create two threads for both of them. It means that the two threads need to communicate each other.
// server
while(true)
{
if(socket.accept())
{
create_a_thread(); // handle one player
}
}
But one game needs two threads in the server? I don't think this is a good design.
How should I redesign my server?
Two thoughts on the conceptual layer:
makeMove(Game g, Move m)
where one player just pushes the move to the server, and the server then sends a notification to that one client.What I mean is: it is important to keep things as simple as possible. When you build a mental model of a chess game; yes, then the first idea is that even your server would need that notion of a "game" of "two connected" users. But that isn't necessarily true. Rather see the server as a "mail box" where you put a letter "here is my next move"; and the server makes sure it gets to the other player!
Long story short: you should first identify the core responsibilities within your server (like: "receiving user requests", "processing user requests", "send notifications"); and then you look into "multi-threading" those.