PageRenderTime 32ms CodeModel.GetById 17ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/win/stream.c

http://github.com/joyent/libuv
C | 198 lines | 141 code | 37 blank | 20 comment | 18 complexity | b7869a509c9b864d2cc9a2d6efc40bb3 MD5 | raw file
  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
 30int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) {
 31  switch (stream->type) {
 32    case UV_TCP:
 33      return uv_tcp_listen((uv_tcp_t*)stream, backlog, cb);
 34    case UV_NAMED_PIPE:
 35      return uv_pipe_listen((uv_pipe_t*)stream, backlog, cb);
 36    default:
 37      assert(0);
 38      return -1;
 39  }
 40}
 41
 42
 43int uv_accept(uv_stream_t* server, uv_stream_t* client) {
 44  switch (server->type) {
 45    case UV_TCP:
 46      return uv_tcp_accept((uv_tcp_t*)server, (uv_tcp_t*)client);
 47    case UV_NAMED_PIPE:
 48      return uv_pipe_accept((uv_pipe_t*)server, client);
 49    default:
 50      assert(0);
 51      return -1;
 52  }
 53}
 54
 55
 56int uv_read_start(uv_stream_t* handle, uv_alloc_cb alloc_cb,
 57    uv_read_cb read_cb) {
 58  if (handle->flags & UV_HANDLE_READING) {
 59    uv__set_sys_error(handle->loop, UV_EALREADY);
 60    return -1;
 61  }
 62
 63  if (!(handle->flags & UV_HANDLE_READABLE)) {
 64    uv__set_artificial_error(handle->loop, UV_ENOTCONN);
 65    return -1;
 66  }
 67
 68  switch (handle->type) {
 69    case UV_TCP:
 70      return uv_tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
 71    case UV_NAMED_PIPE:
 72      return uv_pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
 73    case UV_TTY:
 74      return uv_tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
 75    default:
 76      assert(0);
 77      return -1;
 78  }
 79}
 80
 81
 82int uv_read2_start(uv_stream_t* handle, uv_alloc_cb alloc_cb,
 83    uv_read2_cb read_cb) {
 84  if (handle->flags & UV_HANDLE_READING) {
 85    uv__set_sys_error(handle->loop, UV_EALREADY);
 86    return -1;
 87  }
 88
 89  if (!(handle->flags & UV_HANDLE_READABLE)) {
 90    uv__set_artificial_error(handle->loop, UV_ENOTCONN);
 91    return -1;
 92  }
 93
 94  switch (handle->type) {
 95    case UV_NAMED_PIPE:
 96      return uv_pipe_read2_start((uv_pipe_t*)handle, alloc_cb, read_cb);
 97    default:
 98      assert(0);
 99      return -1;
100  }
101}
102
103
104int uv_read_stop(uv_stream_t* handle) {
105  if (!(handle->flags & UV_HANDLE_READING))
106    return 0;
107
108  if (handle->type == UV_TTY) {
109    return uv_tty_read_stop((uv_tty_t*) handle);
110  } else {
111    handle->flags &= ~UV_HANDLE_READING;
112    DECREASE_ACTIVE_COUNT(handle->loop, handle);
113    return 0;
114  }
115}
116
117
118int uv_write(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt,
119    uv_write_cb cb) {
120  uv_loop_t* loop = handle->loop;
121
122  if (!(handle->flags & UV_HANDLE_WRITABLE)) {
123    uv__set_artificial_error(loop, UV_EPIPE);
124    return -1;
125  }
126
127  switch (handle->type) {
128    case UV_TCP:
129      return uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, bufcnt, cb);
130    case UV_NAMED_PIPE:
131      return uv_pipe_write(loop, req, (uv_pipe_t*) handle, bufs, bufcnt, cb);
132    case UV_TTY:
133      return uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, bufcnt, cb);
134    default:
135      assert(0);
136      uv__set_sys_error(loop, WSAEINVAL);
137      return -1;
138  }
139}
140
141
142int uv_write2(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt,
143    uv_stream_t* send_handle, uv_write_cb cb) {
144  uv_loop_t* loop = handle->loop;
145
146  if (!(handle->flags & UV_HANDLE_WRITABLE)) {
147    uv__set_artificial_error(loop, UV_EPIPE);
148    return -1;
149  }
150
151  switch (handle->type) {
152    case UV_NAMED_PIPE:
153      return uv_pipe_write2(loop, req, (uv_pipe_t*) handle, bufs, bufcnt, send_handle, cb);
154    default:
155      assert(0);
156      uv__set_sys_error(loop, WSAEINVAL);
157      return -1;
158  }
159}
160
161
162int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
163  uv_loop_t* loop = handle->loop;
164
165 if (!(handle->flags & UV_HANDLE_WRITABLE)) {
166    uv__set_artificial_error(loop, UV_EPIPE);
167    return -1;
168  }
169
170  if (!(handle->flags & UV_HANDLE_WRITABLE)) {
171    uv__set_artificial_error(loop, UV_EPIPE);
172    return -1;
173  }
174
175  uv_req_init(loop, (uv_req_t*) req);
176  req->type = UV_SHUTDOWN;
177  req->handle = handle;
178  req->cb = cb;
179
180  handle->flags &= ~UV_HANDLE_WRITABLE;
181  handle->shutdown_req = req;
182  handle->reqs_pending++;
183  REGISTER_HANDLE_REQ(loop, handle, req);
184
185  uv_want_endgame(loop, (uv_handle_t*)handle);
186
187  return 0;
188}
189
190
191int uv_is_readable(const uv_stream_t* handle) {
192  return !!(handle->flags & UV_HANDLE_READABLE);
193}
194
195
196int uv_is_writable(const uv_stream_t* handle) {
197  return !!(handle->flags & UV_HANDLE_WRITABLE);
198}