News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Question about IOCP

Started by dicky96, October 17, 2006, 03:55:38 PM

Previous topic - Next topic

dicky96

Hi guys
thanks for previous help regards IOCP. I've done quite a bit more studying on this topic and think I'm about ready to write some code. (At least) one thing is still confusing me.  I read on msdn an article which says that you should not use your worker threads to post AcceptEX messages.

thsi is the article  http://msdn.microsoft.com/msdnmag/issues/1000/winsock/

quote
It may seem that the logical thread to post AcceptEx requests is one of the worker threads that is associated with the completion port and involved in processing other I/O completion notifications. However, recall that a worker thread should not execute a blocking or high-latency system call if such an action can be avoided. One of the side effects of the layered architecture of Winsock2 is that the overhead to a socket or WSASocket API call may be significant. Every AcceptEx call requires the creation of a new socket, so it is best to have a separate thread that posts AcceptEx and is not involved in other I/O processing. You may also choose to use this thread for performing other tasks such as event logging.

But is this overhead really that great?

I also read on CodeProject this article http://www.codeproject.com/internet/iocp.asp
This proposes a technique of creating 500 (or however many) sockets before starting to listen. In this case could you just post AcceptEX from the IO worker thread without incurring such a high overhead as the sockets are already created?

So what is the best strategy here?  One thought that came to my mind is post some AcceptEX requests before starting to listen for clients, keep a global variable storing the number of AcceptEX posted, decrement this variable in my IO worker thread each time a socket is used, then post a custom windows message to trigger another thread that creates more sockets and AcceptEx requests if the variable drops below a certain number.  Is that a resonable method to use?

Any help greatly appreciated, i've not programmed IOCP before
dicky

James Ladd

It's all a question of responsiveness and resource usage.

The approach I would take is to have a thread dedicated to creating sockets and calling AcceptEx on them.
When AcceptEx completes it would associated the socket with the IOCP facilities and go back to
accepting again.

This approach ensures that new connections are served in a timely manner without waiting for one of the
other worker threads to become free to service a connection request.

You should also have a scavenger thread that checks each socket to see if they have ever sent or received data.
If they havent after a period of say 5 minutes it should forcebly close the socket.
This stops DOS attacks where a client opens a connection but never does anything with it, eventually using all
the connections and denying new connections to the server.

For this reason the thread that does AcceptEx requests should put a time limit on the call.

I hope this helps.

Rgs, James.

dicky96

Yes James, thanks, your advice always appreciated

The thread that is creating sockets and accepting connections, should this just be allowed to block all the time on Accept until a client connects, do it's stuff, then block on accept again (rather than my notion of using a global variable to monitor the number of acceptEx outstanding in my IO worker therad and posting a custom Windows Message if it falls too low)

The scavenger thread - yes I understand the need for such an approach. How am I best to trigger this thread into action (I assume it should just run every now and then) do I just use  a timer?

I'm quite new to multithreaded programming (only wrote one app using it so far) this project is the biggest I've attempted so far - OK I know you are supposed to walk before you run, but the worse that could happen is I trip up a few times  :lol

dicky