Results 1 to 4 of 4

Thread: Non-blocking stream read

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Location
    Italy
    Posts
    69
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Non-blocking stream read

    Hello,
    does Qt provide some way for reading something from a stream (qtextstream, associated with stdin/stdout) in a non-blocking fashon?

    Here's what i've to do:
    my application checks every N msec if new data has arrived - a timer calls readData(). This method should check if there are new data on the stream for reading. If there aren't, just return, else read the data and process them.

    I'm not using threads and i'd like to avoid them.
    There is some way? (maybe using tell() or something similar?)
    I tried with bytesAvailable() but returns always 0...

    Thanks!

    Edit:
    pos() and size() return always 0, isSequential() returns true, but bytesAvailable() is always 0, too.
    Last edited by akiross; 15th April 2009 at 02:26.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Non-blocking stream read

    No, the actual reading is performed in a synchronous manner. You can use a QSocketNotifier with stdin to get a signal based notification upon data arrival.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    akiross (15th April 2009)

  4. #3
    Join Date
    Feb 2007
    Location
    Italy
    Posts
    69
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Non-blocking stream read

    Thanks, it was exactly what I was needing
    Actually I solved my problem, but there's one more question: how can I know if there are pending data on stdin?

    Actually, my parsing function is blocking:
    Qt Code:
    1. cin >> p;
    To copy to clipboard, switch view to plain text mode 
    Will block if the point isn't complete (it have a syntactic structure like "(x,y)" ).

    It would be nice if I could read the partial data as they are notified, put them in a buffer and analyze the buffer with a regex: if full data are present, I can use the parsing function without blocking. The parsed data are consumed and the incomplete data will be added at the next notification (by QSocketNotifier of course).

    This would allow me to read data like:
    Qt Code:
    1. while (buffer.indexOf(regex) != -1) {
    2. QPoint p;
    3. buffer >> p;
    4. }
    To copy to clipboard, switch view to plain text mode 

    I was thinking to do this by keeping a QString buffer and QTextStream on it, but there is a problem: if I read all the data from the stream (like readAll()), it will block, because stdin is always open and waiting for new data:

    Qt Code:
    1. QTextStream input(/* using stdin */);
    2. static QString buffer;
    3. ...
    4. void notification(int stream) {
    5. buffer += input.readAll(); // Blocks here
    6. // Check the buffer using QRegex and extract all the available data
    7. }
    To copy to clipboard, switch view to plain text mode 

    Do you have any advice on how to do this?

    Thanks wysota, you're always helpful!

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Non-blocking stream read

    If you use a socket notifier, you will be notified upon arrival of new data. Then you can use the IODevice you are using or QTextStream to read from stdin to do the actual reading.

    Qt Code:
    1. QFile stdinfile;
    2. stdinfile.open(0, QFile::ReadOnly);
    3. //...
    4. int pending = stdinfile.bytesAvailable();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QFile can't read a file
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2009, 20:24
  2. External Lib read from QBuffer on Calback problem
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2008, 19:43
  3. Phonon Media Object blocking on play()
    By traetox in forum Qt Programming
    Replies: 0
    Last Post: 21st May 2008, 05:43
  4. How to read CD with read?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 29th June 2007, 08:20
  5. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.