Mixing messages and socket operations

Sean Kelly sean at invisibleduck.org
Tue Mar 11 17:04:45 PDT 2014


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.


More information about the Digitalmars-d-learn mailing list