dispatch source for multi-client sockets
How does one create a dispatch source that responds to multiple connections on the same socket, that is, listening on a socket with a backlog larger than 1? In the online doc it seems that you should have a connected socket to read from, the same way as reading from a file. But documentation also says you need a socket returned from listen(). So where does the accept() call fit into this? Specifically it would be nice to avoid blocking on accept() before creating a read source. I found this echo server example based on kqueue events which seems to receive a read event. First the handler will accept the socket, then do normal read processing. Is this also how it should work with libdispatch, and if so, what about other platforms. http://www.monkeys.com/freeware/kqueue-echo.c And how about similar event sources for DNS lookup? Libevent did a bit of work on these issues, and I see it has already been mentioned on this mailing list. Related: I don't see any examples of custom event sources - but I thought that might also be a way to schedule client handlers? Finally, what is the policy on busy loops and locking operations? One could for example push a blocking socket accept() call onto a queue, but this consumes a thread from a thread pool that is designed to reflect number of cpus not number of long running tasks. Alternatively one could create a dedicated thread for this purpose, but that doesn't seem very elegant. Regards, Mikkel
Le 22 sept. 2009 à 12:15, Mikkel Fahnøe Jørgensen a écrit :
How does one create a dispatch source that responds to multiple connections on the same socket, that is, listening on a socket with a backlog larger than 1?
In the online doc it seems that you should have a connected socket to read from, the same way as reading from a file. But documentation also says you need a socket returned from listen().
So where does the accept() call fit into this? Specifically it would be nice to avoid blocking on accept() before creating a read source.
You mean, like a Web server ? http://developer.apple.com/mac/library/samplecode/DispatchWebServer/index.ht...
-- Jean-Daniel
Since the event sources are built in kqueues, a listen's sockets read event indicates a pending connection. Another example is <http://www.opensource.apple.com/source/libdispatch/libdispatch-84.5.1/exampl...
Note that this example, by design, suspends the listening queue to prevent new connections from being accepted in parallel, but you would want to remove that restriction... Shantonu Sent from my MacBook On Sep 22, 2009, at 3:49 AM, Jean-Daniel Dupas wrote:
Le 22 sept. 2009 à 12:15, Mikkel Fahnøe Jørgensen a écrit :
How does one create a dispatch source that responds to multiple connections on the same socket, that is, listening on a socket with a backlog larger than 1?
In the online doc it seems that you should have a connected socket to read from, the same way as reading from a file. But documentation also says you need a socket returned from listen().
So where does the accept() call fit into this? Specifically it would be nice to avoid blocking on accept() before creating a read source.
You mean, like a Web server ?
http://developer.apple.com/mac/library/samplecode/DispatchWebServer/index.ht...
-- Jean-Daniel
_______________________________________________ libdispatch-dev mailing list libdispatch-dev@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/libdispatch-dev
participants (3)
-
Jean-Daniel Dupas
-
Mikkel Fahnøe Jørgensen
-
Shantonu Sen