Io module

wait_readable

procedureextensionexperimental2 overloadsSource

Waits until read would not wait: bytes have arrived, the peer closed its end, or the connection failed (read then says which). Nothing is consumed and no buffer is needed, so a fiber can wait for a quiet connection and take its memory only when there is something to read. Ends with .TIMED_OUT or .CANCELED like any other wait, and .CLOSED if the socket is closed meanwhile.

Example↗
Io :: #import "Extensions/Io";

Io.io_run(() {
    listener, _ := Io.tcp_listen("127.0.0.1:0");
    defer Io.close(listener);

    address, _ := Io.local_address(listener);
    client, _ := Io.connect(address);
    server, _ := Io.accept(listener);
    defer Io.close(client);
    defer Io.close(server);

    Io.write(client, "x");

    assert(Io.wait_readable(server) == .NONE); // data is waiting, nothing consumed
    buffer: [4] u8;
    count, _ := Io.read(server, buffer);
    assert(count == 1);
});
wait_readable :: (stream: Tcp_Stream) -> Io_Error

Parameters

stream: Tcp_Stream

Returns

wait_readable :: (reader: Reader) -> Io_Error

Waits until a read would return at once: bytes are there, the source ended, or it failed (the read then reports which). It takes no buffer and consumes nothing, so a connection can sit idle without holding memory and allocate only when something arrives. Deadlines and cancel end it like any other wait. .UNSUPPORTED for a reader that cannot tell (a file, a pipe, a source of your own without wait_proc): can_wait_readable says beforehand.

Parameters

reader: Reader

Returns

See also