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,
73 err = ERROR_INVALID_PARAMETER;
74 switch (handle->type) {
76 err = uv_tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
79 err = uv_pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
82 err = uv_tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
88 return uv_translate_sys_error(err);
92 int uv_read_stop(uv_stream_t* handle) {
95 if (!(handle->flags & UV_HANDLE_READING))
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);
104 handle->flags &= ~UV_HANDLE_READING;
105 DECREASE_ACTIVE_COUNT(handle->loop, handle);
108 return uv_translate_sys_error(err);
112 int uv_write(uv_write_t* req,
114 const uv_buf_t bufs[],
117 uv_loop_t* loop = handle->loop;
120 if (!(handle->flags & UV_HANDLE_WRITABLE)) {
124 err = ERROR_INVALID_PARAMETER;
125 switch (handle->type) {
127 err = uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb);
130 err = uv__pipe_write(
131 loop, req, (uv_pipe_t*) handle, bufs, nbufs, NULL, cb);
134 err = uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb);
140 return uv_translate_sys_error(err);
144 int uv_write2(uv_write_t* req,
146 const uv_buf_t bufs[],
148 uv_stream_t* send_handle,
150 uv_loop_t* loop = handle->loop;
153 if (send_handle == NULL) {
154 return uv_write(req, handle, bufs, nbufs, cb);
157 if (handle->type != UV_NAMED_PIPE || !((uv_pipe_t*) handle)->ipc) {
159 } else if (!(handle->flags & UV_HANDLE_WRITABLE)) {
163 err = uv__pipe_write(
164 loop, req, (uv_pipe_t*) handle, bufs, nbufs, send_handle, cb);
165 return uv_translate_sys_error(err);
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)
174 if (!(stream->flags & UV_HANDLE_WRITABLE))
177 switch (stream->type) {
179 return uv__tcp_try_write((uv_tcp_t*) stream, bufs, nbufs);
181 return uv__tty_try_write((uv_tty_t*) stream, bufs, nbufs);
191 int uv_try_write2(uv_stream_t* stream,
192 const uv_buf_t bufs[],
194 uv_stream_t* send_handle) {
195 if (send_handle != NULL)
197 return uv_try_write(stream, bufs, nbufs);
201 int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
202 uv_loop_t* loop = handle->loop;
204 if (!(handle->flags & UV_HANDLE_WRITABLE) ||
205 handle->flags & UV_HANDLE_SHUTTING ||
206 uv__is_closing(handle)) {
210 UV_REQ_INIT(req, UV_SHUTDOWN);
211 req->handle = handle;
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);
220 uv_want_endgame(loop, (uv_handle_t*)handle);
226 int uv_is_readable(const uv_stream_t* handle) {
227 return !!(handle->flags & UV_HANDLE_READABLE);
231 int uv_is_writable(const uv_stream_t* handle) {
232 return !!(handle->flags & UV_HANDLE_WRITABLE);
236 int uv_stream_set_blocking(uv_stream_t* handle, int blocking) {
237 if (handle->type != UV_NAMED_PIPE)
241 handle->flags |= UV_HANDLE_BLOCKING_WRITES;
243 handle->flags &= ~UV_HANDLE_BLOCKING_WRITES;