From 9fc621c829a58f5fc3db6916fd7739b365de40f4 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 6 May 2018 11:03:39 +0100 Subject: [PATCH] Add missing assertion for outbound connections. --- syndicate/drivers/tcp.rkt | 1 + syndicate/examples/chat-client.rkt | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 syndicate/examples/chat-client.rkt diff --git a/syndicate/drivers/tcp.rkt b/syndicate/drivers/tcp.rkt index 9cf65d0..b022095 100644 --- a/syndicate/drivers/tcp.rkt +++ b/syndicate/drivers/tcp.rkt @@ -70,6 +70,7 @@ (values (open-input-string "") o))]) (tcp:tcp-connect host port))) + (assert (tcp-accepted id)) (define unblock! (run-connection id cin cout)) (unblock!)) diff --git a/syndicate/examples/chat-client.rkt b/syndicate/examples/chat-client.rkt new file mode 100644 index 0000000..371476c --- /dev/null +++ b/syndicate/examples/chat-client.rkt @@ -0,0 +1,21 @@ +#lang imperative-syndicate + +(require/activate imperative-syndicate/drivers/tcp) +(require/activate imperative-syndicate/drivers/external-event) +(require (only-in racket/port read-bytes-line-evt)) + +(spawn (define id 'chat) + + (assert (tcp-connection id (tcp-address "localhost" 5999))) + (on (asserted (tcp-accepted id)) (printf "*** Connected.\n")) + (stop-when (retracted (tcp-accepted id)) (printf "*** Remote EOF. Terminating.\n")) + + (on (message (tcp-in id $bs)) + (write-bytes bs) + (flush-output)) + + (define stdin-evt (read-bytes-line-evt (current-input-port) 'any)) + (on (message (inbound (external-event stdin-evt (list $line)))) + (if (eof-object? line) + (stop-current-facet (printf "*** Local EOF. Terminating.\n")) + (send! (tcp-out id (bytes-append line #"\n"))))))