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_read_stop(uv_stream_t* handle) {
102 if (!(handle->flags & UV_HANDLE_READING))
106 if (handle->type == UV_TTY) {
107 err = uv_tty_read_stop((uv_tty_t*) handle);
109 if (handle->type == UV_NAMED_PIPE) {
110 uv__pipe_stop_read((uv_pipe_t*) handle);
112 handle->flags &= ~UV_HANDLE_READING;
114 DECREASE_ACTIVE_COUNT(handle->loop, handle);
117 return uv_translate_sys_error(err);
121 int uv_write(uv_write_t* req,
123 const uv_buf_t bufs[],
126 uv_loop_t* loop = handle->loop;
129 if (!(handle->flags & UV_HANDLE_WRITABLE)) {
133 err = ERROR_INVALID_PARAMETER;
134 switch (handle->type) {
136 err = uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb);
139 err = uv_pipe_write(loop, req, (uv_pipe_t*) handle, bufs, nbufs, cb);
142 err = uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb);
148 return uv_translate_sys_error(err);
152 int uv_write2(uv_write_t* req,
154 const uv_buf_t bufs[],
156 uv_stream_t* send_handle,
158 uv_loop_t* loop = handle->loop;
161 if (!(handle->flags & UV_HANDLE_WRITABLE)) {
165 err = ERROR_INVALID_PARAMETER;
166 switch (handle->type) {
168 err = uv_pipe_write2(loop,
180 return uv_translate_sys_error(err);
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)
189 if (!(stream->flags & UV_HANDLE_WRITABLE))
192 switch (stream->type) {
194 return uv__tcp_try_write((uv_tcp_t*) stream, bufs, nbufs);
196 return uv__tty_try_write((uv_tty_t*) stream, bufs, nbufs);
206 int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
207 uv_loop_t* loop = handle->loop;
209 if (!(handle->flags & UV_HANDLE_WRITABLE)) {
213 uv_req_init(loop, (uv_req_t*) req);
214 req->type = UV_SHUTDOWN;
215 req->handle = handle;
218 handle->flags &= ~UV_HANDLE_WRITABLE;
219 handle->stream.conn.shutdown_req = req;
220 handle->reqs_pending++;
221 REGISTER_HANDLE_REQ(loop, handle, req);
223 uv_want_endgame(loop, (uv_handle_t*)handle);
229 int uv_is_readable(const uv_stream_t* handle) {
230 return !!(handle->flags & UV_HANDLE_READABLE);
234 int uv_is_writable(const uv_stream_t* handle) {
235 return !!(handle->flags & UV_HANDLE_WRITABLE);
239 int uv_stream_set_blocking(uv_stream_t* handle, int blocking) {
240 if (handle->type != UV_NAMED_PIPE)
244 handle->flags |= UV_HANDLE_BLOCKING_WRITES;
246 handle->flags &= ~UV_HANDLE_BLOCKING_WRITES;