Blame docs/src/stream.rst

Packit b5b901
Packit b5b901
.. _stream:
Packit b5b901
Packit b5b901
:c:type:`uv_stream_t` --- Stream handle
Packit b5b901
=======================================
Packit b5b901
Packit b5b901
Stream handles provide an abstraction of a duplex communication channel.
Packit b5b901
:c:type:`uv_stream_t` is an abstract type, libuv provides 3 stream implementations
Packit b5b901
in the form of :c:type:`uv_tcp_t`, :c:type:`uv_pipe_t` and :c:type:`uv_tty_t`.
Packit b5b901
Packit b5b901
Packit b5b901
Data types
Packit b5b901
----------
Packit b5b901
Packit b5b901
.. c:type:: uv_stream_t
Packit b5b901
Packit b5b901
    Stream handle type.
Packit b5b901
Packit b5b901
.. c:type:: uv_connect_t
Packit b5b901
Packit b5b901
    Connect request type.
Packit b5b901
Packit b5b901
.. c:type:: uv_shutdown_t
Packit b5b901
Packit b5b901
    Shutdown request type.
Packit b5b901
Packit b5b901
.. c:type:: uv_write_t
Packit b5b901
Packit b5b901
    Write request type. Careful attention must be paid when reusing objects of
Packit b5b901
    this type. When a stream is in non-blocking mode, write requests sent
Packit b5b901
    with ``uv_write`` will be queued. Reusing objects at this point is undefined
Packit b5b901
    behaviour. It is safe to reuse the ``uv_write_t`` object only after the
Packit b5b901
    callback passed to ``uv_write`` is fired.
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_read_cb)(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)
Packit b5b901
Packit b5b901
    Callback called when data was read on a stream.
Packit b5b901
Packit b5b901
    `nread` is > 0 if there is data available or < 0 on error. When we've
Packit b5b901
    reached EOF, `nread` will be set to ``UV_EOF``. When `nread` < 0,
Packit b5b901
    the `buf` parameter might not point to a valid buffer; in that case
Packit b5b901
    `buf.len` and `buf.base` are both set to 0.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        `nread` might be 0, which does *not* indicate an error or EOF. This
Packit b5b901
        is equivalent to ``EAGAIN`` or ``EWOULDBLOCK`` under ``read(2)``.
Packit b5b901
Packit b5b901
    The callee is responsible for stopping/closing the stream when an error happens
Packit b5b901
    by calling :c:func:`uv_read_stop` or :c:func:`uv_close`. Trying to read
Packit b5b901
    from the stream again is undefined.
Packit b5b901
Packit b5b901
    The callee is responsible for freeing the buffer, libuv does not reuse it.
Packit b5b901
    The buffer may be a null buffer (where buf->base=NULL and buf->len=0) on
Packit b5b901
    error.
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_write_cb)(uv_write_t* req, int status)
Packit b5b901
Packit b5b901
    Callback called after data was written on a stream. `status` will be 0 in
Packit b5b901
    case of success, < 0 otherwise.
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_connect_cb)(uv_connect_t* req, int status)
Packit b5b901
Packit b5b901
    Callback called after a connection started by :c:func:`uv_connect` is done.
Packit b5b901
    `status` will be 0 in case of success, < 0 otherwise.
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_shutdown_cb)(uv_shutdown_t* req, int status)
Packit b5b901
Packit b5b901
    Callback called after a shutdown request has been completed. `status` will
Packit b5b901
    be 0 in case of success, < 0 otherwise.
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_connection_cb)(uv_stream_t* server, int status)
Packit b5b901
Packit b5b901
    Callback called when a stream server has received an incoming connection.
Packit b5b901
    The user can accept the connection by calling :c:func:`uv_accept`.
Packit b5b901
    `status` will be 0 in case of success, < 0 otherwise.
Packit b5b901
Packit b5b901
Packit b5b901
Public members
Packit b5b901
^^^^^^^^^^^^^^
Packit b5b901
Packit b5b901
.. c:member:: size_t uv_stream_t.write_queue_size
Packit b5b901
Packit b5b901
    Contains the amount of queued bytes waiting to be sent. Readonly.
