Jump to content
  • Advertisement
Sign in to follow this  
Afr0m@n

Good IOCP design

This topic is 4513 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Could someone please give me some hints here? I've just started messing with IOCP in C#, but I'm feeling quite unsure about my own design and the code I'm writing. The scheme I have planned so far is as follows: All threads (20 or so) in a threadpool will continiously listen for incoming connections and listen for activity on the IOCP. If there's a new incoming connection, the threads that listen will dispatch a new socket to the IOCP, which will be picked up by the threads that listen to the IOCP. The threads that listen to the IOCP will then proceed to asynchronously read some data, and determine based on a huge select-case structure what to do with the data read (including determining if more data should be read). If there's no data to be read, the socket will be dispatched to the IOCP again, only to be picked up by yet another thread - or the same one. The same thing happens if data was read and could be processed correctly. Is this a good scheme (I.E will it make for scalable server software)? Are there better schemes out there, and if so - which ones? Thanks in advance! Edit: And YES, I'm making an MMORPG, and NO - I'm not quitting - especially when I've come this far (seeing as I have IOCP code that actually runs (not that it's much yet, but still...), and a design scheme for my server).

Share this post


Link to post
Share on other sites
Advertisement
For TCP, that sounds about like how you'd use IOCP. Remember that IOCP is not portable (Windows only), if you later want to get cheaper, more reliable online hosting...

I wouldn't spawn more threads than I have CPUs, though, plus maybe one extra. 20 threads sounds vastly too much.

Share this post


Link to post
Share on other sites
Thanks alot! :)

One question: Why is 20 too much? I've spawned 20 on my machine now, and it's going smooth as melted butter.

Share this post


Link to post
Share on other sites
> I wouldn't spawn more threads than I have CPUs,
> though, plus maybe one extra.

That is usually the minimum with IOCP. It depends on the load and how much work each thread has to perform. In the good old days of DirectPlay, you could easily blow more than 20 threads on the server side. Here is an article that describes how to tune your server:

http://www.microsoft.com/mscorp/corpevents/meltdown2001/ppt/DPScaleNPerf.ppt ( Clicky )

Keep in mind that this is DirectPlay8, so the comments and recommendations also covered the Win98 case which didn't have IOCP; and DP8 used UDP not TCP. But the test cases they showcased was based on Win2K so this is IOCP underneath. Nonetheless, it's a good read for the methodology.

-cb

Share this post


Link to post
Share on other sites
Thanks guys!

kuphryn:

I am spawning a threadpool witch exactly 20 threads, which I dispatch the listening and socket handling functions onto.

cbenoi1: Thanks! Will have a look, definately! :)

hplus0603: Wait - I just came to think of something; How does IOCP deal with the fact that the fd_set can't be bigger than 64 on Windows? Or do I have to still add code to manually extract ready sockets, or is this done for me in C#?

Share this post


Link to post
Share on other sites
Hi,
I have a good implementation for GetNet server at

http://www.zalsoft.com/index.php?page=Getae2&subpage=GeticNet.html

The code is portable.
Is using a pool of threads loused-coupled on queues
on wich the receiver/sender thread places/gets the data.
Overlapped IO performances are as good as the Completion port are.

Share this post


Link to post
Share on other sites
It's not a question of "why not use 20 threads" it's a question of "why use 20 threads". That many threads is just going to waste resources for no particular reason. Depending on exactly how you're doing things the vast majority of your threads will sit idle the vast majority of the time. And if you do somehow manage to get them all woken up then as soon as your awake thread count goes over your processor thread count you're worse off then you would have been with a single threaded model. It'll also be harder to debug when things go wrong because you have that many more threads to ponder over.

IOCP does not know or care about fd_set. It is not built around select() and thus isn't subject to its limitations.

Having a first-draft IOCP implementation running is not particularly far along on your way to a MMORPG... By all means continue if you wish to do though.

Share this post


Link to post
Share on other sites
Quote:
IOCP does not know or care about fd_set. It is not built around select() and thus isn't subject to its limitations.


Correct. In addition, there is no 64-socket limit to select() on Windows; only a limit to the default header file structures. See the Forum FAQ for more information.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!