Is there any way of checking if I can read from Pipe: without it blocking (due to no data)?
I found SetBlockingMode(), but (1) it has the wrong semantics (I can't check a Read() will succeed without calling Read(), thus getting data I don't want yet), and (2) it's OS4-only (ideally I'd like something compatible with OS3).
WaitForChar() should work with a pipe IMHO.
@thomas
Thanks! That looks like exactly what I want :-)
Author of the PortablE programming language.
I love using Amiga OS4.1 on my X1000 & Sam440 :-D
If you are programming for older versions of AmigaOS, ISTR that WaitForChar()
has a checkered past with respect to PIPE:.
Check out the documentation file "queue-handler.doc" delivered with AS updates, probably residing
in "sys:documentation/handlers/" (but not in the SDK Autodocs.
quote:
--------------------------
BUGS
Support for the ACTION_WAIT_CHAR packet was unreliable if a timeout
value > 0 was given in that the ACTION_WAIT_CHAR packet would always
return immediately, treating any timeout > 0 like a timeout of 0
microseconds. This bug is present in version 51 and 52.1, and was
fixed in version 52.2.
When used with a NOBLOCK reading pipe, the ACTION_READ packet
would always fail. ACTION_READ will now read much data as possible
and return ERROR_WOULD_BLOCK or ERROR_BROKEN_PIPE as appropriate.
This was fixed in 53.3.
----------------------------
I'm not sure that WaitForChar() ever worked on AOS 3, but can't find anything right now.
Tom B
@thomas
I've run into one problem: When I reach the EOF (of the stream/pipe) WaitForChar() waits for the full time-out period before returning :( . I'm trying to think of a work-around for this - any ideas?
Author of the PortablE programming language.
I love using Amiga OS4.1 on my X1000 & Sam440 :-D
Just found this piece of code/example in SDK, but for 'WaitForData()', maybe it helps you somehow.
AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P
@jabirulo
It would have been exactly what I wanted... if it wasn't OS4-only :-/ .
@thomas
I've thought of a work-around for WaitForChar() waiting the full timeout at EOF, which will hopefully work: A pipe only generates an EOF when the other end of the pipe has been closed. And that is closed only when the program using it has quit. Therefore I can call WaitForChar() UNLESS the program has quit. And in fact there is no need to call WaitForChar() when the program has quit, because the pipe contains all the data it will ever receive from that program! So this seems like a fairly reliable solution, but I have not tested it yet...
Author of the PortablE programming language.
I love using Amiga OS4.1 on my X1000 & Sam440 :-D
@thomas
The idea I described in my previous post did NOT work. It seems that Curl takes a while to quit, after finishing downloading, and this allows enough time for my program to call WaitForChar() - which then hangs.
This also explains better why WaitForChar() hangs at the end of the file... it's not directly to do with EOF, but rather just that Curl hasn't closed the Pipe yet, so of course WaitForChar() would wait. BUT it still seems to be a bug, because Curl does eventually close the Pipe, yet WaitForChar() CONTINUES waiting.
GOOD NEWS: I've implemented a solution which does work (99.93% of the time anyway!). What I do is see how many bytes Read() returned, compared to the requested (buffer) size, and if it is less then I assume I've reach the EOF so WaitForChar() will no-longer be called (it'll still perform one last Read to ensure it really is EOF, as indicated by Read() returning 0 bytes).
@jabirulo
It appears that WaitForData() has exactly the same bug as WaitForChar(), so it's no help. Probably WaitForChar() uses WaitForData() internally on OS4.
Author of the PortablE programming language.
I love using Amiga OS4.1 on my X1000 & Sam440 :-D
Chris, you said you wanted to know if data available to read. This implies a brief poll, so I wonder, what about passing a zero value as time-out to WaitForChar() ? Does this not work for a check?
@hypex
Yes, that would work. I don't recall the sequence of events, or why I phrased it the way I did, but anyway I now need to both poll & wait for data to become ready. Polling is more important, but waiting is very useful too.
Author of the PortablE programming language.
I love using Amiga OS4.1 on my X1000 & Sam440 :-D