about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-10-18 10:39:18 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-10-18 10:39:18 -0700
commitc83f3bca47b1538ce469fc4463d6165156735675 (patch)
tree2575be098fb10f8373750f7905a5c06ab3c46c79
parent9c78e58e686e23aecfe1b229fc5abc8e0b4f8e3b (diff)
downloadmu-c83f3bca47b1538ce469fc4463d6165156735675.tar.gz
3519 - reading lots of data from a socket
In the process I've also altered the API of $read-from-socket to return
a boolean (eof?) rather than the number of bytes read (which is implicit
in the length of the returned array).
-rw-r--r--091socket.cc13
-rw-r--r--092socket.mu9
2 files changed, 17 insertions, 5 deletions
diff --git a/091socket.cc b/091socket.cc
index 23cdfb75..54d63ae1 100644
--- a/091socket.cc
+++ b/091socket.cc
@@ -141,6 +141,14 @@ case _READ_FROM_SOCKET: {
     raise << maybe(get(Recipe, r).name) << "'$read-from-socket' requires exactly two product, but got '" << inst.original_string << "'\n" << end();
     break;
   }
+  if (!is_mu_text(inst.products.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "first product of '$read-from-socket' should be a text (address array character), but got '" << to_string(inst.products.at(0)) << "'\n" << end();
+    break;
+  }
+  if (!is_mu_boolean(inst.products.at(1))) {
+    raise << maybe(get(Recipe, r).name) << "second product of '$read-from-socket' should be a boolean (eof?), but got '" << to_string(inst.products.at(1)) << "'\n" << end();
+    break;
+  }
   break;
 }
 :(before "End Primitive Recipe Implementations")
@@ -151,11 +159,12 @@ case _READ_FROM_SOCKET: {
   int socket_fd = socket->fd;
   char contents[bytes];
   bzero(contents, bytes);
-  int bytes_read = read(socket_fd, contents, bytes - 1 /* null-terminated */);
+  int bytes_read = read(socket_fd, contents, bytes-/*terminal null*/1);
 //?   cerr << "Read:\n" << string(contents) << "\n";
+//?   cerr << "bytes read: " << bytes_read << '\n';
   products.resize(2);
   products.at(0).push_back(new_mu_text(contents));
-  products.at(1).push_back(bytes_read);
+  products.at(1).push_back(bytes_read < bytes-1);
   break;
 }
 
diff --git a/092socket.mu b/092socket.mu
index 43e98f22..fed6d927 100644
--- a/092socket.mu
+++ b/092socket.mu
@@ -120,17 +120,20 @@ def receive-from-socket session:num, sink:&:sink:char -> sink:&:sink:char [
   local-scope
   load-ingredients
   {
-    req:text, bytes-read:num <- $read-from-socket session, 4096/bytes
-    $print [read ], bytes-read, [ bytes from socket], 10/newline
+    req:text, eof?:bool <- $read-from-socket session, 4096/bytes
+    bytes-read:num <- length *req
+#?     $print [read ], bytes-read, [ bytes from socket], 10/newline
     i:num <- copy 0
     {
+#?       $print [  write ], i, 10/newline
       done?:bool <- greater-or-equal i, bytes-read
       break-if done?
-      c:char <- index *req, i
+      c:char <- index *req, i  # todo: unicode
       sink <- write sink, c
       i <- add i, 1
       loop
     }
+    loop-unless eof?
   }
   sink <- close sink
 ]