abf477f64421d50843acd4af6729eb0ff62ec091
[platform/upstream/cmake.git] / Utilities / cmlibuv / 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,
69                    uv_alloc_cb alloc_cb,
70                    uv_read_cb read_cb) {
71   int err;
72
73   err = ERROR_INVALID_PARAMETER;
74   switch (handle->type) {
75     case UV_TCP:
76       err = uv_tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
77       break;
78     case UV_NAMED_PIPE:
79       err = uv_pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
80       break;
81     case UV_TTY:
82       err = uv_tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
83       break;
84     default:
85       assert(0);
86   }
87
88   return uv_translate_sys_error(err);
89 }
90
91
92 int uv_read_stop(uv_stream_t* handle) {
93   int err;
94
95   if (!(handle->flags & UV_HANDLE_READING))
96     return 0;
97
98   err = 0;
99   if (handle->type == UV_TTY) {
100     err = uv_tty_read_stop((uv_tty_t*) handle);
101   } else if (handle->type == UV_NAMED_PIPE) {
102     uv__pipe_read_stop((uv_pipe_t*) handle);
103   } else {
104     handle->flags &= ~UV_HANDLE_READING;
105     DECREASE_ACTIVE_COUNT(handle->loop, handle);
106   }
107
108   return uv_translate_sys_error(err);
109 }
110
111
112 int uv_write(uv_write_t* req,
113              uv_stream_t* handle,
114              const uv_buf_t bufs[],
115              unsigned int nbufs,
116              uv_write_cb cb) {
117   uv_loop_t* loop = handle->loop;
118   int err;
119
120   if (!(handle->flags & UV_HANDLE_WRITABLE)) {
121     return UV_EPIPE;
122   }
123
124   err = ERROR_INVALID_PARAMETER;
125   switch (handle->type) {
126     case UV_TCP:
127       err = uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb);
128       break;
129     case UV_NAMED_PIPE:
130       err = uv__pipe_write(
131           loop, req, (uv_pipe_t*) handle, bufs, nbufs, NULL, cb);
132       break;
133     case UV_TTY:
134       err = uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb);
135       break;
136     default:
137       assert(0);
138   }
139
140   return uv_translate_sys_error(err);
141 }
142
143
144 int uv_write2(uv_write_t* req,
145               uv_stream_t* handle,
146               const uv_buf_t bufs[],
147               unsigned int nbufs,
148               uv_stream_t* send_handle,
149               uv_write_cb cb) {
150   uv_loop_t* loop = handle->loop;
151   int err;
152
153   if (send_handle == NULL) {
154     return uv_write(req, handle, bufs, nbufs, cb);
155   }
156
157   if (handle->type != UV_NAMED_PIPE || !((uv_pipe_t*) handle)->ipc) {
158     return UV_EINVAL;
159   } else if (!(handle->flags & UV_HANDLE_WRITABLE)) {
160     return UV_EPIPE;
161   }
162
163   err = uv__pipe_write(
164       loop, req, (uv_pipe_t*) handle, bufs, nbufs, send_handle, cb);
165   return uv_translate_sys_error(err);
166 }
167
168
169 int uv_try_write(uv_stream_t* stream,
170                  const uv_buf_t bufs[],
171                  unsigned int nbufs) {
172   if (stream->flags & UV_HANDLE_CLOSING)
173     return UV_EBADF;
174   if (!(stream->flags & UV_HANDLE_WRITABLE))
175     return UV_EPIPE;
176
177   switch (stream->type) {
178     case UV_TCP:
179       return uv__tcp_try_write((uv_tcp_t*) stream, bufs, nbufs);
180     case UV_TTY:
181       return uv__tty_try_write((uv_tty_t*) stream, bufs, nbufs);
182     case UV_NAMED_PIPE:
183       return UV_EAGAIN;
184     default:
185       assert(0);
186       return UV_ENOSYS;
187   }
188 }
189
190
191 int uv_try_write2(uv_stream_t* stream,
192                   const uv_buf_t bufs[],
193                   unsigned int nbufs,
194                   uv_stream_t* send_handle) {
195   if (send_handle != NULL)
196     return UV_EAGAIN;
197   return uv_try_write(stream, bufs, nbufs);
198 }
199
200
201 int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
202   uv_loop_t* loop = handle->loop;
203
204   if (!(handle->flags & UV_HANDLE_WRITABLE) ||
205       handle->flags & UV_HANDLE_SHUTTING ||
206       uv__is_closing(handle)) {
207     return UV_ENOTCONN;
208   }
209
210   UV_REQ_INIT(req, UV_SHUTDOWN);
211   req->handle = handle;
212   req->cb = cb;
213
214   handle->flags &= ~UV_HANDLE_WRITABLE;
215   handle->flags |= UV_HANDLE_SHUTTING;
216   handle->stream.conn.shutdown_req = req;
217   handle->reqs_pending++;
218   REGISTER_HANDLE_REQ(loop, handle, req);
219
220   uv_want_endgame(loop, (uv_handle_t*)handle);
221
222   return 0;
223 }
224
225
226 int uv_is_readable(const uv_stream_t* handle) {
227   return !!(handle->flags & UV_HANDLE_READABLE);
228 }
229
230
231 int uv_is_writable(const uv_stream_t* handle) {
232   return !!(handle->flags & UV_HANDLE_WRITABLE);
233 }
234
235
236 int uv_stream_set_blocking(uv_stream_t* handle, int blocking) {
237   if (handle->type != UV_NAMED_PIPE)
238     return UV_EINVAL;
239
240   if (blocking != 0)
241     handle->flags |= UV_HANDLE_BLOCKING_WRITES;
242   else
243     handle->flags &= ~UV_HANDLE_BLOCKING_WRITES;
244
245   return 0;
246 }