Question

In most online multiplayer strategy games, players build buildings and attack to get better at the game.

But, there's one thing I've never understood. A player could start building some building, that'll last for 3 hours.

But then, even though he disconnects, the building keeps getting built.

That, of course, means that it's server sided.

But to keep the server responsive while the building is being built, it has to be on a separate thread, which then becomes a problem.

Think about 500 players connected to one server, each building 2 buildings. That'll account to 1000 threads, and as a result the server would be unresponsive.

So how is it done correctly? I want to program a similar game (for Desktop with C#) but I have no idea how to manage the server sided constructions with efficient multi-threading.

Was it helpful?

Solution

This is a very complicated topic actually :)

The simple answer is that you have only 1 main game thread that actually runs the game. Each player opens a connection to the server. The server can handle each connection in a thread or more commonly will use non-blocking sockets with a single network thread.

In C# I have used the async socket methods which use IOCP behind the scenes - this means you roughly have 1 thread managing the actual sockets and then a thread pool actually delegating to handle the data once its in memory.

If this isn't making much sense to you I recommend doing some reading on game design and networking. I would recommend using something like lidgren if you don't want to write your own networking code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top