Sign in to follow this  

Good IOCP design

This topic is 4298 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
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
> 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
Quote:
Original post by hplus0603
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...


If I don't use IOCP and I would like to do something like IOCP, how can I do this on BSD socket interface?

Share this post


Link to post
Share on other sites
Which flavor of BSD (or Linux)? There are variants of the poll interface, which allows you to do similar things. However, IOCP "proper" is a Windows kernel primitive, that you won't find elsewhere, and you can't built it in a reasonable way on top of another interface.

Share this post


Link to post
Share on other sites
The following is a good reference for non-Windows systems:

http://www.kegel.com/c10k.html ( Clicky )

You should look up "aio, posix, kqueue" on your favorite search engine to find out more details and sample source code.

-cb

Share this post


Link to post
Share on other sites
Thanks guys! Appreciate it. I've decided to try to make my application work with as few threads as possible initially, and then to increase the number of threads later on when there are more users. After all, if the server can handle few threads, it shouldn't be a problem adding more threads either, so long as I increase the number of CPUs. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by cbenoi1
The following is a good reference for non-Windows systems:

http://www.kegel.com/c10k.html ( Clicky )

You should look up "aio, posix, kqueue" on your favorite search engine to find out more details and sample source code.

-cb


Thank you cbenoi1, this reference is very useful :->

Share this post


Link to post
Share on other sites

This topic is 4298 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.

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