deprecating std.stream, std.cstream, std.socketstream

Adam D. Ruppe destructionator at gmail.com
Wed May 16 10:49:14 PDT 2012


tbh, I've found byChunk to be less than worthless
in my experience; it's a liability because I still
have to wrap it somehow to real real world files.

Consider reading a series of strings in the format
<length><data>,[...].

I'd like it to be this simple (neglecting priming the loop):

string[] s;
while(!file.eof)) {
     ubyte length = file.read!ubyte;
     s ~= file.read!string(length);
}


The C fgetc/fread interface can do this reasonably
well.

string[] s;
while(!feof(fp)) {
    ubyte length = fgetc(fp);
    char[] buffer;
    buffer.length = length;
    fread(buffer.ptr, 1, length, fp);
    s ~= assumeUnique(buffer);
}


But, doing it with byChunk is an exercise in pain
that I don't even feel like writing here.




Another problem is consider a network interface. You
want to handle the packets as they come in.

byChunk doesn't work at all because it blocks until it
gets the chunk of the requested size.

foreach(chunk; socket.byChunk(1024))


suppose you get a packet of length 1000 and you have
to answer it. That will block forever.

So, if you use byChunk as the underlying thing to fill
your buffer... you don't get anywhere.


I think a better input primitive is byPacket(max_size).
This works more like the read primitive on the operating
system.

Moreover, I want it to buffer, and control how much is consumed.


auto packetSource = socket.byPacket(1024);
foreach(packet; packetSource) {
    // as soon as some data comes in we can get the length
    if(packet.length < 2) continue;
    auto length = packet.peek!(ushort); // neglect endian for now
    if(packet.length < length + 2) continue; // wait for more data

    packet.consume(2);
    handle(packet.consume(length));
}



In addition to the byChunk blocking problem...
what if the length straddles the edge?



byChunk is just a huge hassle to work with for every file
format I've tried so far. byLine is a little better
(some file formats are defined as being line based)
but still a bit of a pain for anything that can spill
into two lines.


More information about the Digitalmars-d mailing list