about summary refs log tree commit diff stats
path: root/server-socket.mu
diff options
context:
space:
mode:
authorStephen Malina <stephenmalina@gmail.com>2016-09-11 16:26:44 -0400
committerStephen Malina <stephenmalina@gmail.com>2016-09-11 17:16:47 -0400
commit68578a7828ce8300fa10b28b5f57e56723303e93 (patch)
tree6d8683701f44ccabada39644c8054c07b9790249 /server-socket.mu
parentfa20bd3143ffb637e438b44dcb10b56d9abf9888 (diff)
downloadmu-68578a7828ce8300fa10b28b5f57e56723303e93.tar.gz
3323 - Add simple network primitives
Includes four Mu functions:
- $socket: Creates the C structure for a socket and tries to bind and
  listen on a user-provided port.
- $accept: Returns a number pointer to a new socket session. Should
    be called with the result of $socket.
- $read-from-socket: Read one character from the socket, passed in
    as a Mu number. Should only be called after calling $socket and
    $accept.
- $close-socket: Takes two parameters, one for the result of $socket
    and one for the result of $accept, closing both sockets
    and releasing bound ports.
Diffstat (limited to 'server-socket.mu')
-rw-r--r--server-socket.mu14
1 files changed, 14 insertions, 0 deletions
diff --git a/server-socket.mu b/server-socket.mu
new file mode 100644
index 00000000..8a9b27ef
--- /dev/null
+++ b/server-socket.mu
@@ -0,0 +1,14 @@
+def main [
+  local-scope
+  socket:number <- $socket 8080/port
+  $print [Mu socket creation returned ], socket, 10/newline
+  session:number <- $accept socket
+  {
+    client-message:address:buffer <- new-buffer 1024
+    c:character <- $read-from-socket session
+    break-unless c
+    $print c
+    loop
+  }
+  $close-socket socket, session
+]