uv: Upgrade to v0.11.17
[platform/upstream/nodejs.git] / deps / uv / src / win / stream.c
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
30 int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) {
31   int err;
32
33   err = ERROR_INVALID_PARAMETER;
34   switch (stream->type) {
35     case UV_TCP:
36       err = uv_tcp_listen((uv_tcp_t*)stream, backlog, cb);
37       break;
38     case UV_NAMED_PIPE:
39       err = uv_pipe_listen((uv_pipe_t*)stream, backlog, cb);
40       break;
41     default:
42       assert(0);
43   }
44
45   return uv_translate_sys_error(err);
46 }
47
48
49 int uv_accept(uv_stream_t* server, uv_stream_t* client) {
50   int err;
51
52   err = ERROR_INVALID_PARAMETER;
53   switch (server->type) {
54     case UV_TCP:
55       err = uv_tcp_accept((uv_tcp_t*)server, (uv_tcp_t*)client);
56       break;
57     case UV_NAMED_PIPE:
58       err = uv_pipe_accept((uv_pipe_t*)server, client);
59       break;
60     default:
61       assert(0);
62   }
63
64   return uv_translate_sys_error(err);
65 }
66
67
68 int uv_read_start(uv_stream_t* handle, uv_alloc_cb alloc_cb,
69     uv_read_cb read_cb) {
70   int err;
71
72   if (handle->flags & UV_HANDLE_READING) {
73     return UV_EALREADY;
74   }
75
76   if (!(handle->flags & UV_HANDLE_READABLE)) {
77     return UV_ENOTCONN;
78   }
79
80   err = ERROR_INVALID_PARAMETER;
81   switch (handle->type) {
82     case UV_TCP:
83       err = uv_tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
84       break;
85     case UV_NAMED_PIPE:
86       err = uv_pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
87       break;
88     case UV_TTY:
89       err = uv_tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
90       break;
91     default:
92       assert(0);
93   }
94
95   return uv_translate_sys_error(err);
96 }
97
98
99 int uv_read2_start(uv_stream_t* handle, uv_alloc_cb alloc_cb,
100     uv_read2_cb read_cb) {
101   int err;
102
103   if (handle->flags & UV_HANDLE_READING) {
104     return UV_EALREADY;
105   }
106
107   if (!(handle->flags & UV_HANDLE_READABLE)) {
108     return UV_ENOTCONN;
109   }
110
111   err = ERROR_INVALID_PARAMETER;
112   switch (handle->type) {
113     case UV_NAMED_PIPE:
114       err = uv_pipe_read2_start((uv_pipe_t*)handle, alloc_cb, read_cb);
115       break;
116     default:
117       assert(0);
118   }
119
120   return uv_translate_sys_error(err);
121 }
122
123
124 int uv_read_stop(uv_stream_t* handle) {
125   int err;
126
127   if (!(handle->flags & UV_HANDLE_READING))
128     return 0;
129
130   err = 0;
131   if (handle->type == UV_TTY) {
132     err = uv_tty_read_stop((uv_tty_t*) handle);
133   } else {
134     handle->flags &= ~UV_HANDLE_READING;
135     DECREASE_ACTIVE_COUNT(handle->loop, handle);
136   }
137
138   return uv_translate_sys_error(err);
139 }
140
141
142 int uv_write(uv_write_t* req,
143              uv_stream_t* handle,
144              const uv_buf_t bufs[],
145              unsigned int nbufs,
146              uv_write_cb cb) {
147   uv_loop_t* loop = handle->loop;
148   int err;
149
150   if (!(handle->flags & UV_HANDLE_WRITABLE)) {
151     return UV_EPIPE;
152   }
153
154   err = ERROR_INVALID_PARAMETER;
155   switch (handle->type) {
156     case UV_TCP:
157       err = uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb);
158       break;
159     case UV_NAMED_PIPE:
160       err = uv_pipe_write(loop, req, (uv_pipe_t*) handle, bufs, nbufs, cb);
161       break;
162     case UV_TTY:
163       err = uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb);
164       break;
165     default:
166       assert(0);
167   }
168
169   return uv_translate_sys_error(err);
170 }
171
172
173 int uv_write2(uv_write_t* req,
174               uv_stream_t* handle,
175               const uv_buf_t bufs[],
176               unsigned int nbufs,
177               uv_stream_t* send_handle,
178               uv_write_cb cb) {
179   uv_loop_t* loop = handle->loop;
180   int err;
181
182   if (!(handle->flags & UV_HANDLE_WRITABLE)) {
183     return UV_EPIPE;
184   }
185
186   err = ERROR_INVALID_PARAMETER;
187   switch (handle->type) {
188     case UV_NAMED_PIPE:
189       err = uv_pipe_write2(loop,
190                            req,
191                            (uv_pipe_t*) handle,
192                            bufs,
193                            nbufs,
194                            send_handle,
195                            cb);
196       break;
197     default:
198       assert(0);
199   }
200
201   return uv_translate_sys_error(err);
202 }
203
204
205 int uv_try_write(uv_stream_t* stream,
206                  const uv_buf_t bufs[],
207                  unsigned int nbufs) {
208   /* NOTE: Won't work with overlapped writes */
209   return UV_ENOSYS;
210 }
211
212
213 int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
214   uv_loop_t* loop = handle->loop;
215
216   if (!(handle->flags & UV_HANDLE_WRITABLE)) {
217     return UV_EPIPE;
218   }
219
220   uv_req_init(loop, (uv_req_t*) req);
221   req->type = UV_SHUTDOWN;
222   req->handle = handle;
223   req->cb = cb;
224
225   handle->flags &= ~UV_HANDLE_WRITABLE;
226   handle->shutdown_req = req;
227   handle->reqs_pending++;
228   REGISTER_HANDLE_REQ(loop, handle, req);
229
230   uv_want_endgame(loop, (uv_handle_t*)handle);
231
232   return 0;
233 }
234
235
236 int uv_is_readable(const uv_stream_t* handle) {
237   return !!(handle->flags & UV_HANDLE_READABLE);
238 }
239
240
241 int uv_is_writable(const uv_stream_t* handle) {
242   return !!(handle->flags & UV_HANDLE_WRITABLE);
243 }
244
245
246 int uv_stream_set_blocking(uv_stream_t* handle, int blocking) {
247   if (blocking != 0)
248     handle->flags |= UV_HANDLE_BLOCKING_WRITES;
249   else
250     handle->flags &= ~UV_HANDLE_BLOCKING_WRITES;
251
252   return 0;
253 }