streaming redux

Dmitry Olshansky dmitry.olsh at gmail.com
Fri Dec 31 04:39:13 PST 2010


On 31.12.2010 11:35, Daniel Gibson wrote:
> Am 30.12.2010 22:59, schrieb Dmitry Olshansky:
>> On 28.12.2010 16:08, Daniel Gibson wrote:
>> [snip]
>>> ## UnbufferedInputTransport:
>>>
>>> I'd like "void readFully(ubyte[] buffer)" which reads buffer.length
>>> bytes or throws an exception if that is not possible
>>> This would also fix busy-waiting (it'd block until buffer.length bytes
>>> are available).
>>>
>>> Also "size_t read(void* buffer, size_t length)" (and the same for
>>> readFully()) would be nice, so one can read from the stream to buffers
>>> of arbitrary type without too much casting. Is probably especially
>>> handy when used with (data from) extern(C) functions and such.
>>>
>>> Also, for convenience: "ubyte[] read(size_t length)" (does something
>>> like "return read(new ubyte[length]);"
>>> and "ubyte[] readFully(size_t length)"
>>
>> This, I guess, would be provided by free functions in the same module,
>> there is no point in requiring to implement them inside the stream 
>> itself.
>
> Maybe for the convenience functions, but what about readFully()?
> Could the stream not support a non-blocking read that reads up-to 
> buffer.length bytes and a blocking read that blocks until 
> buffer.length bytes are read?

I meant something like this (assuming call  to t.read blocks):
//reads exactly buf.length bytes, not counting some extra that might 
reside in the internal buffer
ubyte[] readFully(BufferedInputTransport t, ubyte[] buf) //changed 
signatures to prevent allocations
auto dst = buf[];
while(!dst.empty){
     auto res = t.read(dst, dst.length);
     dst = dst[res.length..$];
}
return buf;
}
Also that would be pig slow without buffering. The internal 
implementation of BufferedTransport may use non-blocking IO to keep 
reasonable buffer fill rate.
> If you want to read whole ints, floats, shorts, ... you need something 
> like that anyway, because only one byte of an int doesn't help you at 
> all. But because the stream may support something like this natively, 
> it makes sense to have readFully() here and not in Unformatter.
>

>>>
>>> ## UnbufferedOutputTransport:
>>>
>>> I'd like "void write(void *buffer, size_t length)" - for the same
>>> reason as read(void* buffer, size_t length).
>>
>> Ditto
>>
>
> Cheers,
> - Daniel
-- 
Dmitry Olshansky



More information about the Digitalmars-d mailing list