Stream

Provides stream operations.

libuv reference: http://docs.libuv.org/en/v1.x/stream.html

Input Events

UV_STREAM_LISTEN

input (_uv_stream_t&&, int) UV_STREAM_LISTEN;
  • Occurrence:
    • Whenever a stream server receives an incoming connection.
  • Payload:
    • _uv_stream_t&&: pointer to the stream server

libuv reference: http://docs.libuv.org/en/v1.x/stream.html#c.uv_connection_cb

UV_STREAM_CONNECT

input (_uv_connect_t&&, int) UV_STREAM_CONNECT;
  • Occurrence:
    • Whenever a connection opens.
  • Payload:
    • _uv_connect_t&&: pointer to the connection
    • int: open status
      • 0: success
      • <0: error

libuv reference: http://docs.libuv.org/en/v1.x/stream.html#c.uv_connect_cb

UV_STREAM_READ

input (_uv_stream_t&&, ssize) UV_STREAM_READ;
  • Occurrence:
    • Whenever data is available on a stream.
  • Payload:
    • _uv_stream_t&&: pointer to the stream
    • ssize: number of bytes available
      • >0: data available
      • <0: error

libuv reference: http://docs.libuv.org/en/v1.x/stream.html#c.uv_read_cb

UV_STREAM_WRITE

input (_uv_write_t&&, int) UV_STREAM_WRITE;
  • Occurrence:
    • Whenever writing to a stream completes.
  • Payload:
    • _uv_write_T&&: pointer to the write request
    • int: completion status
      • 0: success
      • <0: error

libuv reference: http://docs.libuv.org/en/v1.x/stream.html#c.uv_write_cb

UV_STREAM_ERROR

input (_uv_stream_t&&, int) UV_STREAM_ERROR;
  • Occurrence:
    • Whenever a read or write error occurs in a stream.
  • Payload:
    • _uv_stream_t&&: pointer to the stream
    • int: error code

UV_STREAM_ERROR always occurs before the corresponding UV_STREAM_READ or UV_STREAM_WRITE.

libuv reference: http://docs.libuv.org/en/v1.x/errors.html

Data Abstractions

UV_Stream

A stream abstraction.

data UV_Stream with
    var&[] byte         buffer;
    var&   _uv_stream_t handle;
end
  • Fields:
    • buffer: alias to the read & write buffer
    • handle: underlying operating system handle

Code Abstractions

UV_Stream_Listen

Starts listening for incoming connections in a stream.

code/await UV_Stream_Listen (var& UV_Stream stream, var int? backlog)
                                -> (event void ok)
                                    -> int
  • Parameters
    • stream: stream to listen
    • backlog: number of connections the kernel might queue (default: 128)
  • Public fields
    • ok: event signalled on every new incoming connection
  • Return
    • int: operation status
      • 0: success
      • <0: error

Céu-libuv references: ceu_uv_listen, UV_STREAM_LISTEN.

Example

Opens a TCP stream, binds it to port 7000, and then enters in listen mode. Each incoming connection triggers the event ok.

#include "uv/tcp.ceu"

var&? UV_TCP_Open tcp = spawn UV_TCP_Open(_);
watching tcp do
    var _sockaddr_in addr = _;
    _uv_ip4_addr("0.0.0.0", 7000, &&addr);
    _uv_tcp_bind(&&tcp.stream.handle as _uv_tcp_t&&, &&addr as _sockaddr&&, 0);

    var&? UV_Stream_Listen listen = spawn UV_Stream_Listen(&tcp.stream,_);
    watching listen do
        every listen.ok do
            <...>   // handle incoming connections
        end
    end
end

escape 0;

UV_Stream_Read_N

Reads a specified number of bytes from the stream to its buffer.

code/await UV_Stream_Read_N (var& UV_Stream stream, var usize? n) -> ssize
  • Parameters
    • stream: stream to read
    • n: number of bytes to read (default: whatever arrives in the stream)
  • Return
    • ssize: number of bytes read from stream
      • >=0: number of bytes (not related to n)
      • <0: read error

After returning, if no errors occur, the stream buffer will contain at least n bytes. If the buffer already contains n bytes in the beginning, no read occurs and 0 is returned.

Céu-libuv references: ceu_uv_read_start, UV_STREAM_READ.

libuv references: uv_read_stop.

Example

Connects to 127.0.0.1:7000 and reads and writes in a loop:

#include "uv/tcp.ceu"

var&? UV_TCP_Connect c = spawn UV_TCP_Connect("127.0.0.1", 7000, _);
watching c do
    await c.ok;

    loop do
        await UV_Stream_Read_N(&c.stream,_);    // reads anything
        _printf("%s\n", &&c.stream.buffer[0]);  // shows it in the screen
        await UV_Stream_Write_N(&c.stream,_);   // writes it back
    end
end

escape 0;

UV_Stream_Read_Line

Reads a line from a stream.

code/await UV_Stream_Read_Line (var& UV_Stream stream, var&[] byte line) -> ssize
  • Parameters
    • stream: stream to read
    • line: alias to destination buffer (excludes the leading \n)
  • Return
    • ssize: number of bytes read from stream
      • >=0: number of bytes (not related to n)
      • <0: read error

Céu-libuv references: UV_Stream_Read_N.

Example

Connects to 127.0.0.1:7000 and reads and writes in a loop:

#include "uv/tcp.ceu"

var&? UV_TCP_Connect c = spawn UV_TCP_Connect("127.0.0.1", 7000, _);
watching c do
    await c.ok;

    loop do
        var[] byte line;
        await UV_Stream_Read_Line(&c.stream,&line);     // reads a line
        _printf("%s\n", &&line[0]);                     // shows it in the screen
        line = line .. "\n" .. c.stream.buffer;
        c.stream.buffer = [] .. line;
        await UV_Stream_Write_N(&c.stream,_);           // writes it back
    end
end

escape 0;

UV_Stream_Write_N

Writes a specified number of bytes to the stream from its buffer.

code/await UV_Stream_Write_N (var& UV_Stream stream, var usize? n) -> ssize
  • Parameters
    • stream: stream to write
    • n: number of bytes to write (default: current size of the stream buffer)
  • Return
    • ssize: number of bytes written
      • >=0: number of bytes
      • <0: write error

The written bytes are removed from the stream buffer.

Céu-libuv references: ceu_uv_write, UV_STREAM_WRITE.

Example

Connects to 127.0.0.1:7000 and reads and writes in a loop:

#include "uv/tcp.ceu"

var&? UV_TCP_Connect c = spawn UV_TCP_Connect("127.0.0.1", 7000, _);
watching c do
    await c.ok;

    loop do
        await UV_Stream_Read_N(&c.stream,_);    // reads anything
        _printf("%s\n", &&c.stream.buffer[0]);  // shows it in the screen
        await UV_Stream_Write_N(&c.stream,_);   // writes it back
    end
end

escape 0;