From 203563b3465fe5f25f97e475b1a4dbe957845030 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 23 Sep 2016 23:07:58 -0700 Subject: 3412 --- html/server-socket.mu.html | 53 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-) (limited to 'html/server-socket.mu.html') diff --git a/html/server-socket.mu.html b/html/server-socket.mu.html index 0095f59a..5bb09e1f 100644 --- a/html/server-socket.mu.html +++ b/html/server-socket.mu.html @@ -15,6 +15,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color * { font-size: 12pt; font-size: 1em; } .muRecipe { color: #ff8700; } .Delimiter { color: #800080; } +.Comment { color: #9090ff; } .Constant { color: #00a0a0; } .Special { color: #c00000; } .muControl { color: #c0a020; } @@ -29,22 +30,36 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color
+# Example of reading from a socket using channels and writing back to it
+# directly. Running this file and navigating to <address of server>:8080
+# should result in your browser displaying "SUCCESS!".
+#
+# Unfortunately, the reading implementation has some redundant, inelegant
+# code to make up for my lack of insight into Linux's socket internals.
 def main [
   local-scope
   socket:num <- $socket 8080/port
   $print [Mu socket creation returned ], socket, 10/newline
   session:num <- $accept socket
-  write-to-socket session, [HTTP/1.0 200 OK
-
-OK]
+  contents:&:source:char, sink:&:sink:char <- new-channel 30
+  sink <- start-running transmit-from-socket session, sink
+  buf:&:buffer <- new-buffer 30
   {
-    c:char, eof?:boolean <- $read-from-socket session
-    $print c
-    break-if eof?
+    c:char, done?:bool, contents <- read contents
+    break-if done?
+    buf <- append buf, c
     loop
   }
-  $print 10/newline, [Hit end of socket, closing...], 10/newline
-  $close-socket socket, session
+  socket-text:text <- buffer-to-array buf
+  $print [Done reading from socket.], 10/newline
+  write-to-socket session, [HTTP/1.0 200 OK
+Content-type: text/plain
+
+SUCCESS!
+]
+  $print 10/newline, [Wrote to and closing socket...], 10/newline
+  $close-socket session
+  $close-socket socket
 ]
 
 def write-to-socket session-socket:number, s:address:array:character [
@@ -56,11 +71,33 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color
     done?:boolean <- greater-or-equal i, len
     break-if done?
     c:character <- index *s, i
+    $print [writing to socket: ], i, [ ], c, 10/newline
     $write-to-socket session-socket, c
     i <- add i, 1
     loop
   }
 ]
+
+def transmit-from-socket session:num, sink:&:sink:char -> sink:&:sink:char [
+  local-scope
+  load-ingredients
+  {
+    req:text, bytes-read:number <- $read-from-socket session, 4096/bytes
+    $print [read ], bytes-read, [ bytes from socket], 10/newline
+    i:number <- copy 0
+    {
+      done?:boolean <- greater-or-equal i, bytes-read
+      break-if done?
+      c:char <- index *req, i
+      end-of-request?:bool <- equal c, 10/newline
+      break-if end-of-request? # To be safe, for now.
+      sink <- write sink, c
+      i <- add i, 1
+      loop
+    }
+  }
+  sink <- close sink
+]
 
-- cgit 1.4.1-2-gfad0