libuv/desired-api.md

160 lines
5.0 KiB
Markdown
Raw Normal View History

2011-05-28 08:47:47 +00:00
Warning: this is not actual API but desired API.
# `uv_handle_t`
2011-05-28 22:40:10 +00:00
This is the abstract base class of all types of handles. All handles have in
common:
2011-05-28 08:47:47 +00:00
2011-05-28 22:40:10 +00:00
* When handles are initialized, the reference count to the event loop is
increased by one.
2011-05-28 08:47:47 +00:00
* The user owns the `uv_handle_t` memory and is in charge of freeing it.
2011-05-28 22:40:10 +00:00
* In order to free resources associated with a handle, one must `uv_close()`
and wait for the `uv_close_cb` callback. After the close callback has been
made, the user is allowed to the `uv_handle_t` object.
* The `uv_close_cb` is always made directly off the event loop. That is, it
is not called from `uv_close()`.
2011-05-28 08:47:47 +00:00
# `uv_tcp_server_t`
2011-05-28 22:40:10 +00:00
A TCP server class that is a subclass of `uv_handle_t`. This can be bound to
an address and begin accepting new TCP sockets.
2011-05-28 08:47:47 +00:00
2011-05-29 01:52:33 +00:00
int uv_bind4(uv_tcp_server_t* tcp_server, struct sockaddr_in* address);
int uv_bind6(uv_tcp_server_t* tcp_server, struct sockaddr_in6* address);
2011-05-28 08:47:47 +00:00
2011-05-28 22:40:10 +00:00
Binds the TCP server to an address. The `address` can be created with
`uv_ip4_addr()`. Call this before `uv_listen()`
2011-05-28 08:47:47 +00:00
Returns zero on success, -1 on failure. Errors in order of least-seriousness:
2011-05-28 22:40:10 +00:00
* `UV_EADDRINUSE` There is already another socket bound to the specified
address.
2011-05-28 08:47:47 +00:00
* `UV_EADDRNOTAVAIL` The `address` parameter is an IP address that is not
* `UV_EINVAL` The server is already bound to an address.
* `UV_EFAULT` Memory of `address` parameter is unintelligible.
2011-06-07 12:12:08 +00:00
int uv_listen(uv_tcp_server_t*, int backlog, uv_connection_cb cb);
2011-05-29 01:52:33 +00:00
Begins listening for connections. The accept callback is level-triggered.
int uv_accept(uv_tcp_server_t* server,
uv_tcp_t* client);
2011-05-29 01:52:33 +00:00
Accepts a connection. This should be called after the accept callback is
made. The `client` parameter should be uninitialized memory; `uv_accept` is
used instead of `uv_tcp_init` for server-side `uv_tcp_t` initialization.
Return value 0 indicates success, -1 failure. Possible errors:
2011-06-07 12:12:08 +00:00
* `UV_EAGAIN` There are no connections. Wait for the `uv_connection_cb` callback
2011-05-29 01:52:33 +00:00
to be called again.
* `UV_EFAULT` The memory of either `server` is unintelligible.
2011-05-28 08:47:47 +00:00
# `uv_stream_t`
2011-05-28 22:40:10 +00:00
An abstract subclass of `uv_handle_t`. Streams represent something that
reads and/or writes data. Streams can be half or full-duplex. TCP sockets
are streams, files are streams with offsets.
2011-05-28 08:47:47 +00:00
2011-05-29 01:52:33 +00:00
int uv_read_start(uv_stream_t* stream,
uv_alloc_cb alloc_cb,
uv_read_cb read_cb);
2011-05-28 08:47:47 +00:00
2011-05-28 22:40:10 +00:00
Starts the stream reading continuously. The `alloc_cb` is used to allow the
user to implement various means of supplying the stream with buffers to
fill. The `read_cb` returns buffers to the user filled with data.
2011-05-28 08:47:47 +00:00
2011-05-28 22:40:10 +00:00
Sometimes the buffers returned to the user do not contain data. This does
not indicate EOF as in other systems. EOF is made via the `uv_eof_cb` which
2011-05-28 23:06:29 +00:00
can be set like this `uv_set_eof_cb(stream, eof_cb);`
2011-05-29 01:52:33 +00:00
int uv_read_stop(uv_stream_t* stream);
2011-05-28 23:06:29 +00:00
Stops reading from the stream.
2011-05-29 01:52:33 +00:00
int uv_write_req_init(uv_write_req_t*,
uv_stream_t*,
uv_buf_t bufs[],
int butcnf);
2011-05-28 23:06:29 +00:00
Initiates a write request on a stream.
2011-05-29 01:52:33 +00:00
int uv_shutdown_req_init(uv_shutdown_req_t*, uv_stream_t*)
2011-05-28 08:47:47 +00:00
2011-05-28 23:06:29 +00:00
Initiates a shutdown of outgoing data once the write queue drains.
2011-05-28 08:47:47 +00:00
# `uv_tcp_t`
2011-05-29 01:52:33 +00:00
The TCP handle class represents one endpoint of a duplex TCP stream.
`uv_tcp_t` is a subclass of `uv_stream_t`. A TCP handle can represent a
client side connection (one that has been used with uv_connect_req_init`)
or a server-side connection (one that was initialized with `uv_accept`)
int uv_connect_req_init(uv_connect_req_t* req,
uv_tcp_t* socket,
struct sockaddr* addr);
2011-05-29 01:52:33 +00:00
Initiates a request to open a connection.
2011-05-28 22:40:10 +00:00
# `uv_req_t`
Abstract class represents an asynchronous request. This is a subclass of `uv_handle_t`.
# `uv_connect_req_t`
Subclass of `uv_req_t`. Represents a request for a TCP connection. Operates
on `uv_tcp_t` handles. Like other types of requests the `close_cb` indicates
completion of the request.
2011-05-29 01:52:33 +00:00
int uv_connect_req_init(uv_connect_req_t* req,
uv_tcp_t* socket,
struct sockaddr* addr);
2011-05-28 22:40:10 +00:00
Initializes the connection request. Returning 0 indicates success, -1 if
there was an error. The following values can be retrieved from
`uv_last_error` in the case of an error:
2011-05-28 08:47:47 +00:00
2011-05-28 22:40:10 +00:00
* ???
2011-05-29 01:52:33 +00:00
# `uv_shutdown_req_t`
Subclass of `uv_req_t`. Represents an ongoing shutdown request. Once the
write queue of the parent `uv_stream_t` is drained, the outbound data
channel is shutdown. Once a shutdown request is initiated on a stream, the
stream will allow no more writes.
int uv_shutdown_req_init(uv_shutdown_req_t*,
uv_stream_t* parent);
2011-05-29 01:52:33 +00:00
Initializes the shutdown request.
# `uv_write_req_t`
int uv_write_req_init(uv_write_req_t*,
uv_stream_t*,
uv_buf_t bufs[],
int butcnf);
2011-05-29 01:52:33 +00:00
Initiates a write request on a stream.