diff --git a/src/tcp_stream.carp b/src/tcp_stream.carp index 3978570..9f760b5 100644 --- a/src/tcp_stream.carp +++ b/src/tcp_stream.carp @@ -42,11 +42,11 @@ and accepted server connections. "TcpStream_send_MINUS_len_") (private read-) (hidden read-) - (register read- (Fn [&TcpStream] String) "TcpStream_read_") + (register read- (Fn [&TcpStream &Int] String) "TcpStream_read_") (private read-bytes-) (hidden read-bytes-) (register read-bytes- - (Fn [&TcpStream] (Array Byte)) + (Fn [&TcpStream &Int] (Array Byte)) "TcpStream_read_MINUS_bytes_") (private read-append-) (hidden read-append-) @@ -82,10 +82,18 @@ and accepted server connections. (doc read "reads up to 4096 bytes from the stream. Returns the data as a string, or an error. Returns an empty string on connection close.") - (defn read [stream] (let [s (read- stream)] (Result.Success s))) - - (doc read-bytes "reads up to 4096 bytes as a byte array.") - (defn read-bytes [stream] (Result.Success (read-bytes- stream))) + (defn read [stream] + (let [status 0 + data (read- stream &status)] + (if (< status 0) (Result.Error (System.error-text)) (Result.Success data)))) + + (doc read-bytes + "reads up to 4096 bytes as a byte array. Returns an empty array on +connection close, or an error if the read failed.") + (defn read-bytes [stream] + (let [status 0 + data (read-bytes- stream &status)] + (if (< status 0) (Result.Error (System.error-text)) (Result.Success data)))) (doc read-append "reads from the stream and appends to an existing byte buffer. diff --git a/src/tcp_stream.h b/src/tcp_stream.h index 427d0b1..5d57028 100644 --- a/src/tcp_stream.h +++ b/src/tcp_stream.h @@ -55,23 +55,31 @@ int TcpStream_send_MINUS_len_(TcpStream* s, String* msg, int len) { return (int)send_all(s->fd, *msg, (size_t)len); } -String TcpStream_read_(TcpStream* s) { +String TcpStream_read_(TcpStream* s, int *status) { String buf = CARP_MALLOC(SOCK_BUF_SIZE + 1); ssize_t r = read(s->fd, buf, SOCK_BUF_SIZE); - if (r < 0) { - buf[0] = '\0'; + if (r > 0) { + buf[r] = '\0'; + *status = (int)r; return buf; } - buf[r] = '\0'; + buf[0] = '\0'; + *status = r == 0 ? 0 : -1; return buf; } -Array TcpStream_read_MINUS_bytes_(TcpStream* s) { +Array TcpStream_read_MINUS_bytes_(TcpStream* s, int *status) { Array buf; buf.capacity = SOCK_BUF_SIZE; buf.data = CARP_MALLOC(SOCK_BUF_SIZE); ssize_t r = read(s->fd, buf.data, SOCK_BUF_SIZE); - buf.len = r < 0 ? 0 : (int)r; + if (r > 0) { + buf.len = (int)r; + *status = (int)r; + return buf; + } + buf.len = 0; + *status = r == 0 ? 0 : -1; return buf; } diff --git a/src/unix_stream.carp b/src/unix_stream.carp index fcd948f..d616929 100644 --- a/src/unix_stream.carp +++ b/src/unix_stream.carp @@ -36,11 +36,11 @@ "UnixStream_send_MINUS_bytes_") (private read-) (hidden read-) - (register read- (Fn [&UnixStream] String) "UnixStream_read_") + (register read- (Fn [&UnixStream &Int] String) "UnixStream_read_") (private read-bytes-) (hidden read-bytes-) (register read-bytes- - (Fn [&UnixStream] (Array Byte)) + (Fn [&UnixStream &Int] (Array Byte)) "UnixStream_read_MINUS_bytes_") (private read-append-) (hidden read-append-) @@ -83,10 +83,18 @@ Returns bytes sent or an error.") (doc read "reads up to 4096 bytes from the stream. Returns the data as a string, or an error. Returns an empty string on connection close.") - (defn read [stream] (let [s (read- stream)] (Result.Success s))) - - (doc read-bytes "reads up to 4096 bytes as a byte array.") - (defn read-bytes [stream] (Result.Success (read-bytes- stream))) + (defn read [stream] + (let [status 0 + data (read- stream &status)] + (if (< status 0) (Result.Error (System.error-text)) (Result.Success data)))) + + (doc read-bytes + "reads up to 4096 bytes as a byte array. Returns an empty array on +connection close, or an error if the read failed.") + (defn read-bytes [stream] + (let [status 0 + data (read-bytes- stream &status)] + (if (< status 0) (Result.Error (System.error-text)) (Result.Success data)))) (doc read-append "reads from the stream and appends to an existing byte buffer. diff --git a/src/unix_stream.h b/src/unix_stream.h index d70f947..c6b2416 100644 --- a/src/unix_stream.h +++ b/src/unix_stream.h @@ -39,23 +39,31 @@ int UnixStream_send_MINUS_bytes_(UnixStream* s, Array* data) { return (int)send_all(s->fd, (const char*)data->data, data->len); } -String UnixStream_read_(UnixStream* s) { +String UnixStream_read_(UnixStream* s, int *status) { String buf = CARP_MALLOC(SOCK_BUF_SIZE + 1); ssize_t r = read(s->fd, buf, SOCK_BUF_SIZE); - if (r < 0) { - buf[0] = '\0'; + if (r > 0) { + buf[r] = '\0'; + *status = (int)r; return buf; } - buf[r] = '\0'; + buf[0] = '\0'; + *status = r == 0 ? 0 : -1; return buf; } -Array UnixStream_read_MINUS_bytes_(UnixStream* s) { +Array UnixStream_read_MINUS_bytes_(UnixStream* s, int *status) { Array buf; buf.capacity = SOCK_BUF_SIZE; buf.data = CARP_MALLOC(SOCK_BUF_SIZE); ssize_t r = read(s->fd, buf.data, SOCK_BUF_SIZE); - buf.len = r < 0 ? 0 : (int)r; + if (r > 0) { + buf.len = (int)r; + *status = (int)r; + return buf; + } + buf.len = 0; + *status = r == 0 ? 0 : -1; return buf; }