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) {
32 if (uv__is_closing(stream)) {
35 err = ERROR_INVALID_PARAMETER;
36 switch (stream->type) {
38 err = uv__tcp_listen((uv_tcp_t*)stream, backlog, cb);
41 err = uv__pipe_listen((uv_pipe_t*)stream, backlog, cb);
47 return uv_translate_sys_error(err);
51 int uv_accept(uv_stream_t* server, uv_stream_t* client) {
54 err = ERROR_INVALID_PARAMETER;
55 switch (server->type) {
57 err = uv__tcp_accept((uv_tcp_t*)server, (uv_tcp_t*)client);
60 err = uv__pipe_accept((uv_pipe_t*)server, client);
66 return uv_translate_sys_error(err);
70 int uv__read_start(uv_stream_t* handle,
75 err = ERROR_INVALID_PARAMETER;
76 switch (handle->type) {
78 err = uv__tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
81 err = uv__pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
84 err = uv__tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
90 return uv_translate_sys_error(err);
94 int uv_read_stop(uv_stream_t* handle) {
97 if (!(handle->flags & UV_HANDLE_READING))
101 if (handle->type == UV_TTY) {
102 err = uv__tty_read_stop((uv_tty_t*) handle);
103 } else if (handle->type == UV_NAMED_PIPE) {
104 uv__pipe_read_stop((uv_pipe_t*) handle);
106 handle->flags &= ~UV_HANDLE_READING;
107 DECREASE_ACTIVE_COUNT(handle->loop, handle);
110 return uv_translate_sys_error(err);
114 int uv_write(uv_write_t* req,
116 const uv_buf_t bufs[],
119 uv_loop_t* loop = handle->loop;
122 if (!(handle->flags & UV_HANDLE_WRITABLE)) {
126 err = ERROR_INVALID_PARAMETER;
127 switch (handle->type) {
129 err = uv__tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb);
132 err = uv__pipe_write(
133 loop, req, (uv_pipe_t*) handle, bufs, nbufs, NULL, cb);
136 err = uv__tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb);
142 return uv_translate_sys_error(err);
146 int uv_write2(uv_write_t* req,
148 const uv_buf_t bufs[],
150 uv_stream_t* send_handle,
152 uv_loop_t* loop = handle->loop;
155 if (send_handle == NULL) {
156 return uv_write(req, handle, bufs, nbufs, cb);
159 if (handle->type != UV_NAMED_PIPE || !((uv_pipe_t*) handle)->ipc) {
161 } else if (!(handle->flags & UV_HANDLE_WRITABLE)) {
165 err = uv__pipe_write(
166 loop, req, (uv_pipe_t*) handle, bufs, nbufs, send_handle, cb);
167 return uv_translate_sys_error(err);
171 int uv_try_write(uv_stream_t* stream,
172 const uv_buf_t bufs[],
173 unsigned int nbufs) {
174 if (stream->flags & UV_HANDLE_CLOSING)
176 if (!(stream->flags & UV_HANDLE_WRITABLE))
179 switch (stream->type) {
181 return uv__tcp_try_write((uv_tcp_t*) stream, bufs, nbufs);
183 return uv__tty_try_write((uv_tty_t*) stream, bufs, nbufs);
193 int uv_try_write2(uv_stream_t* stream,
194 const uv_buf_t bufs[],
196 uv_stream_t* send_handle) {
197 if (send_handle != NULL)
199 return uv_try_write(stream, bufs, nbufs);
203 int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
204 uv_loop_t* loop = handle->loop;
206 if (!(handle->flags & UV_HANDLE_WRITABLE) ||
207 handle->flags & UV_HANDLE_SHUTTING ||
208 uv__is_closing(handle)) {
212 UV_REQ_INIT(req, UV_SHUTDOWN);
213 req->handle = handle;
216 handle->flags &= ~UV_HANDLE_WRITABLE;
217 handle->flags |= UV_HANDLE_SHUTTING;
218 handle->stream.conn.shutdown_req = req;
219 handle->reqs_pending++;
220 REGISTER_HANDLE_REQ(loop, handle, req);
222 if (handle->stream.conn.write_reqs_pending == 0) {
223 if (handle->type == UV_NAMED_PIPE)
224 uv__pipe_shutdown(loop, (uv_pipe_t*) handle, req);
226 uv__insert_pending_req(loop, (uv_req_t*) req);
233 int uv_is_readable(const uv_stream_t* handle) {
234 return !!(handle->flags & UV_HANDLE_READABLE);
238 int uv_is_writable(const uv_stream_t* handle) {
239 return !!(handle->flags & UV_HANDLE_WRITABLE);
243 int uv_stream_set_blocking(uv_stream_t* handle, int blocking) {
244 if (handle->type != UV_NAMED_PIPE)
248 handle->flags |= UV_HANDLE_BLOCKING_WRITES;
250 handle->flags &= ~UV_HANDLE_BLOCKING_WRITES;