revise installing a license file
[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_read_stop(uv_stream_t* handle) {
100   int err;
101
102   if (!(handle->flags & UV_HANDLE_READING))
103     return 0;
104
105   err = 0;
106   if (handle->type == UV_TTY) {
107     err = uv_tty_read_stop((uv_tty_t*) handle);
108   } else {
109     if (handle->type == UV_NAMED_PIPE) {
110       uv__pipe_stop_read((uv_pipe_t*) handle);
111     } else {
112       handle->flags &= ~UV_HANDLE_READING;
113     }
114     DECREASE_ACTIVE_COUNT(handle->loop, handle);
115   }
116
117   return uv_translate_sys_error(err);
118 }
119
120
121 int uv_write(uv_write_t* req,
122              uv_stream_t* handle,
123              const uv_buf_t bufs[],
124              unsigned int nbufs,
125              uv_write_cb cb) {
126   uv_loop_t* loop = handle->loop;
127   int err;
128
129   if (!(handle->flags & UV_HANDLE_WRITABLE)) {
130     return UV_EPIPE;
131   }
132
133   err = ERROR_INVALID_PARAMETER;
134   switch (handle->type) {
135     case UV_TCP:
136       err = uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb);
137       break;
138     case UV_NAMED_PIPE:
139       err = uv_pipe_write(loop, req, (uv_pipe_t*) handle, bufs, nbufs, cb);
140       break;
141     case UV_TTY:
142       err = uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb);
143       break;
144     default:
145       assert(0);
146   }
147
148   return uv_translate_sys_error(err);
149 }
150
151
152 int uv_write2(uv_write_t* req,
153               uv_stream_t* handle,
154               const uv_buf_t bufs[],
155               unsigned int nbufs,
156               uv_stream_t* send_handle,
157               uv_write_cb cb) {
158   uv_loop_t* loop = handle->loop;
159   int err;
160
161   if (!(handle->flags & UV_HANDLE_WRITABLE)) {
162     return UV_EPIPE;
163   }
164
165   err = ERROR_INVALID_PARAMETER;
166   switch (handle->type) {
167     case UV_NAMED_PIPE:
168       err = uv_pipe_write2(loop,
169                            req,
170                            (uv_pipe_t*) handle,
171                            bufs,
172                            nbufs,
173                            send_handle,
174                            cb);
175       break;
176     default:
177       assert(0);
178   }
179
180   return uv_translate_sys_error(err);
181 }
182
183
184 int uv_try_write(uv_stream_t* stream,
185                  const uv_buf_t bufs[],
186                  unsigned int nbufs) {
187   if (stream->flags & UV__HANDLE_CLOSING)
188     return UV_EBADF;
189   if (!(stream->flags & UV_HANDLE_WRITABLE))
190     return UV_EPIPE;
191
192   switch (stream->type) {
193     case UV_TCP:
194       return uv__tcp_try_write((uv_tcp_t*) stream, bufs, nbufs);
195     case UV_TTY:
196       return uv__tty_try_write((uv_tty_t*) stream, bufs, nbufs);
197     case UV_NAMED_PIPE:
198       return UV_EAGAIN;
199     default:
200       assert(0);
201       return UV_ENOSYS;
202   }
203 }
204
205
206 int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
207   uv_loop_t* loop = handle->loop;
208
209   if (!(handle->flags & UV_HANDLE_WRITABLE)) {
210     return UV_EPIPE;
211   }
212
213   uv_req_init(loop, (uv_req_t*) req);
214   req->type = UV_SHUTDOWN;
215   req->handle = handle;
216   req->cb = cb;
217
218   handle->flags &= ~UV_HANDLE_WRITABLE;
219   handle->stream.conn.shutdown_req = req;
220   handle->reqs_pending++;
221   REGISTER_HANDLE_REQ(loop, handle, req);
222
223   uv_want_endgame(loop, (uv_handle_t*)handle);
224
225   return 0;
226 }
227
228
229 int uv_is_readable(const uv_stream_t* handle) {
230   return !!(handle->flags & UV_HANDLE_READABLE);
231 }
232
233
234 int uv_is_writable(const uv_stream_t* handle) {
235   return !!(handle->flags & UV_HANDLE_WRITABLE);
236 }
237
238
239 int uv_stream_set_blocking(uv_stream_t* handle, int blocking) {
240   if (handle->type != UV_NAMED_PIPE)
241     return UV_EINVAL;
242
243   if (blocking != 0)
244     handle->flags |= UV_HANDLE_BLOCKING_WRITES;
245   else
246     handle->flags &= ~UV_HANDLE_BLOCKING_WRITES;
247
248   return 0;
249 }