libuv-svace-build
252 строки · 6.4 Кб
1/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2*
3* Permission is hereby granted, free of charge, to any person obtaining a copy
4* of this software and associated documentation files (the "Software"), to
5* deal in the Software without restriction, including without limitation the
6* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7* sell copies of the Software, and to permit persons to whom the Software is
8* furnished to do so, subject to the following conditions:
9*
10* The above copyright notice and this permission notice shall be included in
11* all copies or substantial portions of the Software.
12*
13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19* IN THE SOFTWARE.
20*/
21
22#include <assert.h>
23
24#include "uv.h"
25#include "internal.h"
26#include "handle-inl.h"
27#include "req-inl.h"
28
29
30int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) {
31int err;
32if (uv__is_closing(stream)) {
33return UV_EINVAL;
34}
35err = ERROR_INVALID_PARAMETER;
36switch (stream->type) {
37case UV_TCP:
38err = uv__tcp_listen((uv_tcp_t*)stream, backlog, cb);
39break;
40case UV_NAMED_PIPE:
41err = uv__pipe_listen((uv_pipe_t*)stream, backlog, cb);
42break;
43default:
44assert(0);
45}
46
47return uv_translate_sys_error(err);
48}
49
50
51int uv_accept(uv_stream_t* server, uv_stream_t* client) {
52int err;
53
54err = ERROR_INVALID_PARAMETER;
55switch (server->type) {
56case UV_TCP:
57err = uv__tcp_accept((uv_tcp_t*)server, (uv_tcp_t*)client);
58break;
59case UV_NAMED_PIPE:
60err = uv__pipe_accept((uv_pipe_t*)server, client);
61break;
62default:
63assert(0);
64}
65
66return uv_translate_sys_error(err);
67}
68
69
70int uv__read_start(uv_stream_t* handle,
71uv_alloc_cb alloc_cb,
72uv_read_cb read_cb) {
73int err;
74
75err = ERROR_INVALID_PARAMETER;
76switch (handle->type) {
77case UV_TCP:
78err = uv__tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
79break;
80case UV_NAMED_PIPE:
81err = uv__pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
82break;
83case UV_TTY:
84err = uv__tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
85break;
86default:
87assert(0);
88}
89
90return uv_translate_sys_error(err);
91}
92
93
94int uv_read_stop(uv_stream_t* handle) {
95int err;
96
97if (!(handle->flags & UV_HANDLE_READING))
98return 0;
99
100err = 0;
101if (handle->type == UV_TTY) {
102err = uv__tty_read_stop((uv_tty_t*) handle);
103} else if (handle->type == UV_NAMED_PIPE) {
104uv__pipe_read_stop((uv_pipe_t*) handle);
105} else {
106handle->flags &= ~UV_HANDLE_READING;
107DECREASE_ACTIVE_COUNT(handle->loop, handle);
108}
109
110return uv_translate_sys_error(err);
111}
112
113
114int uv_write(uv_write_t* req,
115uv_stream_t* handle,
116const uv_buf_t bufs[],
117unsigned int nbufs,
118uv_write_cb cb) {
119uv_loop_t* loop = handle->loop;
120int err;
121
122if (!(handle->flags & UV_HANDLE_WRITABLE)) {
123return UV_EPIPE;
124}
125
126err = ERROR_INVALID_PARAMETER;
127switch (handle->type) {
128case UV_TCP:
129err = uv__tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb);
130break;
131case UV_NAMED_PIPE:
132err = uv__pipe_write(
133loop, req, (uv_pipe_t*) handle, bufs, nbufs, NULL, cb);
134break;
135case UV_TTY:
136err = uv__tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb);
137break;
138default:
139assert(0);
140}
141
142return uv_translate_sys_error(err);
143}
144
145
146int uv_write2(uv_write_t* req,
147uv_stream_t* handle,
148const uv_buf_t bufs[],
149unsigned int nbufs,
150uv_stream_t* send_handle,
151uv_write_cb cb) {
152uv_loop_t* loop = handle->loop;
153int err;
154
155if (send_handle == NULL) {
156return uv_write(req, handle, bufs, nbufs, cb);
157}
158
159if (handle->type != UV_NAMED_PIPE || !((uv_pipe_t*) handle)->ipc) {
160return UV_EINVAL;
161} else if (!(handle->flags & UV_HANDLE_WRITABLE)) {
162return UV_EPIPE;
163}
164
165err = uv__pipe_write(
166loop, req, (uv_pipe_t*) handle, bufs, nbufs, send_handle, cb);
167return uv_translate_sys_error(err);
168}
169
170
171int uv_try_write(uv_stream_t* stream,
172const uv_buf_t bufs[],
173unsigned int nbufs) {
174if (stream->flags & UV_HANDLE_CLOSING)
175return UV_EBADF;
176if (!(stream->flags & UV_HANDLE_WRITABLE))
177return UV_EPIPE;
178
179switch (stream->type) {
180case UV_TCP:
181return uv__tcp_try_write((uv_tcp_t*) stream, bufs, nbufs);
182case UV_TTY:
183return uv__tty_try_write((uv_tty_t*) stream, bufs, nbufs);
184case UV_NAMED_PIPE:
185return UV_EAGAIN;
186default:
187assert(0);
188return UV_ENOSYS;
189}
190}
191
192
193int uv_try_write2(uv_stream_t* stream,
194const uv_buf_t bufs[],
195unsigned int nbufs,
196uv_stream_t* send_handle) {
197if (send_handle != NULL)
198return UV_EAGAIN;
199return uv_try_write(stream, bufs, nbufs);
200}
201
202
203int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
204uv_loop_t* loop = handle->loop;
205
206if (!(handle->flags & UV_HANDLE_WRITABLE) ||
207uv__is_stream_shutting(handle) ||
208uv__is_closing(handle)) {
209return UV_ENOTCONN;
210}
211
212UV_REQ_INIT(req, UV_SHUTDOWN);
213req->handle = handle;
214req->cb = cb;
215
216handle->flags &= ~UV_HANDLE_WRITABLE;
217handle->stream.conn.shutdown_req = req;
218handle->reqs_pending++;
219REGISTER_HANDLE_REQ(loop, handle, req);
220
221if (handle->stream.conn.write_reqs_pending == 0) {
222if (handle->type == UV_NAMED_PIPE)
223uv__pipe_shutdown(loop, (uv_pipe_t*) handle, req);
224else
225uv__insert_pending_req(loop, (uv_req_t*) req);
226}
227
228return 0;
229}
230
231
232int uv_is_readable(const uv_stream_t* handle) {
233return !!(handle->flags & UV_HANDLE_READABLE);
234}
235
236
237int uv_is_writable(const uv_stream_t* handle) {
238return !!(handle->flags & UV_HANDLE_WRITABLE);
239}
240
241
242int uv_stream_set_blocking(uv_stream_t* handle, int blocking) {
243if (handle->type != UV_NAMED_PIPE)
244return UV_EINVAL;
245
246if (blocking != 0)
247handle->flags |= UV_HANDLE_BLOCKING_WRITES;
248else
249handle->flags &= ~UV_HANDLE_BLOCKING_WRITES;
250
251return 0;
252}
253