Sign in to follow this  
Codejack

Multi-Threaded Design Question

Recommended Posts

Codejack    100
Hi Guys, OK I am thinking about a design for a multi-threaded game server that serves about 500 players at most. In generalities, I am thinking there will be three threads in this server: 1. The "Listen" thread that listens for new connections and passes them to the connection manager. 2. The "Connection Manager" thread that polls sets of sockes for incoming data from the connected clients. This passes the data to the third (Game) thread. 3. This is the main thread that processes the information from the clients, updates the game and contains the game loop. I have decided not to go for a thread per user as I think this will cause insurmmountable perfromance problems with all the thread-locking of resources etc. Is this a reasonable assumption? How would I handle a user that submits a request that involves a long database query without it typing up the "Connection Manager" thread?

Share this post


Link to post
Share on other sites
CmpDev    100
TCP or UDP?
I suspect TCP due to having a listen does this also mean it has its own port.
Generally two threads are enough one for the game and one for the network.

Quote:
I have decided not to go for a thread per user as I think this will cause insurmmountable perfromance problems with all the thread-locking of resources etc. Is this a reasonable assumption?

Yes, but not only for this reason as you will have thrashing due to the switches.

Quote:
How would I handle a user that submits a request that involves a long database query without it typing up the "Connection Manager" thread?

If this is really a concern then you could use a pool of threads, or a design pattern which uses a pool. What lookup would take a long time?

Share this post


Link to post
Share on other sites
Antheus    2409
Is this Windows? Then IOCP is what you'll need.

For other platforms, similar methods exist.

One thing you can use for optimal processing is the reactor pattern.

As for database queries, those should be fully asynchronous. When you need to do something like this, construct the request, and submit it to database engine and move on. When it's completed, the database engine will make a callback, at which point you respond to the client.

General rule of a thumb is to use one thread per 100 clients for network IO.

The actual choice of threads and their roles depends on logic of the server. Is it synchronous, request/response, publish/subscribe?

Generating response in network thread might be completely unsuitable for this purpose, but it depends on architecture.

Share this post


Link to post
Share on other sites
Codejack    100
Thanks for your response guys. Yes, I would use the Windows platform with a MSSQL database. The layer four protocol is most likely going to be TCP or a reliable UDP setup (like the Raknet package). I think this will be Ok as I am not intending to make a full fledged action game.

The type of game I am thinking about will be a small, slow-paced RPG and community type game (i.e. point and click interface, chat functionality and an event-driven world (e.g. OnClick, OnInspect, OnPickUp, OnPutDown etc on world objects)).

I am actually thinking about coding the event logics in Python and have Python interact with the database and pass the returned information to the C++ application to transmit to the user. I guess this fits the request/response model.

I will look in to I/O Completion Ports and the Reactor pattern - thanks for this information. I am just trying to get a clear idea in my head how this will work :)

Share this post


Link to post
Share on other sites
makaze1    122
Ok, I figured I'd ask here instead of making a new post. I recently took a couple of weeks and made a MUD server. After doing some reading online, I'm starting to realize I used way too many threads. It had the connection thread, a thread for each room, and each player had a thread. Starting to see why thats bad. So I've been thinking about making a simple server and game client. I'm hesitant to say "mmo", because at least at first, I'm only making this to get more familiar with programming a server.

Now for my questions. For my MUD, I used TCP. Only a string of text is sent, and connection speed really isnt a problem. So I've read that UDP is a better choice for an online game, even a "rpg" (I use quotes because I'm just looking at making a framework pretty much). Then, packet structure. I send the size of the packet before the full packet, then put what? A small structure, with a command type and any modifiers? (the text when trying to talk, etc).

Also, threads. I do like the idea of multithreading. One thread that waits for a client to connect, checks the user name/password, and hands it off to the game. Does each zone of the game need its own thread? Or run through the zone list and handle each one one at a time?

I apologize for the wall of text, but thanks a ton if you read through it ^^;

Share this post


Link to post
Share on other sites
kyoryu    224
I don't see any particular need to use threading for the architecture you're talking about.

It will add significant complexity with very little actual benefit.

Share this post


Link to post
Share on other sites
hplus0603    11347
One thread per zone allows you to scale to multi-CPU systems for larger worlds. This is assuming that zones are computationally independent (no locking required).

One process per zone might be even better, as it allows you to scale to multiple physical machines. It requires the client to re-connect to a different machine when zoning, though.

I don't think sign-on is necessarily a great thing to put in a thread, because it's neither computationally intensive, nor requiring especially low latency -- those are the two main criteria to use threads, IMO. However, if that's the only extraneous thread you create in your system, you'll probably do OK.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this