www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixing messages and socket operations

reply "Andre Kostur" <andre kostur.net> writes:
Hi, I'm trying a prototype project at work and have been trying 
to find a good example of network programming in D.  What I'm 
trying to do is have a separate thread to deal with the socket 
(calls .accept() for example), but I'd also like the thread to be 
responsive to the OwnerTerminated message coming from the parent 
thread (the daemon is shutting down, time to orderly tear down 
the threads).   However, .accept is blocking, as is receive().  
I'd rather not have to resort to polling each of the two.  What's 
the community's standard approach to this problem?
Mar 11 2014
parent "Sean Kelly" <sean invisibleduck.org> writes:
On Tuesday, 11 March 2014 at 14:44:51 UTC, Andre Kostur wrote:
 Hi, I'm trying a prototype project at work and have been trying 
 to find a good example of network programming in D.  What I'm 
 trying to do is have a separate thread to deal with the socket 
 (calls .accept() for example), but I'd also like the thread to 
 be responsive to the OwnerTerminated message coming from the 
 parent thread (the daemon is shutting down, time to orderly 
 tear down the threads).   However, .accept is blocking, as is 
 receive().  I'd rather not have to resort to polling each of 
 the two.  What's the community's standard approach to this 
 problem?
To make accept() non-blocking, you can wait on the accept socket using select or poll. When the socket becomes readable, you're ready to listen. Then you could set a timeout for the select call and do a receiveTimeout(0). Things get trickier if you don't like the busy wait approach though. You'd need to have a file descriptor signaled when a message arrived or a message sent on a socket event (I'd favor the former). I can imagine sorting this out using the new Scheduler stuff in std.concurrency, but an easier approach might be to just use vibe.d, which integrates message passing and socket events already.
Mar 11 2014