Blame docs/src/tcp.rst

Packit Service 7c31a4
Packit Service 7c31a4
.. _tcp:
Packit Service 7c31a4
Packit Service 7c31a4
:c:type:`uv_tcp_t` --- TCP handle
Packit Service 7c31a4
=================================
Packit Service 7c31a4
Packit Service 7c31a4
TCP handles are used to represent both TCP streams and servers.
Packit Service 7c31a4
Packit Service 7c31a4
:c:type:`uv_tcp_t` is a 'subclass' of :c:type:`uv_stream_t`.
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
Data types
Packit Service 7c31a4
----------
Packit Service 7c31a4
Packit Service 7c31a4
.. c:type:: uv_tcp_t
Packit Service 7c31a4
Packit Service 7c31a4
    TCP handle type.
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
Public members
Packit Service 7c31a4
^^^^^^^^^^^^^^
Packit Service 7c31a4
Packit Service 7c31a4
N/A
Packit Service 7c31a4
Packit Service 7c31a4
.. seealso:: The :c:type:`uv_stream_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_tcp_init(uv_loop_t* loop, uv_tcp_t* handle)
Packit Service 7c31a4
Packit Service 7c31a4
    Initialize the handle. No socket is created as of yet.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_tcp_init_ex(uv_loop_t* loop, uv_tcp_t* handle, unsigned int flags)
Packit Service 7c31a4
Packit Service 7c31a4
    Initialize the handle with the specified flags. At the moment only the lower 8 bits
Packit Service 7c31a4
    of the `flags` parameter are used as the socket domain. A socket will be created
Packit Service 7c31a4
    for the given domain. If the specified domain is ``AF_UNSPEC`` no socket is created,
Packit Service 7c31a4
    just like :c:func:`uv_tcp_init`.
Packit Service 7c31a4
Packit Service 7c31a4
    .. versionadded:: 1.7.0
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock)
Packit Service 7c31a4
Packit Service 7c31a4
    Open an existing file descriptor or SOCKET as a TCP handle.
Packit Service 7c31a4
Packit Service 7c31a4
    .. versionchanged:: 1.2.1 the file descriptor is set to non-blocking mode.
Packit Service 7c31a4
Packit Service 7c31a4
    .. note::
Packit Service 7c31a4
        The passed file descriptor or SOCKET is not checked for its type, but
Packit Service 7c31a4
        it's required that it represents a valid stream socket.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_tcp_nodelay(uv_tcp_t* handle, int enable)
Packit Service 7c31a4
Packit Service 7c31a4
    Enable `TCP_NODELAY`, which disables Nagle's algorithm.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay)
Packit Service 7c31a4
Packit Service 7c31a4
    Enable / disable TCP keep-alive. `delay` is the initial delay in seconds,
Packit Service 7c31a4
    ignored when `enable` is zero.
Packit Service 7c31a4
Packit Service 7c31a4
    After `delay` has been reached, 10 successive probes, each spaced 1 second
Packit Service 7c31a4
    from the previous one, will still happen. If the connection is still lost
Packit Service 7c31a4
    at the end of this procedure, then the handle is destroyed with a
Packit Service 7c31a4
    ``UV_ETIMEDOUT`` error passed to the corresponding callback.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable)
Packit Service 7c31a4
Packit Service 7c31a4
    Enable / disable simultaneous asynchronous accept requests that are
Packit Service 7c31a4
    queued by the operating system when listening for new TCP connections.
Packit Service 7c31a4
Packit Service 7c31a4
    This setting is used to tune a TCP server for the desired performance.
Packit Service 7c31a4
    Having simultaneous accepts can significantly improve the rate of accepting
Packit Service 7c31a4
    connections (which is why it is enabled by default) but may lead to uneven
Packit Service 7c31a4
    load distribution in multi-process setups.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_tcp_bind(uv_tcp_t* handle, const struct sockaddr* addr, unsigned int flags)
Packit Service 7c31a4
Packit Service 7c31a4
    Bind the handle to an address and port. `addr` should point to an
Packit Service 7c31a4
    initialized ``struct sockaddr_in`` or ``struct sockaddr_in6``.
Packit Service 7c31a4
Packit Service 7c31a4
    When the port is already taken, you can expect to see an ``UV_EADDRINUSE``
Packit Service 7c31a4
    error from either :c:func:`uv_tcp_bind`, :c:func:`uv_listen` or
Packit Service 7c31a4
    :c:func:`uv_tcp_connect`. That is, a successful call to this function does
Packit Service 7c31a4
    not guarantee that the call to :c:func:`uv_listen` or :c:func:`uv_tcp_connect`
Packit Service 7c31a4
    will succeed as well.
Packit Service 7c31a4
Packit Service 7c31a4
    `flags` can contain ``UV_TCP_IPV6ONLY``, in which case dual-stack support
Packit Service 7c31a4
    is disabled and only IPv6 is used.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_tcp_getsockname(const uv_tcp_t* handle, struct sockaddr* name, int* namelen)
Packit Service 7c31a4
Packit Service 7c31a4
    Get the current address to which the handle is bound. `name` must point to
Packit Service 7c31a4
    a valid and big enough chunk of memory, ``struct sockaddr_storage`` is
Packit Service 7c31a4
    recommended for IPv4 and IPv6 support.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_tcp_getpeername(const uv_tcp_t* handle, struct sockaddr* name, int* namelen)
Packit Service 7c31a4
Packit Service 7c31a4
    Get the address of the peer connected to the handle. `name` must point to
Packit Service 7c31a4
    a valid and big enough chunk of memory, ``struct sockaddr_storage`` is
Packit Service 7c31a4
    recommended for IPv4 and IPv6 support.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle, const struct sockaddr* addr, uv_connect_cb cb)
Packit Service 7c31a4
Packit Service 7c31a4
    Establish an IPv4 or IPv6 TCP connection. Provide an initialized TCP handle
Packit Service 7c31a4
    and an uninitialized :c:type:`uv_connect_t`. `addr` should point to an
Packit Service 7c31a4
    initialized ``struct sockaddr_in`` or ``struct sockaddr_in6``.
Packit Service 7c31a4
Packit Service 7c31a4
    On Windows if the `addr` is initialized to point to an unspecified address
Packit Service 7c31a4
    (``0.0.0.0`` or ``::``) it will be changed to point to ``localhost``.
Packit Service 7c31a4
    This is done to match the behavior of Linux systems.
Packit Service 7c31a4
Packit Service 7c31a4
    The callback is made when the connection has been established or when a
Packit Service 7c31a4
    connection error happened.
Packit Service 7c31a4
Packit Service 7c31a4
    .. versionchanged:: 1.19.0 added ``0.0.0.0`` and ``::`` to ``localhost``
Packit Service 7c31a4
        mapping
Packit Service 7c31a4
Packit Service 7c31a4
.. seealso:: The :c:type:`uv_stream_t` API functions also apply.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_tcp_close_reset(uv_tcp_t* handle, uv_close_cb close_cb)
Packit Service 7c31a4
Packit Service 7c31a4
    Resets a TCP connection by sending a RST packet. This is accomplished by
Packit Service 7c31a4
    setting the `SO_LINGER` socket option with a linger interval of zero and
Packit Service 7c31a4
    then calling :c:func:`uv_close`.
Packit Service 7c31a4
    Due to some platform inconsistencies, mixing of :c:func:`uv_shutdown` and
Packit Service 7c31a4
    :c:func:`uv_tcp_close_reset` calls is not allowed.
Packit Service 7c31a4
Packit Service 7c31a4
    .. versionadded:: 1.32.0