Re: sockets blocking - nonblocking
Chris Angelico <[email protected]>
| Newsgroups | gmane.comp.lang.pike.user |
|---|---|
| Message-ID | <CAPTjJmow5cwjFCTTgnehCcW2PN1vVcHBxP2qdNup0xTN2DTXmQ@mail.gmail.com> |
On Wed, Oct 1, 2014 at 5:28 AM, Marc dirix <[email protected]> wrote: > The write works just fine, the server receives the request, and tries to > respond. However the read does not return any data (it just blocks). > > If I kill the server (probably causing the connection to close an then the > read to return which causes the client to start nonblocking mode again, I > suddenly receive the requested data. int written = socket::write(towrite); while ( written < sizeof(towrite) ) { towrite = towrite[written..]; written = socket::write(towrite); } Even if the write is technically working, this is an inefficient way to do it. In non-blocking mode, this will spin as soon as it's given more than it can write in one go. I have a generic kernel for handling async I/O in Pike: https://github.com/Rosuav/Hogan That might be good for some ideas, or you may find you can make use of it directly. (Don't hesitate to ask for features, if there's something it lacks which you desperately need - or just would find handy.) It's single-threaded and handles everything asynchronously; you just write your callback function and it'll get called as appropriate. On systems that support it, it even handles SIGHUP for code reloading, all without you needing to write a single line of code :) I think the main trickiness in your code is the way it switches from non-blocking to effectively-blocking mode, which (if I'm reading your code correctly) will spin the server's one and only thread. What exactly do you need to synchronize? Do you need to prevent the handling of any further requests until you get a response to this one? Or do you simply need to provide a return value? Either way, I'd look into a way of handling it completely asynchronously; either queue requests until the right response comes in, or pass a callback rather than using a return value, respectively. ChrisA