/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. #include <assert.h>
  22. #include "uv.h"
  23. #include "internal.h"
  24. #include "handle-inl.h"
  25. #include "req-inl.h"
  26. int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) {
  27. switch (stream->type) {
  28. case UV_TCP:
  29. return uv_tcp_listen((uv_tcp_t*)stream, backlog, cb);
  30. case UV_NAMED_PIPE:
  31. return uv_pipe_listen((uv_pipe_t*)stream, backlog, cb);
  32. default:
  33. assert(0);
  34. return -1;
  35. }
  36. }
  37. int uv_accept(uv_stream_t* server, uv_stream_t* client) {
  38. switch (server->type) {
  39. case UV_TCP:
  40. return uv_tcp_accept((uv_tcp_t*)server, (uv_tcp_t*)client);
  41. case UV_NAMED_PIPE:
  42. return uv_pipe_accept((uv_pipe_t*)server, client);
  43. default:
  44. assert(0);
  45. return -1;
  46. }
  47. }
  48. int uv_read_start(uv_stream_t* handle, uv_alloc_cb alloc_cb,
  49. uv_read_cb read_cb) {
  50. if (handle->flags & UV_HANDLE_READING) {
  51. uv__set_sys_error(handle->loop, UV_EALREADY);
  52. return -1;
  53. }
  54. if (!(handle->flags & UV_HANDLE_READABLE)) {
  55. uv__set_artificial_error(handle->loop, UV_ENOTCONN);
  56. return -1;
  57. }
  58. switch (handle->type) {
  59. case UV_TCP:
  60. return uv_tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
  61. case UV_NAMED_PIPE:
  62. return uv_pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
  63. case UV_TTY:
  64. return uv_tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
  65. default:
  66. assert(0);
  67. return -1;
  68. }
  69. }
  70. int uv_read2_start(uv_stream_t* handle, uv_alloc_cb alloc_cb,
  71. uv_read2_cb read_cb) {
  72. if (handle->flags & UV_HANDLE_READING) {
  73. uv__set_sys_error(handle->loop, UV_EALREADY);
  74. return -1;
  75. }
  76. if (!(handle->flags & UV_HANDLE_READABLE)) {
  77. uv__set_artificial_error(handle->loop, UV_ENOTCONN);
  78. return -1;
  79. }
  80. switch (handle->type) {
  81. case UV_NAMED_PIPE:
  82. return uv_pipe_read2_start((uv_pipe_t*)handle, alloc_cb, read_cb);
  83. default:
  84. assert(0);
  85. return -1;
  86. }
  87. }
  88. int uv_read_stop(uv_stream_t* handle) {
  89. if (!(handle->flags & UV_HANDLE_READING))
  90. return 0;
  91. if (handle->type == UV_TTY) {
  92. return uv_tty_read_stop((uv_tty_t*) handle);
  93. } else {
  94. handle->flags &= ~UV_HANDLE_READING;
  95. DECREASE_ACTIVE_COUNT(handle->loop, handle);
  96. return 0;
  97. }
  98. }
  99. int uv_write(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt,
  100. uv_write_cb cb) {
  101. uv_loop_t* loop = handle->loop;
  102. if (!(handle->flags & UV_HANDLE_WRITABLE)) {
  103. uv__set_artificial_error(loop, UV_EPIPE);
  104. return -1;
  105. }
  106. switch (handle->type) {
  107. case UV_TCP:
  108. return uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, bufcnt, cb);
  109. case UV_NAMED_PIPE:
  110. return uv_pipe_write(loop, req, (uv_pipe_t*) handle, bufs, bufcnt, cb);
  111. case UV_TTY:
  112. return uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, bufcnt, cb);
  113. default:
  114. assert(0);
  115. uv__set_sys_error(loop, WSAEINVAL);
  116. return -1;
  117. }
  118. }
  119. int uv_write2(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt,
  120. uv_stream_t* send_handle, uv_write_cb cb) {
  121. uv_loop_t* loop = handle->loop;
  122. if (!(handle->flags & UV_HANDLE_WRITABLE)) {
  123. uv__set_artificial_error(loop, UV_EPIPE);
  124. return -1;
  125. }
  126. switch (handle->type) {
  127. case UV_NAMED_PIPE:
  128. return uv_pipe_write2(loop, req, (uv_pipe_t*) handle, bufs, bufcnt, send_handle, cb);
  129. default:
  130. assert(0);
  131. uv__set_sys_error(loop, WSAEINVAL);
  132. return -1;
  133. }
  134. }
  135. int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
  136. uv_loop_t* loop = handle->loop;
  137. if (!(handle->flags & UV_HANDLE_WRITABLE)) {
  138. uv__set_artificial_error(loop, UV_EPIPE);
  139. return -1;
  140. }
  141. if (!(handle->flags & UV_HANDLE_WRITABLE)) {
  142. uv__set_artificial_error(loop, UV_EPIPE);
  143. return -1;
  144. }
  145. uv_req_init(loop, (uv_req_t*) req);
  146. req->type = UV_SHUTDOWN;
  147. req->handle = handle;
  148. req->cb = cb;
  149. handle->flags &= ~UV_HANDLE_WRITABLE;
  150. handle->shutdown_req = req;
  151. handle->reqs_pending++;
  152. REGISTER_HANDLE_REQ(loop, handle, req);
  153. uv_want_endgame(loop, (uv_handle_t*)handle);
  154. return 0;
  155. }
  156. int uv_is_readable(const uv_stream_t* handle) {
  157. return !!(handle->flags & UV_HANDLE_READABLE);
  158. }
  159. int uv_is_writable(const uv_stream_t* handle) {
  160. return !!(handle->flags & UV_HANDLE_WRITABLE);
  161. }