Packit b5b901
Packit b5b901
.. c:member:: uv_stream_t* uv_connect_t.handle
Packit b5b901
Packit b5b901
    Pointer to the stream where this connection request is running.
Packit b5b901
Packit b5b901
.. c:member:: uv_stream_t* uv_shutdown_t.handle
Packit b5b901
Packit b5b901
    Pointer to the stream where this shutdown request is running.
Packit b5b901
Packit b5b901
.. c:member:: uv_stream_t* uv_write_t.handle
Packit b5b901
Packit b5b901
    Pointer to the stream where this write request is running.
Packit b5b901
Packit b5b901
.. c:member:: uv_stream_t* uv_write_t.send_handle
Packit b5b901
Packit b5b901
    Pointer to the stream being sent using this write request.
Packit b5b901
Packit b5b901
.. seealso:: The :c:type:`uv_handle_t` members also apply.
Packit b5b901
Packit b5b901
Packit b5b901
API
Packit b5b901
---
Packit b5b901
Packit b5b901
.. c:function:: int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb)
Packit b5b901
Packit b5b901
    Shutdown the outgoing (write) side of a duplex stream. It waits for pending
Packit b5b901
    write requests to complete. The `handle` should refer to a initialized stream.
Packit b5b901
    `req` should be an uninitialized shutdown request struct. The `cb` is called
Packit b5b901
    after shutdown is complete.
Packit b5b901
Packit b5b901
.. c:function:: int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb)
Packit b5b901
Packit b5b901
    Start listening for incoming connections. `backlog` indicates the number of
Packit b5b901
    connections the kernel might queue, same as :man:`listen(2)`. When a new
Packit b5b901
    incoming connection is received the :c:type:`uv_connection_cb` callback is
Packit b5b901
    called.
Packit b5b901
Packit b5b901
.. c:function:: int uv_accept(uv_stream_t* server, uv_stream_t* client)
Packit b5b901
Packit b5b901
    This call is used in conjunction with :c:func:`uv_listen` to accept incoming
Packit b5b901
    connections. Call this function after receiving a :c:type:`uv_connection_cb`
Packit b5b901
    to accept the connection. Before calling this function the client handle must
Packit b5b901
    be initialized. < 0 return value indicates an error.
Packit b5b901
Packit b5b901
    When the :c:type:`uv_connection_cb` callback is called it is guaranteed that
Packit b5b901
    this function will complete successfully the first time. If you attempt to use
Packit b5b901
    it more than once, it may fail. It is suggested to only call this function once
Packit b5b901
    per :c:type:`uv_connection_cb` call.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        `server` and `client` must be handles running on the same loop.
