Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/tcp_stream.carp
Original file line number Diff line number Diff line change
Expand Up @@ -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-)
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 14 additions & 6 deletions src/tcp_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
20 changes: 14 additions & 6 deletions src/unix_stream.carp
Original file line number Diff line number Diff line change
Expand Up @@ -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-)
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 14 additions & 6 deletions src/unix_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down