From 768b5246398ccc273a98f27ff0b3c3617df69eb0 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 14 Jan 2013 16:53:35 +0100 Subject: [PATCH] unix: make stream.c more DRY --- src/unix/stream.c | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/src/unix/stream.c b/src/unix/stream.c index afd48c1d..f4ed0028 100644 --- a/src/unix/stream.c +++ b/src/unix/stream.c @@ -926,6 +926,14 @@ static void uv__read(uv_stream_t* stream) { while (nread < 0 && errno == EINTR); } +#define INVOKE_READ_CB(stream, status, buf, type) \ + do { \ + if ((stream)->read_cb != NULL) \ + (stream)->read_cb((stream), (status), (buf)); \ + else \ + (stream)->read2_cb((uv_pipe_t*) (stream), (status), (buf), (type)); \ + } \ + while (0) if (nread < 0) { /* Error */ @@ -935,42 +943,22 @@ static void uv__read(uv_stream_t* stream) { uv__io_start(stream->loop, &stream->io_watcher, UV__POLLIN); } uv__set_sys_error(stream->loop, EAGAIN); - - if (stream->read_cb) { - stream->read_cb(stream, 0, buf); - } else { - stream->read2_cb((uv_pipe_t*)stream, 0, buf, UV_UNKNOWN_HANDLE); - } - - return; + INVOKE_READ_CB(stream, 0, buf, UV_UNKNOWN_HANDLE); } else { /* Error. User should call uv_close(). */ uv__set_sys_error(stream->loop, errno); - - if (stream->read_cb) { - stream->read_cb(stream, -1, buf); - } else { - stream->read2_cb((uv_pipe_t*)stream, -1, buf, UV_UNKNOWN_HANDLE); - } - + INVOKE_READ_CB(stream, -1, buf, UV_UNKNOWN_HANDLE); assert(!uv__io_active(&stream->io_watcher, UV__POLLIN) && "stream->read_cb(status=-1) did not call uv_close()"); - return; } - + return; } else if (nread == 0) { /* EOF */ - uv__set_artificial_error(stream->loop, UV_EOF); uv__io_stop(stream->loop, &stream->io_watcher, UV__POLLIN); - if (!uv__io_active(&stream->io_watcher, UV__POLLOUT)) uv__handle_stop(stream); - - if (stream->read_cb) { - stream->read_cb(stream, -1, buf); - } else { - stream->read2_cb((uv_pipe_t*)stream, -1, buf, UV_UNKNOWN_HANDLE); - } + uv__set_artificial_error(stream->loop, UV_EOF); + INVOKE_READ_CB(stream, -1, buf, UV_UNKNOWN_HANDLE); return; } else { /* Successful read */