libuv/src/win/stream.c

285 lines
7.1 KiB
C
Raw Normal View History

2011-07-19 01:47:30 +00:00
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <assert.h>
#include "uv.h"
#include "internal.h"
#include "handle-inl.h"
#include "req-inl.h"
2011-07-19 01:47:30 +00:00
int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) {
int err;
if (uv__is_closing(stream)) {
return UV_EINVAL;
}
err = ERROR_INVALID_PARAMETER;
switch (stream->type) {
case UV_TCP:
err = uv__tcp_listen((uv_tcp_t*)stream, backlog, cb);
break;
case UV_NAMED_PIPE:
err = uv__pipe_listen((uv_pipe_t*)stream, backlog, cb);
break;
default:
assert(0);
}
return uv_translate_sys_error(err);
}
int uv_accept(uv_stream_t* server, uv_stream_t* client) {
int err;
err = ERROR_INVALID_PARAMETER;
switch (server->type) {
case UV_TCP:
err = uv__tcp_accept((uv_tcp_t*)server, (uv_tcp_t*)client);
break;
case UV_NAMED_PIPE:
err = uv__pipe_accept((uv_pipe_t*)server, client);
break;
default:
assert(0);
2011-07-19 01:47:30 +00:00
}
return uv_translate_sys_error(err);
2011-07-19 01:47:30 +00:00
}
int uv__read_start(uv_stream_t* handle,
uv_alloc_cb alloc_cb,
uv_read_cb read_cb) {
int err;
err = ERROR_INVALID_PARAMETER;
switch (handle->type) {
case UV_TCP:
err = uv__tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
break;
case UV_NAMED_PIPE:
err = uv__pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
break;
case UV_TTY:
err = uv__tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
break;
2011-09-30 00:58:58 +00:00
default:
assert(0);
}
return uv_translate_sys_error(err);
2011-09-30 00:58:58 +00:00
}
2011-07-19 01:47:30 +00:00
int uv_read_stop(uv_stream_t* handle) {
int err;
if (!(handle->flags & UV_HANDLE_READING))
return 0;
err = 0;
2011-09-28 01:41:59 +00:00
if (handle->type == UV_TTY) {
err = uv__tty_read_stop((uv_tty_t*) handle);
win,pipe: refactor pipe read cancellation logic * Don't silently ignore ERROR_OPERATION_ABORTED. Code to silently ignore this error was added in c42a4ca, under the false premise that this error is somehow equivalent to EINTR on posix platforms. This isn't true; ERROR_OPERATION_ABORTED doesn't happen unless the application explicitly aborts an I/O operation. Silently ignoring this error elsewhere could potentially hide bugs, hence libuv shouldn't do it. Instead, explicitly deal with it where it is expected. * Don't mark aborted reads as successful. The worker thread used to call ReadFile() on synchronous pipes would incorrectly mark cancelled read requests as successful, leading to issues later on. * Rely on main thread to restart aborted reads. After a blocking ReadFile() call was cancelled, the worker thread would previously attempt to restart it immediately, making synchronization logic needlessly complex. Instead, we simply cancel the operation, and leave it to the main loop to restart it if so desired. Since we now realy on the main thread to restart interrupted ReadFile() calls, we can now have a single function interrupts a synchronous read until the event loop restarts it again. * Clean up uv__pipe_read_stop(). A single function to interrupt blocking reads also allows us to remove weird logic in uv__pipe_read_stop() that quickly pauses and unpauses a read operation in the hope of cancelling it. * Assume CancelIo() and CancelSynchronousIo() are always available. Since libuv doesn't support windows XP and Server 2003 any more, we can assume that these APIs are always available, and do away with branching around them. PR-URL: https://github.com/libuv/libuv/pull/1843 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
2018-05-23 23:07:28 +00:00
} else if (handle->type == UV_NAMED_PIPE) {
uv__pipe_read_stop((uv_pipe_t*) handle);
} else {
win,pipe: refactor pipe read cancellation logic * Don't silently ignore ERROR_OPERATION_ABORTED. Code to silently ignore this error was added in c42a4ca, under the false premise that this error is somehow equivalent to EINTR on posix platforms. This isn't true; ERROR_OPERATION_ABORTED doesn't happen unless the application explicitly aborts an I/O operation. Silently ignoring this error elsewhere could potentially hide bugs, hence libuv shouldn't do it. Instead, explicitly deal with it where it is expected. * Don't mark aborted reads as successful. The worker thread used to call ReadFile() on synchronous pipes would incorrectly mark cancelled read requests as successful, leading to issues later on. * Rely on main thread to restart aborted reads. After a blocking ReadFile() call was cancelled, the worker thread would previously attempt to restart it immediately, making synchronization logic needlessly complex. Instead, we simply cancel the operation, and leave it to the main loop to restart it if so desired. Since we now realy on the main thread to restart interrupted ReadFile() calls, we can now have a single function interrupts a synchronous read until the event loop restarts it again. * Clean up uv__pipe_read_stop(). A single function to interrupt blocking reads also allows us to remove weird logic in uv__pipe_read_stop() that quickly pauses and unpauses a read operation in the hope of cancelling it. * Assume CancelIo() and CancelSynchronousIo() are always available. Since libuv doesn't support windows XP and Server 2003 any more, we can assume that these APIs are always available, and do away with branching around them. PR-URL: https://github.com/libuv/libuv/pull/1843 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
2018-05-23 23:07:28 +00:00
handle->flags &= ~UV_HANDLE_READING;
DECREASE_ACTIVE_COUNT(handle->loop, handle);
}
return uv_translate_sys_error(err);
2011-07-19 01:47:30 +00:00
}
static int uv__check_before_write(uv_stream_t* handle,
const uv_buf_t bufs[],
unsigned int nbufs) {
/* We're not beholden to IOV_MAX but limit the buffer count to catch sign
* conversion bugs where a caller passes in a signed negative number that
* then gets converted to a really large unsigned number.
*/
if (nbufs < 1 || nbufs > 1024*1024) {
return UV_EINVAL;
}
if (uv__count_bufs(bufs, nbufs) > UV__IO_MAX_BYTES) {
return UV_EINVAL;
}
if (!(handle->flags & UV_HANDLE_WRITABLE)) {
return UV_EPIPE;
}
return 0;
}
int uv_write(uv_write_t* req,
uv_stream_t* handle,
const uv_buf_t bufs[],
unsigned int nbufs,
uv_write_cb cb) {
2011-08-31 02:19:16 +00:00
uv_loop_t* loop = handle->loop;
int err;
2011-08-31 02:19:16 +00:00
err = uv__check_before_write(handle, bufs, nbufs);
if (err != 0) {
return err;
}
err = ERROR_INVALID_PARAMETER;
switch (handle->type) {
case UV_TCP:
err = uv__tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb);
break;
case UV_NAMED_PIPE:
err = uv__pipe_write(
loop, req, (uv_pipe_t*) handle, bufs, nbufs, NULL, cb);
return uv_translate_write_sys_error(err);
case UV_TTY:
err = uv__tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb);
break;
default:
assert(0);
2011-07-19 01:47:30 +00:00
}
return uv_translate_sys_error(err);
2011-07-19 01:47:30 +00:00
}
int uv_write2(uv_write_t* req,
uv_stream_t* handle,
const uv_buf_t bufs[],
unsigned int nbufs,
uv_stream_t* send_handle,
uv_write_cb cb) {
2011-09-30 00:58:58 +00:00
uv_loop_t* loop = handle->loop;
int err;
2011-09-30 00:58:58 +00:00
if (send_handle == NULL) {
return uv_write(req, handle, bufs, nbufs, cb);
}
err = uv__check_before_write(handle, bufs, nbufs);
if (err != 0) {
return err;
}
if (handle->type != UV_NAMED_PIPE || !((uv_pipe_t*) handle)->ipc) {
return UV_EINVAL;
2011-09-30 00:58:58 +00:00
}
err = uv__pipe_write(
loop, req, (uv_pipe_t*) handle, bufs, nbufs, send_handle, cb);
return uv_translate_write_sys_error(err);
2011-09-30 00:58:58 +00:00
}
int uv_try_write(uv_stream_t* stream,
const uv_buf_t bufs[],
unsigned int nbufs) {
int err;
err = uv__check_before_write(stream, bufs, nbufs);
if (err != 0) {
return err;
}
if (stream->flags & UV_HANDLE_CLOSING)
return UV_EBADF;
switch (stream->type) {
case UV_TCP:
return uv__tcp_try_write((uv_tcp_t*) stream, bufs, nbufs);
case UV_TTY:
return uv__tty_try_write((uv_tty_t*) stream, bufs, nbufs);
case UV_NAMED_PIPE:
return UV_EAGAIN;
default:
assert(0);
return UV_ENOSYS;
}
}
int uv_try_write2(uv_stream_t* stream,
const uv_buf_t bufs[],
unsigned int nbufs,
uv_stream_t* send_handle) {
if (send_handle != NULL)
return UV_EAGAIN;
return uv_try_write(stream, bufs, nbufs);
}
2011-07-19 01:47:30 +00:00
int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
2011-08-31 02:19:16 +00:00
uv_loop_t* loop = handle->loop;
if (!(handle->flags & UV_HANDLE_WRITABLE) ||
uv__is_stream_shutting(handle) ||
uv__is_closing(handle)) {
return UV_ENOTCONN;
2011-07-19 01:47:30 +00:00
}
UV_REQ_INIT(req, UV_SHUTDOWN);
2011-07-19 01:47:30 +00:00
req->handle = handle;
req->cb = cb;
handle->flags &= ~UV_HANDLE_WRITABLE;
handle->stream.conn.shutdown_req = req;
2011-07-19 01:47:30 +00:00
handle->reqs_pending++;
REGISTER_HANDLE_REQ(loop, handle);
2011-07-19 01:47:30 +00:00
if (handle->stream.conn.write_reqs_pending == 0) {
if (handle->type == UV_NAMED_PIPE)
uv__pipe_shutdown(loop, (uv_pipe_t*) handle, req);
else
uv__insert_pending_req(loop, (uv_req_t*) req);
}
2011-07-19 01:47:30 +00:00
return 0;
}
int uv_is_readable(const uv_stream_t* handle) {
return !!(handle->flags & UV_HANDLE_READABLE);
2012-02-08 20:01:33 +00:00
}
int uv_is_writable(const uv_stream_t* handle) {
return !!(handle->flags & UV_HANDLE_WRITABLE);
2012-02-08 20:01:33 +00:00
}
int uv_stream_set_blocking(uv_stream_t* handle, int blocking) {
2014-02-26 08:25:49 +00:00
if (handle->type != UV_NAMED_PIPE)
return UV_EINVAL;
if (blocking != 0)
handle->flags |= UV_HANDLE_BLOCKING_WRITES;
else
handle->flags &= ~UV_HANDLE_BLOCKING_WRITES;
return 0;
}