ext/libuv/include/uv.h in libuv-1.0.0 vs ext/libuv/include/uv.h in libuv-1.0.2

- old
+ new

@@ -81,11 +81,10 @@ XX(EAI_NONAME, "unknown node or service") \ XX(EAI_OVERFLOW, "argument buffer overflow") \ XX(EAI_PROTOCOL, "resolved protocol is unknown") \ XX(EAI_SERVICE, "service not available for socket type") \ XX(EAI_SOCKTYPE, "socket type not supported") \ - XX(EAI_SYSTEM, "system error") \ XX(EALREADY, "connection already in progress") \ XX(EBADF, "bad file descriptor") \ XX(EBUSY, "resource busy or locked") \ XX(ECANCELED, "operation canceled") \ XX(ECHARSET, "invalid Unicode character") \ @@ -165,10 +164,11 @@ XX(SHUTDOWN, shutdown) \ XX(UDP_SEND, udp_send) \ XX(FS, fs) \ XX(WORK, work) \ XX(GETADDRINFO, getaddrinfo) \ + XX(GETNAMEINFO, getnameinfo) \ typedef enum { #define XX(code, _) UV_ ## code = UV__ ## code, UV_ERRNO_MAP(XX) #undef XX @@ -214,10 +214,11 @@ typedef struct uv_signal_s uv_signal_t; /* Request types. */ typedef struct uv_req_s uv_req_t; typedef struct uv_getaddrinfo_s uv_getaddrinfo_t; +typedef struct uv_getnameinfo_s uv_getnameinfo_t; typedef struct uv_shutdown_s uv_shutdown_t; typedef struct uv_write_s uv_write_t; typedef struct uv_connect_s uv_connect_t; typedef struct uv_udp_send_s uv_udp_send_t; typedef struct uv_fs_s uv_fs_t; @@ -255,11 +256,12 @@ * All callbacks in libuv are made asynchronously. That is they are never * made by the function that takes them as a parameter. */ /* - * Returns the default loop. + * Returns the initialized default loop. It may return NULL in case of + * allocation failture. */ UV_EXTERN uv_loop_t* uv_default_loop(void); /* * Initializes a uv_loop_t structure. @@ -273,47 +275,58 @@ */ UV_EXTERN int uv_loop_close(uv_loop_t* loop); /* * Allocates and initializes a new loop. - * NOTE: This function is DEPRECATED (to be removed after 0.12), users should - * allocate the loop manually and use uv_loop_init instead. + * + * NOTE: + * This function is DEPRECATED (to be removed after 0.12), users should + * allocate the loop manually and use uv_loop_init instead. */ UV_EXTERN uv_loop_t* uv_loop_new(void); /* * Cleans up a loop once it has finished executio and frees its memory. - * NOTE: This function is DEPRECATED (to be removed after 0.12). Users should use - * uv_loop_close and free the memory manually instead. + * + * NOTE: + * This function is DEPRECATED (to be removed after 0.12). Users should use + * uv_loop_close and free the memory manually instead. */ UV_EXTERN void uv_loop_delete(uv_loop_t*); /* + * Returns size of the loop struct, useful for dynamic lookup with FFI. + */ +UV_EXTERN size_t uv_loop_size(void); + +/* * This function runs the event loop. It will act differently depending on the * specified mode: * - UV_RUN_DEFAULT: Runs the event loop until the reference count drops to * zero. Always returns zero. * - UV_RUN_ONCE: Poll for new events once. Note that this function blocks if * there are no pending events. Returns zero when done (no active handles * or requests left), or non-zero if more events are expected (meaning you * should run the event loop again sometime in the future). * - UV_RUN_NOWAIT: Poll for new events once but don't block if there are no - * pending events. + * pending events. Returns zero when done (no active handles + * or requests left), or non-zero if more events are expected (meaning you + * should run the event loop again sometime in the future). */ UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode); /* * This function checks whether the reference count, the number of active * handles or requests left in the event loop, is non-zero. */ UV_EXTERN int uv_loop_alive(const uv_loop_t* loop); /* - * This function will stop the event loop by forcing uv_run to end - * as soon as possible, but not sooner than the next loop iteration. - * If this function was called before blocking for i/o, the loop won't - * block for i/o on this iteration. + * This function will stop the event loop by forcing uv_run to end as soon as + * possible, but not sooner than the next loop iteration. + * If this function was called before blocking for i/o, the loop won't block + * for i/o on this iteration. */ UV_EXTERN void uv_stop(uv_loop_t*); /* * Manually modify the event loop's reference count. Useful if the user wants @@ -388,16 +401,16 @@ /* * `nread` is > 0 if there is data available, 0 if libuv is done reading for * now, or < 0 on error. * - * The callee is responsible for closing the stream when an error happens. - * Trying to read from the stream again is undefined. + * The callee is responsible for closing the stream when an error happens + * by calling uv_close(). Trying to read from the stream again is undefined. * * The callee is responsible for freeing the buffer, libuv does not reuse it. * The buffer may be a null buffer (where buf->base=NULL and buf->len=0) on - * EOF or error. + * error. */ typedef void (*uv_read_cb)(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf); typedef void (*uv_write_cb)(uv_write_t* req, int status); @@ -417,10 +430,14 @@ typedef void (*uv_work_cb)(uv_work_t* req); typedef void (*uv_after_work_cb)(uv_work_t* req, int status); typedef void (*uv_getaddrinfo_cb)(uv_getaddrinfo_t* req, int status, struct addrinfo* res); +typedef void (*uv_getnameinfo_cb)(uv_getnameinfo_t* req, + int status, + const char* hostname, + const char* service); typedef struct { long tv_sec; long tv_nsec; } uv_timespec_t; @@ -491,21 +508,21 @@ struct uv_req_s { UV_REQ_FIELDS }; -/* Platform-specific request types */ +/* Platform-specific request types. */ UV_PRIVATE_REQ_TYPES /* - * uv_shutdown_t is a subclass of uv_req_t + * uv_shutdown_t is a subclass of uv_req_t. * - * Shutdown the outgoing (write) side of a duplex stream. It waits for - * pending write requests to complete. The handle should refer to a - * initialized stream. req should be an uninitialized shutdown request - * struct. The cb is called after shutdown is complete. + * Shutdown the outgoing (write) side of a duplex stream. It waits for pending + * write requests to complete. The handle should refer to a initialized stream. + * req should be an uninitialized shutdown request struct. The cb is called + * after shutdown is complete. */ UV_EXTERN int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb); @@ -526,24 +543,23 @@ /* private */ \ uv_close_cb close_cb; \ void* handle_queue[2]; \ UV_HANDLE_PRIVATE_FIELDS \ -/* The abstract base class of all handles. */ +/* The abstract base class of all handles. */ struct uv_handle_s { UV_HANDLE_FIELDS }; /* - * Returns size of various handle types, useful for FFI - * bindings to allocate correct memory without copying struct - * definitions + * Returns size of various handle types, useful for FFI bindings to allocate + * correct memory without copying struct definitions. */ UV_EXTERN size_t uv_handle_size(uv_handle_type type); /* - * Returns size of request types, useful for dynamic lookup with FFI + * Returns size of request types, useful for dynamic lookup with FFI. */ UV_EXTERN size_t uv_req_size(uv_req_type type); /* * Returns non-zero if the handle is active, zero if it's inactive. @@ -552,11 +568,11 @@ * * - A uv_async_t handle is always active and cannot be deactivated, except * by closing it with uv_close(). * * - A uv_pipe_t, uv_tcp_t, uv_udp_t, etc. handle - basically any handle that - * deals with I/O - is active when it is doing something that involves I/O, + * deals with i/o - is active when it is doing something that involves i/o, * like reading, writing, connecting, accepting new connections, etc. * * - A uv_check_t, uv_idle_t, uv_timer_t, etc. handle is active when it has * been started with a call to uv_check_start(), uv_idle_start(), etc. * @@ -587,10 +603,11 @@ UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb); /* * Constructor for uv_buf_t. + * * Due to platform differences the user cannot rely on the ordering of the * base and len members of the uv_buf_t struct. The user is responsible for * freeing base after the uv_buf_t is done. Return struct passed by value. */ UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len); @@ -603,11 +620,11 @@ uv_read_cb read_cb; \ /* private */ \ UV_STREAM_PRIVATE_FIELDS /* - * uv_stream_t is a subclass of uv_handle_t + * uv_stream_t is a subclass of uv_handle_t. * * uv_stream is an abstract class. * * uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t and uv_tty_t. */ @@ -622,20 +639,20 @@ * This call is used in conjunction with uv_listen() to accept incoming * connections. Call uv_accept after receiving a uv_connection_cb to accept * the connection. Before calling uv_accept use uv_*_init() must be * called on the client. Non-zero return value indicates an error. * - * When the uv_connection_cb is called it is guaranteed that uv_accept will + * When the uv_connection_cb is called it is guaranteed that uv_accept() will * complete successfully the first time. If you attempt to use it more than - * once, it may fail. It is suggested to only call uv_accept once per + * once, it may fail. It is suggested to only call uv_accept() once per * uv_connection_cb call. */ UV_EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client); /* * Read data from an incoming stream. The callback will be made several - * times until there is no more data to read or uv_read_stop is called. + * times until there is no more data to read or uv_read_stop() is called. * When we've reached EOF nread will be set to UV_EOF. * * When nread < 0, the buf parameter might not point to a valid buffer; * in that case buf.len and buf.base are both set to 0. * @@ -690,21 +707,23 @@ unsigned int nbufs, uv_stream_t* send_handle, uv_write_cb cb); /* - * Same as `uv_write()`, but won't queue write request if it can't be completed + * Same as uv_write(), but won't queue write request if it can't be completed * immediately. + * * Will return either: - * - >= 0: number of bytes written (can be less than the supplied buffer size) - * - < 0: negative error code + * - > 0: number of bytes written (can be less than the supplied buffer size). + * - < 0: negative error code (UV_EAGAIN is returned if no data can be sent + * immediately). */ UV_EXTERN int uv_try_write(uv_stream_t* handle, const uv_buf_t bufs[], unsigned int nbufs); -/* uv_write_t is a subclass of uv_req_t */ +/* uv_write_t is a subclass of uv_req_t. */ struct uv_write_s { UV_REQ_FIELDS uv_write_cb cb; uv_stream_t* send_handle; uv_stream_t* handle; @@ -741,19 +760,18 @@ /* * Used to determine whether a stream is closing or closed. * - * N.B. is only valid between the initialization of the handle - * and the arrival of the close callback, and cannot be used - * to validate the handle. + * N.B. is only valid between the initialization of the handle and the arrival + * of the close callback, and cannot be used to validate the handle. */ UV_EXTERN int uv_is_closing(const uv_handle_t* handle); /* - * uv_tcp_t is a subclass of uv_stream_t + * uv_tcp_t is a subclass of uv_stream_t. * * Represents a TCP stream or TCP server. */ struct uv_tcp_s { UV_HANDLE_FIELDS @@ -781,31 +799,32 @@ unsigned int delay); /* * Enable/disable simultaneous asynchronous accept requests that are * queued by the operating system when listening for new tcp connections. + * * This setting is used to tune a tcp server for the desired performance. - * Having simultaneous accepts can significantly improve the rate of - * accepting connections (which is why it is enabled by default) but - * may lead to uneven load distribution in multi-process setups. + * Having simultaneous accepts can significantly improve the rate of accepting + * connections (which is why it is enabled by default) but may lead to uneven + * load distribution in multi-process setups. */ UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable); enum uv_tcp_flags { - /* Used with uv_tcp_bind, when an IPv6 address is used */ + /* Used with uv_tcp_bind, when an IPv6 address is used. */ UV_TCP_IPV6ONLY = 1 }; /* * Bind the handle to an address and port. `addr` should point to an * initialized struct sockaddr_in or struct sockaddr_in6. * - * When the port is already taken, you can expect to see an UV_EADDRINUSE - * error from either uv_tcp_bind(), uv_listen() or uv_tcp_connect(). + * When the port is already taken, you can expect to see an UV_EADDRINUSE error + * from either uv_tcp_bind(), uv_listen() or uv_tcp_connect(). * - * That is, a successful call to uv_tcp_bind() does not guarantee that - * the call to uv_listen() or uv_tcp_connect() will succeed as well. + * That is, a successful call to uv_tcp_bind() does not guarantee that the call + * to uv_listen() or uv_tcp_connect() will succeed as well. */ UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, const struct sockaddr* addr, unsigned int flags); UV_EXTERN int uv_tcp_getsockname(const uv_tcp_t* handle, @@ -815,22 +834,22 @@ struct sockaddr* name, int* namelen); /* * Establish an IPv4 or IPv6 TCP connection. Provide an initialized TCP handle - * and an uninitialized uv_connect_t*. `addr` should point to an initialized + * and an uninitialized uv_connect_t*. `addr` should point to an initialized * struct sockaddr_in or struct sockaddr_in6. * * The callback is made when the connection has been established or when a * connection error happened. */ UV_EXTERN int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle, const struct sockaddr* addr, uv_connect_cb cb); -/* uv_connect_t is a subclass of uv_req_t */ +/* uv_connect_t is a subclass of uv_req_t. */ struct uv_connect_s { UV_REQ_FIELDS uv_connect_cb cb; uv_stream_t* handle; UV_CONNECT_PRIVATE_FIELDS @@ -847,53 +866,69 @@ /* * Indicates message was truncated because read buffer was too small. The * remainder was discarded by the OS. Used in uv_udp_recv_cb. */ UV_UDP_PARTIAL = 2, - /* Indicates if SO_REUSEADDR will be set when binding the handle. + /* + * Indicates if SO_REUSEADDR will be set when binding the handle. * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other - * UNIX platforms, it sets the SO_REUSEADDR flag. What that means is that + * Unix platforms, it sets the SO_REUSEADDR flag. What that means is that * multiple threads or processes can bind to the same address without error * (provided they all set the flag) but only the last one to bind will receive * any traffic, in effect "stealing" the port from the previous listener. */ UV_UDP_REUSEADDR = 4 }; /* - * Called after uv_udp_send(). status 0 indicates - * success otherwise error. + * Called after uv_udp_send(). status 0 indicates success otherwise error. */ typedef void (*uv_udp_send_cb)(uv_udp_send_t* req, int status); /* * Callback that is invoked when a new UDP datagram is received. * * handle UDP handle. * nread Number of bytes that have been received. - * 0 if there is no more data to read. You may - * discard or repurpose the read buffer. - * < 0 if a transmission error was detected. + * - 0 if there is no more data to read. You may discard or repurpose + * the read buffer. Note that 0 may also mean that an empty datagram + * was received (in this case `addr` is not NULL). + * - < 0 if a transmission error was detected. * buf uv_buf_t with the received data. - * addr struct sockaddr* containing the address of the sender. - * Can be NULL. Valid for the duration of the callback only. - * flags One or more OR'ed UV_UDP_* constants. - * Right now only UV_UDP_PARTIAL is used. + * addr struct sockaddr* containing the address of the sender. Can be NULL. + * Valid for the duration of the callback only. + * flags One or more OR'ed UV_UDP_* constants. Right now only UV_UDP_PARTIAL + * is used. + * + * NOTE: + * The receive callback will be called with nread == 0 and addr == NULL when + * there is nothing to read, and with nread == 0 and addr != NULL when an empty + * UDP packet is received. */ typedef void (*uv_udp_recv_cb)(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags); -/* uv_udp_t is a subclass of uv_handle_t */ +/* uv_udp_t is a subclass of uv_handle_t. */ struct uv_udp_s { UV_HANDLE_FIELDS + /* read-only */ + /* + * Number of bytes queued for sending. This field strictly shows how much + * information is currently queued. + */ + size_t send_queue_size; + /* + * Number of send requests currently in the queue awaiting to be processed. + */ + size_t send_queue_count; UV_UDP_PRIVATE_FIELDS }; -/* uv_udp_send_t is a subclass of uv_req_t */ +/* uv_udp_send_t is a subclass of uv_req_t. */ struct uv_udp_send_s { UV_REQ_FIELDS uv_udp_t* handle; uv_udp_send_cb cb; UV_UDP_SEND_PRIVATE_FIELDS @@ -907,30 +942,30 @@ /* * Opens an existing file descriptor or SOCKET as a udp handle. * * Unix only: - * The only requirement of the sock argument is that it follows the - * datagram contract (works in unconnected mode, supports sendmsg()/recvmsg(), - * etc.). In other words, other datagram-type sockets like raw sockets or - * netlink sockets can also be passed to this function. + * The only requirement of the sock argument is that it follows the datagram + * contract (works in unconnected mode, supports sendmsg()/recvmsg(), etc). + * In other words, other datagram-type sockets like raw sockets or netlink + * sockets can also be passed to this function. * - * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other - * UNIX platforms, it sets the SO_REUSEADDR flag. What that means is that - * multiple threads or processes can bind to the same address without error - * (provided they all set the flag) but only the last one to bind will receive - * any traffic, in effect "stealing" the port from the previous listener. + * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other Unix + * platforms, it sets the SO_REUSEADDR flag. What that means is that multiple + * threads or processes can bind to the same address without error (provided + * they all set the flag) but only the last one to bind will receive any + * traffic, in effect "stealing" the port from the previous listener. * This behavior is something of an anomaly and may be replaced by an explicit * opt-in mechanism in future versions of libuv. */ UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock); /* * Bind to an IP address and port. * * Arguments: - * handle UDP handle. Should have been initialized with `uv_udp_init`. + * handle UDP handle. Should have been initialized with uv_udp_init(). * addr struct sockaddr_in or struct sockaddr_in6 with the address and * port to bind to. * flags Indicate how the socket will be bound, UV_UDP_IPV6ONLY and * UV_UDP_REUSEADDR are supported. * @@ -939,23 +974,23 @@ */ UV_EXTERN int uv_udp_bind(uv_udp_t* handle, const struct sockaddr* addr, unsigned int flags); -UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, +UV_EXTERN int uv_udp_getsockname(const uv_udp_t* handle, struct sockaddr* name, int* namelen); /* * Set membership for a multicast address * * Arguments: * handle UDP handle. Should have been initialized with - * `uv_udp_init`. - * multicast_addr multicast address to set membership for - * interface_addr interface address - * membership Should be UV_JOIN_GROUP or UV_LEAVE_GROUP + * uv_udp_init(). + * multicast_addr multicast address to set membership for. + * interface_addr interface address. + * membership Should be UV_JOIN_GROUP or UV_LEAVE_GROUP. * * Returns: * 0 on success, or an error code < 0 on failure. */ UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle, @@ -967,80 +1002,79 @@ * Set IP multicast loop flag. Makes multicast packets loop back to * local sockets. * * Arguments: * handle UDP handle. Should have been initialized with - * `uv_udp_init`. - * on 1 for on, 0 for off + * uv_udp_init(). + * on 1 for on, 0 for off. * * Returns: * 0 on success, or an error code < 0 on failure. */ UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on); /* - * Set the multicast ttl + * Set the multicast ttl. * * Arguments: * handle UDP handle. Should have been initialized with - * `uv_udp_init`. - * ttl 1 through 255 + * uv_udp_init(). + * ttl 1 through 255. * * Returns: * 0 on success, or an error code < 0 on failure. */ UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl); /* - * Set the multicast interface to send on + * Set the multicast interface to send on. * * Arguments: * handle UDP handle. Should have been initialized with - * `uv_udp_init`. - * interface_addr interface address + * uv_udp_init(). + * interface_addr interface address. * * Returns: * 0 on success, or an error code < 0 on failure. */ UV_EXTERN int uv_udp_set_multicast_interface(uv_udp_t* handle, const char* interface_addr); /* - * Set broadcast on or off + * Set broadcast on or off. * * Arguments: * handle UDP handle. Should have been initialized with - * `uv_udp_init`. - * on 1 for on, 0 for off + * uv_udp_init(). + * on 1 for on, 0 for off. * * Returns: * 0 on success, or an error code < 0 on failure. */ UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on); /* - * Set the time to live + * Set the time to live. * * Arguments: * handle UDP handle. Should have been initialized with - * `uv_udp_init`. - * ttl 1 through 255 + * uv_udp_init(). + * ttl 1 through 255. * * Returns: * 0 on success, or an error code < 0 on failure. */ UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl); /* - * Send data. If the socket has not previously been bound with `uv_udp_bind,` - * it is bound to 0.0.0.0 (the "all interfaces" address) and a random - * port number. + * Send data. If the socket has not previously been bound with uv_udp_bind() it + * is bound to 0.0.0.0 (the "all interfaces" address) and a random port number. * * Arguments: * req UDP request handle. Need not be initialized. - * handle UDP handle. Should have been initialized with `uv_udp_init`. + * handle UDP handle. Should have been initialized with uv_udp_init(). * bufs List of buffers to send. * nbufs Number of buffers in `bufs`. * addr struct sockaddr_in or struct sockaddr_in6 with the address and * port of the remote peer. * send_cb Callback to invoke when the data has been sent out. @@ -1054,16 +1088,29 @@ unsigned int nbufs, const struct sockaddr* addr, uv_udp_send_cb send_cb); /* - * Receive data. If the socket has not previously been bound with `uv_udp_bind` - * it is bound to 0.0.0.0 (the "all interfaces" address) and a random - * port number. + * Same as uv_udp_send(), but won't queue a send request if it can't be completed + * immediately. * + * Will return either: + * - >= 0: number of bytes sent (it matches the given buffer size). + * - < 0: negative error code (UV_EAGAIN is returned when the message can't be + * sent immediately). + */ +UV_EXTERN int uv_udp_try_send(uv_udp_t* handle, + const uv_buf_t bufs[], + unsigned int nbufs, + const struct sockaddr* addr); +/* + * Receive data. If the socket has not previously been bound with uv_udp_bind() + * it is bound to 0.0.0.0 (the "all interfaces" address) and a random port + * number. + * * Arguments: - * handle UDP handle. Should have been initialized with `uv_udp_init`. + * handle UDP handle. Should have been initialized with uv_udp_init(). * alloc_cb Callback to invoke when temporary storage is needed. * recv_cb Callback to invoke with received data. * * Returns: * 0 on success, or an error code < 0 on failure. @@ -1074,20 +1121,20 @@ /* * Stop listening for incoming datagrams. * * Arguments: - * handle UDP handle. Should have been initialized with `uv_udp_init`. + * handle UDP handle. Should have been initialized with uv_udp_init(). * * Returns: * 0 on success, or an error code < 0 on failure. */ UV_EXTERN int uv_udp_recv_stop(uv_udp_t* handle); /* - * uv_tty_t is a subclass of uv_stream_t + * uv_tty_t is a subclass of uv_stream_t. * * Representing a stream for the console. */ struct uv_tty_s { UV_HANDLE_FIELDS @@ -1095,16 +1142,16 @@ UV_TTY_PRIVATE_FIELDS }; /* * Initialize a new TTY stream with the given file descriptor. Usually the - * file descriptor will be + * file descriptor will be: * 0 = stdin * 1 = stdout * 2 = stderr * The last argument, readable, specifies if you plan on calling - * uv_read_start with this stream. stdin is readable, stdout is not. + * uv_read_start() with this stream. stdin is readable, stdout is not. * * TTY streams which are not readable have blocking writes. */ UV_EXTERN int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable); @@ -1115,11 +1162,11 @@ /* * To be called when the program exits. Resets TTY settings to default * values for the next process to take over. * - * This function is async signal-safe on UNIX platforms but can fail with error + * This function is async signal-safe on Unix platforms but can fail with error * code UV_EBUSY if you call it when execution is inside uv_tty_set_mode(). */ UV_EXTERN int uv_tty_reset_mode(void); /* @@ -1129,19 +1176,20 @@ /* * Used to detect what type of stream should be used with a given file * descriptor. Usually this will be used during initialization to guess the * type of the stdio streams. + * * For isatty() functionality use this function and test for UV_TTY. */ UV_EXTERN uv_handle_type uv_guess_handle(uv_file file); /* - * uv_pipe_t is a subclass of uv_stream_t + * uv_pipe_t is a subclass of uv_stream_t. * * Representing a pipe stream or pipe server. On Windows this is a Named - * Pipe. On Unix this is a UNIX domain socket. + * Pipe. On Unix this is a Unix domain socket. */ struct uv_pipe_s { UV_HANDLE_FIELDS UV_STREAM_FIELDS int ipc; /* non-zero if this pipe is used for passing handles */ @@ -1158,53 +1206,54 @@ * Opens an existing file descriptor or HANDLE as a pipe. */ UV_EXTERN int uv_pipe_open(uv_pipe_t*, uv_file file); /* - * Bind the pipe to a file path (UNIX) or a name (Windows.) + * Bind the pipe to a file path (Unix) or a name (Windows). * - * Paths on UNIX get truncated to `sizeof(sockaddr_un.sun_path)` bytes, + * Paths on Unix get truncated to `sizeof(sockaddr_un.sun_path)` bytes, * typically between 92 and 108 bytes. */ UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name); /* - * Connect to the UNIX domain socket or the named pipe. + * Connect to the Unix domain socket or the named pipe. * - * Paths on UNIX get truncated to `sizeof(sockaddr_un.sun_path)` bytes, + * Paths on Unix get truncated to `sizeof(sockaddr_un.sun_path)` bytes, * typically between 92 and 108 bytes. */ UV_EXTERN void uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle, const char* name, uv_connect_cb cb); /* - * Get the name of the UNIX domain socket or the named pipe. + * Get the name of the Unix domain socket or the named pipe. * - * A preallocated buffer must be provided. The len parameter holds the - * length of the buffer and it's set to the number of bytes written to the - * buffer on output. If the buffer is not big enough UV_ENOBUFS will be - * returned and len will contain the required size. + * A preallocated buffer must be provided. The len parameter holds the length + * of the buffer and it's set to the number of bytes written to the buffer on + * output. If the buffer is not big enough UV_ENOBUFS will be returned and len + * will contain the required size. */ UV_EXTERN int uv_pipe_getsockname(const uv_pipe_t* handle, char* buf, size_t* len); /* * This setting applies to Windows only. - * Set the number of pending pipe instance handles when the pipe server - * is waiting for connections. + * + * Set the number of pending pipe instance handles when the pipe server is + * waiting for connections. */ UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t* handle, int count); /* * Used to receive handles over ipc pipes. * - * First - call `uv_pipe_pending_count`, if it is > 0 - initialize handle - * using type, returned by `uv_pipe_pending_type` and call - * `uv_accept(pipe, handle)`. + * First - call uv_pipe_pending_count(), if it is > 0 - initialize handle + * using type, returned by uv_pipe_pending_type() and call + * uv_accept(pipe, handle). */ UV_EXTERN int uv_pipe_pending_count(uv_pipe_t* handle); UV_EXTERN uv_handle_type uv_pipe_pending_type(uv_pipe_t* handle); /* @@ -1213,14 +1262,14 @@ * The uv_poll watcher is used to watch file descriptors for readability and * writability, similar to the purpose of poll(2). * * The purpose of uv_poll is to enable integrating external libraries that * rely on the event loop to signal it about the socket status changes, like - * c-ares or libssh2. Using uv_poll_t for any other other purpose is not - * recommended; uv_tcp_t, uv_udp_t, etc. provide an implementation that is - * much faster and more scalable than what can be achieved with uv_poll_t, - * especially on Windows. + * c-ares or libssh2. Using uv_poll_t for any other purpose is not recommended; + * uv_tcp_t, uv_udp_t, etc. provide an implementation that is much faster and + * more scalable than what can be achieved with uv_poll_t, especially on + * Windows. * * It is possible that uv_poll occasionally signals that a file descriptor is * readable or writable even when it isn't. The user should therefore always * be prepared to handle EAGAIN or equivalent when it attempts to read from or * write to the fd. @@ -1231,11 +1280,11 @@ * The user should not close a file descriptor while it is being polled by an * active uv_poll watcher. This can cause the poll watcher to report an error, * but it might also start polling another socket. However the fd can be safely * closed immediately after a call to uv_poll_stop() or uv_close(). * - * On windows only sockets can be polled with uv_poll. On unix any file + * On windows only sockets can be polled with uv_poll. On Unix any file * descriptor that would be accepted by poll(2) can be used with uv_poll. */ struct uv_poll_s { UV_HANDLE_FIELDS uv_poll_cb poll_cb; @@ -1248,12 +1297,14 @@ }; /* Initialize the poll watcher using a file descriptor. */ UV_EXTERN int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd); -/* Initialize the poll watcher using a socket descriptor. On unix this is */ -/* identical to uv_poll_init. On windows it takes a SOCKET handle. */ +/* + * Initialize the poll watcher using a socket descriptor. On Unix this is + * identical to uv_poll_init. On windows it takes a SOCKET handle. + */ UV_EXTERN int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle, uv_os_sock_t socket); /* @@ -1333,15 +1384,19 @@ /* * uv_async_t is a subclass of uv_handle_t. * - * uv_async_send wakes up the event loop and calls the async handle's callback. - * There is no guarantee that every uv_async_send call leads to exactly one - * invocation of the callback; the only guarantee is that the callback function - * is called at least once after the call to async_send. Unlike all other - * libuv functions, uv_async_send can be called from another thread. + * uv_async_send() wakes up the event loop and calls the async handle's callback. + * + * Unlike all other libuv functions, uv_async_send() can be called from another + * thread. + * + * NOTE: + * There is no guarantee that every uv_async_send() call leads to exactly one + * invocation of the callback; the only guarantee is that the callback + * function is called at least once after the call to async_send. */ struct uv_async_s { UV_HANDLE_FIELDS UV_ASYNC_PRIVATE_FIELDS }; @@ -1406,11 +1461,11 @@ UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle); /* - * uv_getaddrinfo_t is a subclass of uv_req_t + * uv_getaddrinfo_t is a subclass of uv_req_t. * * Request object for uv_getaddrinfo. */ struct uv_getaddrinfo_s { UV_REQ_FIELDS @@ -1449,18 +1504,46 @@ * Free the struct addrinfo. Passing NULL is allowed and is a no-op. */ UV_EXTERN void uv_freeaddrinfo(struct addrinfo* ai); -/* uv_spawn() options */ +/* +* uv_getnameinfo_t is a subclass of uv_req_t. +* +* Request object for uv_getnameinfo. +*/ +struct uv_getnameinfo_s { + UV_REQ_FIELDS + /* read-only */ + uv_loop_t* loop; + UV_GETNAMEINFO_PRIVATE_FIELDS +}; + +/* + * Asynchronous getnameinfo. + * + * Returns 0 on success or an error code < 0 on failure. + * + * If successful, your callback gets called sometime in the future with the + * lookup result. + */ +UV_EXTERN int uv_getnameinfo(uv_loop_t* loop, + uv_getnameinfo_t* req, + uv_getnameinfo_cb getnameinfo_cb, + const struct sockaddr* addr, + int flags); + + +/* uv_spawn() options. */ typedef enum { UV_IGNORE = 0x00, UV_CREATE_PIPE = 0x01, UV_INHERIT_FD = 0x02, UV_INHERIT_STREAM = 0x04, - /* When UV_CREATE_PIPE is specified, UV_READABLE_PIPE and UV_WRITABLE_PIPE + /* + * When UV_CREATE_PIPE is specified, UV_READABLE_PIPE and UV_WRITABLE_PIPE * determine the direction of flow, from the child process' perspective. Both * flags may be specified to create a duplex data stream. */ UV_READABLE_PIPE = 0x10, UV_WRITABLE_PIPE = 0x20 @@ -1537,11 +1620,11 @@ */ UV_PROCESS_SETGID = (1 << 1), /* * Do not wrap any arguments in quotes, or perform any other escaping, when * converting the argument list into a command line string. This option is - * only meaningful on Windows systems. On unix it is silently ignored. + * only meaningful on Windows systems. On Unix it is silently ignored. */ UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS = (1 << 2), /* * Spawn the child process in a detached state - this will make it a process * group leader, and will effectively enable the child to keep running after @@ -1550,18 +1633,18 @@ * the child's process handle. */ UV_PROCESS_DETACHED = (1 << 3), /* * Hide the subprocess console window that would normally be created. This - * option is only meaningful on Windows systems. On unix it is silently + * option is only meaningful on Windows systems. On Unix it is silently * ignored. */ UV_PROCESS_WINDOWS_HIDE = (1 << 4) }; /* - * uv_process_t is a subclass of uv_handle_t + * uv_process_t is a subclass of uv_handle_t. */ struct uv_process_s { UV_HANDLE_FIELDS uv_exit_cb exit_cb; int pid; @@ -1584,11 +1667,11 @@ const uv_process_options_t* options); /* * Kills the process with the specified signal. The user must still - * call uv_close on the process. + * call uv_close() on the process. * * Emulates some aspects of Unix exit status on Windows, in that while the * underlying process will be terminated with a status of `1`, * `uv_process_t.exit_signal` will be set to signum, so the process will appear * to have been killed by `signum`. @@ -1610,11 +1693,11 @@ */ UV_EXTERN int uv_kill(int pid, int signum); /* - * uv_work_t is a subclass of uv_req_t + * uv_work_t is a subclass of uv_req_t. */ struct uv_work_s { UV_REQ_FIELDS uv_loop_t* loop; uv_work_cb work_cb; @@ -1645,11 +1728,11 @@ * - A uv_fs_t request has its req->result field set to UV_ECANCELED. * * - A uv_work_t or uv_getaddrinfo_t request has its callback invoked with * status == UV_ECANCELED. * - * This function is currently only implemented on UNIX platforms. On Windows, + * This function is currently only implemented on Unix platforms. On Windows, * it always returns UV_ENOSYS. */ UV_EXTERN int uv_cancel(uv_req_t* req); @@ -1714,19 +1797,19 @@ * Please note that not all uv_rusage_t struct fields will be filled on Windows. */ UV_EXTERN int uv_getrusage(uv_rusage_t* rusage); /* - * This allocates cpu_infos array, and sets count. The array - * is freed using uv_free_cpu_info(). + * This allocates cpu_infos array, and sets count. The array is freed + * using uv_free_cpu_info(). */ UV_EXTERN int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count); UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count); /* - * This allocates addresses array, and sets count. The array - * is freed using uv_free_interface_addresses(). + * This allocates addresses array, and sets count. The array is freed + * using uv_free_interface_addresses(). */ UV_EXTERN int uv_interface_addresses(uv_interface_address_t** addresses, int* count); UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses, int count); @@ -1764,29 +1847,30 @@ UV_FS_FSYNC, UV_FS_FDATASYNC, UV_FS_UNLINK, UV_FS_RMDIR, UV_FS_MKDIR, + UV_FS_MKDTEMP, UV_FS_RENAME, UV_FS_READDIR, UV_FS_LINK, UV_FS_SYMLINK, UV_FS_READLINK, UV_FS_CHOWN, UV_FS_FCHOWN } uv_fs_type; -/* uv_fs_t is a subclass of uv_req_t */ +/* uv_fs_t is a subclass of uv_req_t. */ struct uv_fs_s { UV_REQ_FIELDS uv_fs_type fs_type; uv_loop_t* loop; uv_fs_cb cb; ssize_t result; void* ptr; const char* path; - uv_stat_t statbuf; /* Stores the result of uv_fs_stat and uv_fs_fstat. */ + uv_stat_t statbuf; /* Stores the result of uv_fs_stat() and uv_fs_fstat(). */ UV_FS_PRIVATE_FIELDS }; UV_EXTERN void uv_fs_req_cleanup(uv_fs_t* req); @@ -1806,10 +1890,13 @@ const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb); UV_EXTERN int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode, uv_fs_cb cb); +UV_EXTERN int uv_fs_mkdtemp(uv_loop_t* loop, uv_fs_t* req, const char* template, + uv_fs_cb cb); + UV_EXTERN int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb); UV_EXTERN int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, uv_fs_cb cb); @@ -1849,18 +1936,18 @@ UV_EXTERN int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path, const char* new_path, uv_fs_cb cb); /* - * This flag can be used with uv_fs_symlink on Windows - * to specify whether path argument points to a directory. + * This flag can be used with uv_fs_symlink() on Windows to specify whether + * path argument points to a directory. */ #define UV_FS_SYMLINK_DIR 0x0001 /* - * This flag can be used with uv_fs_symlink on Windows - * to specify whether the symlink is to be created using junction points. + * This flag can be used with uv_fs_symlink() on Windows to specify whether + * the symlink is to be created using junction points. */ #define UV_FS_SYMLINK_JUNCTION 0x0002 UV_EXTERN int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path, const char* new_path, int flags, uv_fs_cb cb); @@ -1934,11 +2021,11 @@ */ UV_EXTERN int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buf, size_t* len); /* - * UNIX signal handling on a per-event loop basis. The implementation is not + * Unix signal handling on a per-event loop basis. The implementation is not * ultra efficient so don't go creating a million event loops with a million * signal watchers. * * Note to Linux users: SIGRT0 and SIGRT1 (signals 32 and 33) are used by the * NPTL pthreads library to manage threads. Installing watchers for those @@ -1989,18 +2076,20 @@ UV_EXTERN int uv_signal_stop(uv_signal_t* handle); /* * Gets load average. + * * See: http://en.wikipedia.org/wiki/Load_(computing) + * * Returns [0,0,0] on Windows. */ UV_EXTERN void uv_loadavg(double avg[3]); /* - * Flags to be passed to uv_fs_event_start. + * Flags to be passed to uv_fs_event_start(). */ enum uv_fs_event_flags { /* * By default, if the fs event watcher is given a directory name, we will * watch for all events in that directory. This flags overrides this behavior @@ -2047,36 +2136,38 @@ UV_EXTERN int uv_fs_event_getpath(uv_fs_event_t* handle, char* buf, size_t* len); -/* Utility */ +/* Utilities. */ -/* Convert string ip addresses to binary structures */ +/* Convert string ip addresses to binary structures. */ UV_EXTERN int uv_ip4_addr(const char* ip, int port, struct sockaddr_in* addr); UV_EXTERN int uv_ip6_addr(const char* ip, int port, struct sockaddr_in6* addr); -/* Convert binary addresses to strings */ +/* Convert binary addresses to strings. */ UV_EXTERN int uv_ip4_name(const struct sockaddr_in* src, char* dst, size_t size); UV_EXTERN int uv_ip6_name(const struct sockaddr_in6* src, char* dst, size_t size); -/* Cross-platform IPv6-capable implementation of the 'standard' inet_ntop */ -/* and inet_pton functions. On success they return 0. If an error */ -/* the target of the `dst` pointer is unmodified. */ +/* + * Cross-platform IPv6-capable implementation of the 'standard' inet_ntop() and + * inet_pton() functions. On success they return 0. If an error the target of + * the `dst` pointer is unmodified. + */ UV_EXTERN int uv_inet_ntop(int af, const void* src, char* dst, size_t size); UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst); -/* Gets the executable path */ +/* Gets the executable path. */ UV_EXTERN int uv_exepath(char* buffer, size_t* size); -/* Gets the current working directory */ +/* Gets the current working directory. */ UV_EXTERN int uv_cwd(char* buffer, size_t* size); -/* Changes the current working directory */ +/* Changes the current working directory. */ UV_EXTERN int uv_chdir(const char* dir); -/* Gets memory info in bytes */ +/* Gets memory info in bytes. */ UV_EXTERN uint64_t uv_get_free_memory(void); UV_EXTERN uint64_t uv_get_total_memory(void); /* * Returns the current high-resolution real time. This is expressed in @@ -2098,17 +2189,17 @@ * It is recommended to call this function as early in your program as possible, * before the inherited file descriptors can be closed or duplicated. * * Note that this function works on a best-effort basis: there is no guarantee * that libuv can discover all file descriptors that were inherited. In general - * it does a better job on Windows than it does on unix. + * it does a better job on Windows than it does on Unix. */ UV_EXTERN void uv_disable_stdio_inheritance(void); /* * Opens a shared library. The filename is in utf-8. Returns 0 on success and - * -1 on error. Call `uv_dlerror(uv_lib_t*)` to get the error message. + * -1 on error. Call uv_dlerror(uv_lib_t*) to get the error message. */ UV_EXTERN int uv_dlopen(const char* filename, uv_lib_t* lib); /* * Close the shared library. @@ -2125,12 +2216,12 @@ * Returns the last uv_dlopen() or uv_dlsym() error message. */ UV_EXTERN const char* uv_dlerror(const uv_lib_t* lib); /* - * The mutex functions return 0 on success or an error code < 0 - * (unless the return type is void, of course). + * The mutex functions return 0 on success or an error code < 0 (unless the + * return type is void, of course). */ UV_EXTERN int uv_mutex_init(uv_mutex_t* handle); UV_EXTERN void uv_mutex_destroy(uv_mutex_t* handle); UV_EXTERN void uv_mutex_lock(uv_mutex_t* handle); UV_EXTERN int uv_mutex_trylock(uv_mutex_t* handle); @@ -2162,39 +2253,52 @@ */ UV_EXTERN int uv_cond_init(uv_cond_t* cond); UV_EXTERN void uv_cond_destroy(uv_cond_t* cond); UV_EXTERN void uv_cond_signal(uv_cond_t* cond); UV_EXTERN void uv_cond_broadcast(uv_cond_t* cond); -/* Waits on a condition variable without a timeout. + +/* + * Same goes for the barrier functions. Note that uv_barrier_wait() returns + * a value > 0 to an arbitrarily chosen "serializer" thread to facilitate + * cleanup, i.e.: * - * Note: - * 1. callers should be prepared to deal with spurious wakeups. + * if (uv_barrier_wait(&barrier) > 0) + * uv_barrier_destroy(&barrier); */ +UV_EXTERN int uv_barrier_init(uv_barrier_t* barrier, unsigned int count); +UV_EXTERN void uv_barrier_destroy(uv_barrier_t* barrier); +UV_EXTERN int uv_barrier_wait(uv_barrier_t* barrier); + +/* + * Waits on a condition variable without a timeout. + * + * NOTE: + * 1. callers should be prepared to deal with spurious wakeups. + */ UV_EXTERN void uv_cond_wait(uv_cond_t* cond, uv_mutex_t* mutex); -/* Waits on a condition variable with a timeout in nano seconds. +/* + * Waits on a condition variable with a timeout in nano seconds. * Returns 0 for success or UV_ETIMEDOUT on timeout, It aborts when other * errors happen. * - * Note: - * 1. callers should be prepared to deal with spurious wakeups. - * 2. the granularity of timeout on Windows is never less than one millisecond. - * 3. uv_cond_timedwait takes a relative timeout, not an absolute time. + * NOTE: + * 1. callers should be prepared to deal with spurious wakeups. + * 2. the granularity of timeout on Windows is never less than one millisecond. + * 3. uv_cond_timedwait() takes a relative timeout, not an absolute time. */ UV_EXTERN int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex, uint64_t timeout); -UV_EXTERN int uv_barrier_init(uv_barrier_t* barrier, unsigned int count); -UV_EXTERN void uv_barrier_destroy(uv_barrier_t* barrier); -UV_EXTERN void uv_barrier_wait(uv_barrier_t* barrier); - -/* Runs a function once and only once. Concurrent calls to uv_once() with the +/* + * Runs a function once and only once. Concurrent calls to uv_once() with the * same guard will block all callers except one (it's unspecified which one). * The guard should be initialized statically with the UV_ONCE_INIT macro. */ UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void)); -/* Thread-local storage. These functions largely follow the semantics of +/* + * Thread-local storage. These functions largely follow the semantics of * pthread_key_create(), pthread_key_delete(), pthread_getspecific() and * pthread_setspecific(). * * Note that the total thread-local storage size may be limited. * That is, it may not be possible to create many TLS keys. @@ -2228,15 +2332,15 @@ struct uv_loop_s { /* User data - use this for whatever. */ void* data; - /* Loop reference counting */ + /* Loop reference counting. */ unsigned int active_handles; void* handle_queue[2]; void* active_reqs[2]; - /* Internal flag to signal loop stop */ + /* Internal flag to signal loop stop. */ unsigned int stop_flag; UV_LOOP_PRIVATE_FIELDS }; @@ -2250,9 +2354,10 @@ #undef UV_CHECK_PRIVATE_FIELDS #undef UV_IDLE_PRIVATE_FIELDS #undef UV_ASYNC_PRIVATE_FIELDS #undef UV_TIMER_PRIVATE_FIELDS #undef UV_GETADDRINFO_PRIVATE_FIELDS +#undef UV_GETNAMEINFO_PRIVATE_FIELDS #undef UV_FS_REQ_PRIVATE_FIELDS #undef UV_WORK_PRIVATE_FIELDS #undef UV_FS_EVENT_PRIVATE_FIELDS #undef UV_SIGNAL_PRIVATE_FIELDS #undef UV_LOOP_PRIVATE_FIELDS