Possible to check if Pipe: has data available to read?

10 posts / 0 new
Last post
ChrisH
ChrisH's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2010-12-07 20:22
Possible to check if Pipe: has data available to read?

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).

thomas
thomas's picture
Offline
Last seen: 1 hour 48 min ago
Joined: 2011-05-16 14:23
WaitForChar() should work

WaitForChar() should work with a pipe IMHO.

ChrisH
ChrisH's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2010-12-07 20:22
@thomas Thanks! That looks

@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

tbreeden
tbreeden's picture
Offline
Last seen: 6 years 1 week ago
Joined: 2010-12-09 03:10
If you are programming for

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

ChrisH
ChrisH's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2010-12-07 20:22
@thomas I've run into one

@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

jabirulo
jabirulo's picture
Offline
Last seen: 1 day 18 hours ago
Joined: 2013-05-30 00:53
Just found this piece of

Just found this piece of code/example in SDK, but for 'WaitForData()', maybe it helps you somehow.

  1. /* Check if a stream is ready for reading; wait for up to 5 seconds. */
  2. int32 ready;
  3.  
  4. ready = IDOS->WaitForData(stream,WFDF_READ,5 * 1000);
  5. if(ready == WFDF_READ) {
  6. IDOS->PutStr("data is available for reading.\n");
  7. }
  8.  
  9. /* Test quickly if a stream can be read/written. */
  10. ready = IDOS->WaitForData(stream,WFDF_READ|WFDF_WRITE,0);
  11. if((ready & WFDF_READ) != 0) {
  12. IDOS->PutStr("data is available for reading.\n");
  13. }
  14.  
  15. if((ready & WFDF_WRITE) != 0) {
  16. IDOS->PutStr("data can be written.\n");
  17. }
  18.  
  19. if(ready == 0){
  20. if( ERROR_ACTION_NOT_KNOWN == IDOS->IoErr() ) {
  21. IDOS->PutStr("handler does not support WaitForData()\n");
  22. }
  23. }

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

ChrisH
ChrisH's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2010-12-07 20:22
@jabirulo It would have been

@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

ChrisH
ChrisH's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2010-12-07 20:22
@thomas The idea I described

@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

hypex
hypex's picture
Offline
Last seen: 1 month 2 weeks ago
Joined: 2011-09-09 16:20
Chris, you said you wanted to

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?

ChrisH
ChrisH's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2010-12-07 20:22
@hypex what about passing a

@hypex

what about passing a zero value as time-out to WaitForChar()

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

Log in or register to post comments