Good IOCP design
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).
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.
I wouldn't spawn more threads than I have CPUs, though, plus maybe one extra. 20 threads sounds vastly too much.
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.
One question: Why is 20 too much? I've spawned 20 on my machine now, and it's going smooth as melted butter.
> 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
> 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
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#?
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#?
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.
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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement