Blame docs/src/handle.rst

Packit b5b901
Packit b5b901
.. _handle:
Packit b5b901
Packit b5b901
:c:type:`uv_handle_t` --- Base handle
Packit b5b901
=====================================
Packit b5b901
Packit b5b901
`uv_handle_t` is the base type for all libuv handle types.
Packit b5b901
Packit b5b901
Structures are aligned so that any libuv handle can be cast to `uv_handle_t`.
Packit b5b901
All API functions defined here work with any handle type.
Packit b5b901
Packit b5b901
Libuv handles are not movable. Pointers to handle structures passed to
Packit b5b901
functions must remain valid for the duration of the requested operation. Take
Packit b5b901
care when using stack allocated handles.
Packit b5b901
Packit b5b901
Data types
Packit b5b901
----------
Packit b5b901
Packit b5b901
.. c:type:: uv_handle_t
Packit b5b901
Packit b5b901
    The base libuv handle type.
Packit b5b901
Packit b5b901
.. c:type:: uv_handle_type
Packit b5b901
Packit b5b901
    The kind of the libuv handle.
Packit b5b901
Packit b5b901
    ::
Packit b5b901
Packit b5b901
        typedef enum {
Packit b5b901
          UV_UNKNOWN_HANDLE = 0,
Packit b5b901
          UV_ASYNC,
Packit b5b901
          UV_CHECK,
Packit b5b901
          UV_FS_EVENT,
Packit b5b901
          UV_FS_POLL,
Packit b5b901
          UV_HANDLE,
Packit b5b901
          UV_IDLE,
Packit b5b901
          UV_NAMED_PIPE,
Packit b5b901
          UV_POLL,
Packit b5b901
          UV_PREPARE,
Packit b5b901
          UV_PROCESS,
Packit b5b901
          UV_STREAM,
Packit b5b901
          UV_TCP,
Packit b5b901
          UV_TIMER,
Packit b5b901
          UV_TTY,
Packit b5b901
          UV_UDP,
Packit b5b901
          UV_SIGNAL,
Packit b5b901
          UV_FILE,
Packit b5b901
          UV_HANDLE_TYPE_MAX
Packit b5b901
        } uv_handle_type;
Packit b5b901
Packit b5b901
.. c:type:: uv_any_handle
Packit b5b901
Packit b5b901
    Union of all handle types.
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_alloc_cb)(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
Packit b5b901
Packit b5b901
    Type definition for callback passed to :c:func:`uv_read_start` and
Packit b5b901
    :c:func:`uv_udp_recv_start`. The user must allocate memory and fill the supplied
Packit b5b901
    :c:type:`uv_buf_t` structure. If NULL is assigned as the buffer's base or 0 as its length,
Packit b5b901
    a ``UV_ENOBUFS`` error will be triggered in the :c:type:`uv_udp_recv_cb` or the
Packit b5b901
    :c:type:`uv_read_cb` callback.
Packit b5b901
Packit Service e08953
    Each buffer is used only once and the user is responsible for freeing it in the
Packit Service e08953
    :c:type:`uv_udp_recv_cb` or the :c:type:`uv_read_cb` callback.
Packit Service e08953
Packit b5b901
    A suggested size (65536 at the moment in most cases) is provided, but it's just an indication,
Packit b5b901
    not related in any way to the pending data to be read. The user is free to allocate the amount
Packit b5b901
    of memory they decide.
Packit b5b901
Packit b5b901
    As an example, applications with custom allocation schemes such as using freelists, allocation
Packit b5b901
    pools or slab based allocators may decide to use a different size which matches the memory
Packit b5b901
    chunks they already have.
Packit b5b901
Packit b5b901
    Example:
Packit b5b901
Packit b5b901
    ::
Packit b5b901
Packit b5b901
        static void my_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
Packit b5b901
          buf->base = malloc(suggested_size);
Packit b5b901
          buf->len = suggested_size;
Packit b5b901
        }
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_close_cb)(uv_handle_t* handle)
Packit b5b901
Packit b5b901
    Type definition for callback passed to :c:func:`uv_close`.
Packit b5b901
Packit b5b901
Packit b5b901
Public members
Packit b5b901
^^^^^^^^^^^^^^
Packit b5b901
Packit b5b901
.. c:member:: uv_loop_t* uv_handle_t.loop
Packit b5b901
Packit Service e08953
    Pointer to the :c:type:`uv_loop_t` the handle is running on. Readonly.
Packit b5b901
Packit b5b901
.. c:member:: uv_handle_type uv_handle_t.type
Packit b5b901
Packit b5b901
    The :c:type:`uv_handle_type`, indicating the type of the underlying handle. Readonly.
Packit b5b901
Packit b5b901
.. c:member:: void* uv_handle_t.data
Packit b5b901
Packit b5b901
    Space for user-defined arbitrary data. libuv does not use this field.
Packit b5b901
Packit b5b901
Packit b5b901
API
Packit b5b901
---
Packit b5b901
Packit b5b901
.. c:function:: UV_HANDLE_TYPE_MAP(iter_macro)
Packit b5b901
Packit b5b901
    Macro that expands to a series of invocations of `iter_macro` for
Packit b5b901
    each of the handle types. `iter_macro` is invoked with two
Packit b5b901
    arguments: the name of the `uv_handle_type` element without the
Packit b5b901
    `UV_` prefix, and the name of the corresponding structure type
Packit b5b901
    without the `uv_` prefix and `_t` suffix.
Packit b5b901
Packit b5b901
.. c:function:: int uv_is_active(const uv_handle_t* handle)
Packit b5b901
Packit b5b901
    Returns non-zero if the handle is active, zero if it's inactive. What
Packit b5b901
    "active" means depends on the type of handle:
Packit b5b901
Packit b5b901
    - A uv_async_t handle is always active and cannot be deactivated, except
Packit b5b901
      by closing it with uv_close().
Packit b5b901
Packit b5b901
    - A uv_pipe_t, uv_tcp_t, uv_udp_t, etc. handle - basically any handle that
Packit b5b901
      deals with i/o - is active when it is doing something that involves i/o,
Packit b5b901
      like reading, writing, connecting, accepting new connections, etc.
Packit b5b901
Packit b5b901
    - A uv_check_t, uv_idle_t, uv_timer_t, etc. handle is active when it has
Packit b5b901
      been started with a call to uv_check_start(), uv_idle_start(), etc.
Packit b5b901
Packit b5b901
    Rule of thumb: if a handle of type `uv_foo_t` has a `uv_foo_start()`
Packit b5b901
    function, then it's active from the moment that function is called.
Packit b5b901
    Likewise, `uv_foo_stop()` deactivates the handle again.
Packit b5b901
Packit b5b901
.. c:function:: int uv_is_closing(const uv_handle_t* handle)
Packit b5b901
Packit b5b901
    Returns non-zero if the handle is closing or closed, zero otherwise.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        This function should only be used between the initialization of the handle and the
Packit b5b901
        arrival of the close callback.
Packit b5b901
Packit b5b901
.. c:function:: void uv_close(uv_handle_t* handle, uv_close_cb close_cb)
Packit b5b901
Packit b5b901
    Request handle to be closed. `close_cb` will be called asynchronously after
Packit b5b901
    this call. This MUST be called on each handle before memory is released.
Packit Service e08953
    Moreover, the memory can only be released in `close_cb` or after it has
Packit Service e08953
    returned.
Packit b5b901
Packit b5b901
    Handles that wrap file descriptors are closed immediately but
Packit b5b901
    `close_cb` will still be deferred to the next iteration of the event loop.
Packit b5b901
    It gives you a chance to free up any resources associated with the handle.
Packit b5b901
Packit b5b901
    In-progress requests, like uv_connect_t or uv_write_t, are cancelled and
Packit b5b901
    have their callbacks called asynchronously with status=UV_ECANCELED.
Packit b5b901
Packit b5b901
.. c:function:: void uv_ref(uv_handle_t* handle)
Packit b5b901
Packit b5b901
    Reference the given handle. References are idempotent, that is, if a handle
Packit b5b901
    is already referenced calling this function again will have no effect.
Packit b5b901
Packit b5b901
    See :ref:`refcount`.
Packit b5b901
Packit b5b901
.. c:function:: void uv_unref(uv_handle_t* handle)
Packit b5b901
Packit b5b901
    Un-reference the given handle. References are idempotent, that is, if a handle
Packit b5b901
    is not referenced calling this function again will have no effect.
Packit b5b901
Packit b5b901
    See :ref:`refcount`.
Packit b5b901
Packit b5b901
.. c:function:: int uv_has_ref(const uv_handle_t* handle)
Packit b5b901
Packit b5b901
    Returns non-zero if the handle referenced, zero otherwise.
Packit b5b901
Packit b5b901
    See :ref:`refcount`.
Packit b5b901
Packit b5b901
.. c:function:: size_t uv_handle_size(uv_handle_type type)
Packit b5b901
Packit b5b901
    Returns the size of the given handle type. Useful for FFI binding writers
Packit b5b901
    who don't want to know the structure layout.
Packit b5b901
Packit b5b901
Packit b5b901
Miscellaneous API functions
Packit b5b901
---------------------------
Packit b5b901
Packit b5b901
The following API functions take a :c:type:`uv_handle_t` argument but they work
Packit b5b901
just for some handle types.
Packit b5b901
Packit b5b901
.. c:function:: int uv_send_buffer_size(uv_handle_t* handle, int* value)
Packit b5b901
Packit b5b901
    Gets or sets the size of the send buffer that the operating
Packit b5b901
    system uses for the socket.
Packit b5b901
Packit Service e08953
    If `*value` == 0, then it will set `*value` to the current send buffer size.
Packit Service e08953
    If `*value` > 0 then it will use `*value` to set the new send buffer size.
Packit Service e08953
Packit Service e08953
    On success, zero is returned. On error, a negative result is
Packit Service e08953
    returned.
Packit b5b901
Packit b5b901
    This function works for TCP, pipe and UDP handles on Unix and for TCP and
Packit b5b901
    UDP handles on Windows.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        Linux will set double the size and return double the size of the original set value.
Packit b5b901
Packit b5b901
.. c:function:: int uv_recv_buffer_size(uv_handle_t* handle, int* value)
Packit b5b901
Packit b5b901
    Gets or sets the size of the receive buffer that the operating
Packit b5b901
    system uses for the socket.
Packit b5b901
Packit Service e08953
    If `*value` == 0, then it will set `*value` to the current receive buffer size.
Packit Service e08953
    If `*value` > 0 then it will use `*value` to set the new receive buffer size.
Packit Service e08953
Packit Service e08953
    On success, zero is returned. On error, a negative result is
Packit Service e08953
    returned.
Packit b5b901
Packit b5b901
    This function works for TCP, pipe and UDP handles on Unix and for TCP and
Packit b5b901
    UDP handles on Windows.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        Linux will set double the size and return double the size of the original set value.
Packit b5b901
Packit b5b901
.. c:function:: int uv_fileno(const uv_handle_t* handle, uv_os_fd_t* fd)
Packit b5b901
Packit b5b901
    Gets the platform dependent file descriptor equivalent.
Packit b5b901
Packit b5b901
    The following handles are supported: TCP, pipes, TTY, UDP and poll. Passing
Packit b5b901
    any other handle type will fail with `UV_EINVAL`.
Packit b5b901
Packit b5b901
    If a handle doesn't have an attached file descriptor yet or the handle
Packit b5b901
    itself has been closed, this function will return `UV_EBADF`.
Packit b5b901
Packit b5b901
    .. warning::
Packit b5b901
        Be very careful when using this function. libuv assumes it's in control of the file
Packit b5b901
        descriptor so any change to it may lead to malfunction.
Packit b5b901
Packit b5b901
.. c:function:: uv_loop_t* uv_handle_get_loop(const uv_handle_t* handle)
Packit b5b901
Packit b5b901
    Returns `handle->loop`.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.19.0
Packit b5b901
Packit b5b901
.. c:function:: void* uv_handle_get_data(const uv_handle_t* handle)
Packit b5b901
Packit b5b901
    Returns `handle->data`.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.19.0
Packit b5b901
Packit b5b901
.. c:function:: void* uv_handle_set_data(uv_handle_t* handle, void* data)
Packit b5b901
Packit b5b901
    Sets `handle->data` to `data`.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.19.0
Packit b5b901
Packit b5b901
.. c:function:: uv_handle_type uv_handle_get_type(const uv_handle_t* handle)
Packit b5b901
Packit b5b901
    Returns `handle->type`.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.19.0
Packit b5b901
Packit b5b901
.. c:function:: const char* uv_handle_type_name(uv_handle_type type)
Packit b5b901
Packit b5b901
    Returns the name for the equivalent struct for a given handle type,
Packit b5b901
    e.g. `"pipe"` (as in :c:type:`uv_pipe_t`) for `UV_NAMED_PIPE`.
Packit b5b901
Packit b5b901
    If no such handle type exists, this returns `NULL`.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.19.0
Packit b5b901
Packit b5b901
.. _refcount:
Packit b5b901
Packit b5b901
Reference counting
Packit b5b901
------------------
Packit b5b901
Packit b5b901
The libuv event loop (if run in the default mode) will run until there are no
Packit b5b901
active `and` referenced handles left. The user can force the loop to exit early
Packit b5b901
by unreferencing handles which are active, for example by calling :c:func:`uv_unref`
Packit b5b901
after calling :c:func:`uv_timer_start`.
Packit b5b901
Packit b5b901
A handle can be referenced or unreferenced, the refcounting scheme doesn't use
Packit b5b901
a counter, so both operations are idempotent.
Packit b5b901
Packit b5b901
All handles are referenced when active by default, see :c:func:`uv_is_active`
Packit b5b901
for a more detailed explanation on what being `active` involves.