1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
26 #include "handle-inl.h"
30 int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) {
33 err = ERROR_INVALID_PARAMETER;
34 switch (stream->type) {
36 err = uv_tcp_listen((uv_tcp_t*)stream, backlog, cb);
39 err = uv_pipe_listen((uv_pipe_t*)stream, backlog, cb);
45 return uv_translate_sys_error(err);
49 int uv_accept(uv_stream_t* server, uv_stream_t* client) {
52 err = ERROR_INVALID_PARAMETER;
53 switch (server->type) {
55 err = uv_tcp_accept((uv_tcp_t*)server, (uv_tcp_t*)client);
58 err = uv_pipe_accept((uv_pipe_t*)server, client);
64 return uv_translate_sys_error(err);
68 int uv_read_start(uv_stream_t* handle, uv_alloc_cb alloc_cb,
72 if (handle->flags & UV_HANDLE_READING) {
76 if (!(handle->flags & UV_HANDLE_READABLE)) {
80 err = ERROR_INVALID_PARAMETER;
81 switch (handle->type) {
83 err = uv_tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
86 err = uv_pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
89 err = uv_tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
95 return uv_translate_sys_error(err);
99 int uv_read2_start(uv_stream_t* handle, uv_alloc_cb alloc_cb,
100 uv_read2_cb read_cb) {
103 if (handle->flags & UV_HANDLE_READING) {
107 if (!(handle->flags & UV_HANDLE_READABLE)) {
111 err = ERROR_INVALID_PARAMETER;
112 switch (handle->type) {
114 err = uv_pipe_read2_start((uv_pipe_t*)handle, alloc_cb, read_cb);
120 return uv_translate_sys_error(err);
124 int uv_read_stop(uv_stream_t* handle) {
127 if (!(handle->flags & UV_HANDLE_READING))
131 if (handle->type == UV_TTY) {
132 err = uv_tty_read_stop((uv_tty_t*) handle);
134 handle->flags &= ~UV_HANDLE_READING;
135 DECREASE_ACTIVE_COUNT(handle->loop, handle);
138 return uv_translate_sys_error(err);
142 int uv_write(uv_write_t* req,
144 const uv_buf_t bufs[],
147 uv_loop_t* loop = handle->loop;
150 if (!(handle->flags & UV_HANDLE_WRITABLE)) {
154 err = ERROR_INVALID_PARAMETER;
155 switch (handle->type) {
157 err = uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb);
160 err = uv_pipe_write(loop, req, (uv_pipe_t*) handle, bufs, nbufs, cb);
163 err = uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb);
169 return uv_translate_sys_error(err);
173 int uv_write2(uv_write_t* req,
175 const uv_buf_t bufs[],
177 uv_stream_t* send_handle,
179 uv_loop_t* loop = handle->loop;
182 if (!(handle->flags & UV_HANDLE_WRITABLE)) {
186 err = ERROR_INVALID_PARAMETER;
187 switch (handle->type) {
189 err = uv_pipe_write2(loop,
201 return uv_translate_sys_error(err);
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 */
213 int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
214 uv_loop_t* loop = handle->loop;
216 if (!(handle->flags & UV_HANDLE_WRITABLE)) {
220 uv_req_init(loop, (uv_req_t*) req);
221 req->type = UV_SHUTDOWN;
222 req->handle = handle;
225 handle->flags &= ~UV_HANDLE_WRITABLE;
226 handle->shutdown_req = req;
227 handle->reqs_pending++;
228 REGISTER_HANDLE_REQ(loop, handle, req);
230 uv_want_endgame(loop, (uv_handle_t*)handle);
236 int uv_is_readable(const uv_stream_t* handle) {
237 return !!(handle->flags & UV_HANDLE_READABLE);
241 int uv_is_writable(const uv_stream_t* handle) {
242 return !!(handle->flags & UV_HANDLE_WRITABLE);
246 int uv_stream_set_blocking(uv_stream_t* handle, int blocking) {
248 handle->flags |= UV_HANDLE_BLOCKING_WRITES;
250 handle->flags &= ~UV_HANDLE_BLOCKING_WRITES;