Skip to content

Commit

Permalink
unix: make stream.c more DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Jan 14, 2013
1 parent c931e31 commit 768b524
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions src/unix/stream.c
Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down

0 comments on commit 768b524

Please sign in to comment.