Packit b5b901
Packit b5b901
.. c:function:: int uv_read_start(uv_stream_t* stream, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Packit b5b901
Packit b5b901
    Read data from an incoming stream. The :c:type:`uv_read_cb` callback will
Packit b5b901
    be made several times until there is no more data to read or
Packit b5b901
    :c:func:`uv_read_stop` is called.
Packit b5b901
Packit b5b901
.. c:function:: int uv_read_stop(uv_stream_t*)
Packit b5b901
Packit b5b901
    Stop reading data from the stream. The :c:type:`uv_read_cb` callback will
Packit b5b901
    no longer be called.
Packit b5b901
Packit b5b901
    This function is idempotent and may be safely called on a stopped stream.
Packit b5b901
Packit b5b901
.. c:function:: int uv_write(uv_write_t* req, uv_stream_t* handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
Packit b5b901
Packit b5b901
    Write data to stream. Buffers are written in order. Example:
Packit b5b901
Packit b5b901
    ::
Packit b5b901
Packit b5b901
        void cb(uv_write_t* req, int status) {
Packit b5b901
            /* Logic which handles the write result */
Packit b5b901
        }
Packit b5b901
Packit b5b901
        uv_buf_t a[] = {
Packit b5b901
            { .base = "1", .len = 1 },
Packit b5b901
            { .base = "2", .len = 1 }
Packit b5b901
        };
Packit b5b901
Packit b5b901
        uv_buf_t b[] = {
Packit b5b901
            { .base = "3", .len = 1 },
Packit b5b901
            { .base = "4", .len = 1 }
Packit b5b901
        };
Packit b5b901
Packit b5b901
        uv_write_t req1;
Packit b5b901
        uv_write_t req2;
Packit b5b901
Packit b5b901
        /* writes "1234" */
Packit b5b901
        uv_write(&req1, stream, a, 2, cb);
Packit b5b901
        uv_write(&req2, stream, b, 2, cb);
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        The memory pointed to by the buffers must remain valid until the callback gets called.
Packit b5b901
        This also holds for :c:func:`uv_write2`.
Packit b5b901
Packit b5b901
.. c:function:: int uv_write2(uv_write_t* req, uv_stream_t* handle, const uv_buf_t bufs[], unsigned int nbufs, uv_stream_t* send_handle, uv_write_cb cb)
Packit b5b901
Packit b5b901
    Extended write function for sending handles over a pipe. The pipe must be
Packit b5b901
    initialized with `ipc` == 1.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        `send_handle` must be a TCP socket or pipe, which is a server or a connection (listening
Packit b5b901
        or connected state). Bound sockets or pipes will be assumed to be servers.
Packit b5b901
Packit b5b901
.. c:function:: int uv_try_write(uv_stream_t* handle, const uv_buf_t bufs[], unsigned int nbufs)
Packit b5b901
Packit b5b901
    Same as :c:func:`uv_write`, but won't queue a write request if it can't be
Packit b5b901
    completed immediately.
Packit b5b901
Packit b5b901
    Will return either:
Packit b5b901
Packit b5b901
    * > 0: number of bytes written (can be less than the supplied buffer size).
Packit b5b901
    * < 0: negative error code (``UV_EAGAIN`` is returned if no data can be sent
Packit b5b901
      immediately).
Packit b5b901
Packit b5b901
.. c:function:: int uv_is_readable(const uv_stream_t* handle)
Packit b5b901
Packit b5b901
    Returns 1 if the stream is readable, 0 otherwise.
Packit b5b901
Packit b5b901
.. c:function:: int uv_is_writable(const uv_stream_t* handle)
Packit b5b901
Packit b5b901
    Returns 1 if the stream is writable, 0 otherwise.
Packit b5b901
Packit b5b901
.. c:function:: int uv_stream_set_blocking(uv_stream_t* handle, int blocking)
Packit b5b901
Packit b5b901
    Enable or disable blocking mode for a stream.
Packit b5b901
Packit b5b901
    When blocking mode is enabled all writes complete synchronously. The
Packit b5b901
    interface remains unchanged otherwise, e.g. completion or failure of the
Packit b5b901
    operation will still be reported through a callback which is made
Packit b5b901
    asynchronously.
Packit b5b901
Packit b5b901
    .. warning::
Packit b5b901
        Relying too much on this API is not recommended. It is likely to change
Packit b5b901
        significantly in the future.
Packit b5b901
Packit b5b901
        Currently only works on Windows for :c:type:`uv_pipe_t` handles.
Packit b5b901
        On UNIX platforms, all :c:type:`uv_stream_t` handles are supported.
Packit b5b901
Packit b5b901
        Also libuv currently makes no ordering guarantee when the blocking mode
Packit b5b901
        is changed after write requests have already been submitted. Therefore it is
Packit b5b901
        recommended to set the blocking mode immediately after opening or creating
Packit b5b901
        the stream.
Packit b5b901
Packit b5b901
    .. versionchanged:: 1.4.0 UNIX implementation added.
Packit b5b901
Packit b5b901
.. c:function:: size_t uv_stream_get_write_queue_size(const uv_stream_t* stream)
Packit b5b901
Packit b5b901
    Returns `stream->write_queue_size`.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.19.0
Packit b5b901
Packit b5b901
.. seealso:: The :c:type:`uv_handle_t` API functions also apply.