Thread overview
Async read an output stream but how many bytes ?
Mar 31, 2016
Basile B.
Mar 31, 2016
Adam D. Ruppe
Mar 31, 2016
Basile B.
Mar 31, 2016
Basile B.
March 31, 2016
Hey, I have a class that wraps a process and implements two events. One of them is called after poll() for example if new data are available.

The event is called correctly but I don't know how exactly how to read the data:

- per buffer of fixed size ?
- using the stream information ?

If i try to read until nothing is available anymore, the event handler returns at the end of the process (instead of being called several times): eg this helper function of the process that the event handler can call to read:


    bool readOutput(T)(ref T t)
    if (isSomeString!T)
    {
        bool result;
        T buffer;
        while (true) // or (!output.eof)
        {
            auto cnt = output.readln(buffer);
            if (cnt)
            {
                t ~= buffer;
                result = true;
            }
            else break;
        }
        return result;
    }


the events are called in the callback of a thread-based timer:


    void check(Object notifier)
    {
        if (_ppid.stdout.eof)
        {
            _checker.stop; // the thrad-based timer
            if(_onTerminate)
                _onTerminate(this);
        }
        else
        {
            // not sure if something else must be done to poll properly ?
	    pollfd pfd = { _ppid.stdout.fileno, POLLIN };
	    if (poll(&pfd, 1, 0) == 1 && _onOutputBuffer)
                _onOutputBuffer(this); // the handler can call readOutput
        }
    }

There should be a way to know how many bytes are available ?
March 31, 2016
On Thursday, 31 March 2016 at 00:50:16 UTC, Basile B. wrote:
> There should be a way to know how many bytes are available ?

You are already using poll()... I'd just use read() directly on the file number too. It will read as much as is available up to the max size of the buffer, but if it can't fill it all, it just does what it can and returns.
March 31, 2016
On Thursday, 31 March 2016 at 01:12:50 UTC, Adam D. Ruppe wrote:
> On Thursday, 31 March 2016 at 00:50:16 UTC, Basile B. wrote:
>> There should be a way to know how many bytes are available ?
>
> You are already using poll()... I'd just use read() directly on the file number too. It will read as much as is available up to the max size of the buffer, but if it can't fill it all, it just does what it can and returns.

It's probably the only way. I've also tried core.stdc.stdio.readf() in a loop and it was also reading until the process termination. So I'll go for a single readf operation on a ubyte[].
March 31, 2016
On 3/30/16 10:06 PM, Basile B. wrote:
> On Thursday, 31 March 2016 at 01:12:50 UTC, Adam D. Ruppe wrote:
>> On Thursday, 31 March 2016 at 00:50:16 UTC, Basile B. wrote:
>>> There should be a way to know how many bytes are available ?
>>
>> You are already using poll()... I'd just use read() directly on the
>> file number too. It will read as much as is available up to the max
>> size of the buffer, but if it can't fill it all, it just does what it
>> can and returns.
>
> It's probably the only way. I've also tried core.stdc.stdio.readf() in a
> loop and it was also reading until the process termination. So I'll go
> for a single readf operation on a ubyte[].

stdio.readf is buffered. It does not deal with async io properly I think.

-Steve

March 31, 2016
On Thursday, 31 March 2016 at 13:48:46 UTC, Steven Schveighoffer wrote:
> stdio.readf is buffered. It does not deal with async io properly I think.
>
> -Steve

Yes, I must use core.sys.posix.unistd.read, which was actually what A.D.Ruppe suggested, I think.