1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
.. _stream:
:c:type:`uv_stream_t` --- Stream handle
=======================================
Stream handles provide an abstraction of a duplex communication channel.
:c:type:`uv_stream_t` is an abstract type, libuv provides 3 stream implementations
in the for of :c:type:`uv_tcp_t`, :c:type:`uv_pipe_t` and :c:type:`uv_tty_t`.
Data types
----------
.. c:type:: uv_stream_t
Stream handle type.
.. c:type:: uv_connect_t
Connect request type.
.. c:type:: uv_shutdown_t
Shutdown request type.
.. c:type:: uv_write_t
Write request type.
.. c:type:: void (*uv_read_cb)(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)
Callback called when data was read on a stream.
`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 stopping closing the stream when an error happens
by calling :c:func:`uv_read_stop` or :c:func:`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
error.
.. c:type:: void (*uv_write_cb)(uv_write_t* req, int status)
Callback called after data was written on a stream. `status` will be 0 in
case of success, < 0 otherwise.
.. c:type:: void (*uv_connect_cb)(uv_connect_t* req, int status)
Callback called after a connection started by :c:func:`uv_connect` is done.
`status` will be 0 in case of success, < 0 otherwise.
.. c:type:: void (*uv_shutdown_cb)(uv_shutdown_t* req, int status)
Callback called after s shutdown request has been completed. `status` will
be 0 in case of success, < 0 otherwise.
.. c:type:: void (*uv_connection_cb)(uv_stream_t* server, int status)
Callback called when a stream server has received an incoming connection.
The user can accept the connection by calling :c:func:`uv_accept`.
`status` will be 0 in case of success, < 0 otherwise.
Public members
^^^^^^^^^^^^^^
.. c:member:: size_t uv_stream_t.write_queue_size
Contains the amount of queued bytes waiting to be sent. Readonly.
.. c:member:: uv_stream_t* uv_connect_t.handle
Pointer to the stream where this connection request is running.
.. c:member:: uv_stream_t* uv_shutdown_t.handle
Pointer to the stream where this shutdown request is running.
.. c:member:: uv_stream_t* uv_write_t.handle
Pointer to the stream where this write request is running.
.. c:member:: uv_stream_t* uv_write_t.send_handle
Pointer to the stream being sent using this write request..
.. seealso:: The :c:type:`uv_handle_t` members also apply.
API
---
.. c:function:: int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb)
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.
.. c:function:: int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb)
Start listening for incoming connections. `backlog` indicates the number of
connections the kernel might queue, same as :man:`listen(2)`. When a new
incoming connection is received the :c:type:`uv_connection_cb` callback is
called.
.. c:function:: int uv_accept(uv_stream_t* server, uv_stream_t* client)
This call is used in conjunction with :c:func:`uv_listen` to accept incoming
connections. Call this function after receiving a :c:type:`uv_connection_cb`
to accept the connection. Before calling this function the client handle must
be initialized. < 0 return value indicates an error.
When the :c:type:`uv_connection_cb` callback is called it is guaranteed that
this function will complete successfully the first time. If you attempt to use
it more than once, it may fail. It is suggested to only call this function once
per :c:type:`uv_connection_cb` call.
.. note::
`server` and `client` must be handles running on the same loop.
.. c:function:: int uv_read_start(uv_stream_t*, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Read data from an incoming stream. The callback will be made several
times until there is no more data to read or :c:func:`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.
.. note::
`nread` might also be 0, which does *not* indicate an error or EOF, it happens when
libuv requested a buffer through the alloc callback but then decided that it didn't
need that buffer.
.. c:function:: int uv_read_stop(uv_stream_t*)
Stop reading data from the stream. The :c:type:`uv_read_cb` callback will
no longer be called.
.. 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)
Write data to stream. Buffers are written in order. Example:
::
uv_buf_t a[] = {
{ .base = "1", .len = 1 },
{ .base = "2", .len = 1 }
};
uv_buf_t b[] = {
{ .base = "3", .len = 1 },
{ .base = "4", .len = 1 }
};
uv_write_t req1;
uv_write_t req2;
/* writes "1234" */
uv_write(&req1, stream, a, 2);
uv_write(&req2, stream, b, 2);
.. 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)
Extended write function for sending handles over a pipe. The pipe must be
initialized with `ipc` == 1.
.. note::
`send_handle` must be a TCP socket or pipe, which is a server or a connection (listening
or connected state). Bound sockets or pipes will be assumed to be servers.
.. c:function:: int uv_try_write(uv_stream_t* handle, const uv_buf_t bufs[], unsigned int nbufs)
Same as :c:func:`uv_write`, but won't queue a 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 (``UV_EAGAIN`` is returned if no data can be sent
immediately).
.. c:function:: int uv_is_readable(const uv_stream_t* handle)
Returns 1 if the stream is readable, 0 otherwise.
.. c:function:: int uv_is_writable(const uv_stream_t* handle)
Returns 1 if the stream is writable, 0 otherwise.
.. c:function:: int uv_stream_set_blocking(uv_stream_t* handle, int blocking)
Enable or disable blocking mode for a stream.
When blocking mode is enabled all writes complete synchronously. The
interface remains unchanged otherwise, e.g. completion or failure of the
operation will still be reported through a callback which is made
asynchronously.
.. warning::
Relying too much on this API is not recommended. It is likely to change
significantly in the future.
Currently only works on Windows for :c:type:`uv_pipe_t` handles.
On UNIX platforms, all :c:type:`uv_stream_t` handles are supported.
Also libuv currently makes no ordering guarantee when the blocking mode
is changed after write requests have already been submitted. Therefore it is
recommended to set the blocking mode immediately after opening or creating
the stream.
.. versionchanged:: 1.4.0 UNIX implementation added.
.. seealso:: The :c:type:`uv_handle_t` API functions also apply.
|