Blame docs/src/stream.rst

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