Skip to content

Instantly share code, notes, and snippets.

@jgreco
Created September 22, 2019 19:50
Show Gist options
  • Save jgreco/0992a96da09ddb80d7b8582be869f66b to your computer and use it in GitHub Desktop.
Save jgreco/0992a96da09ddb80d7b8582be869f66b to your computer and use it in GitHub Desktop.
(define (connect-to-server #:port [port 9393] #:hostname [hostname "127.0.0.1"])
(define-values (conn-in conn-out) (tcp-connect hostname port))
(define request-bindings (make-hash))
(thread (thunk
(let loop ()
(define receive-evt (thread-receive-evt))
(match (sync conn-in receive-evt)
[(? (curry eq? receive-evt) th)
(match-define (job-request sexp return-channel) (thread-receive))
(define id (random 4294967087))
(write `(with-id ,id ,sexp) conn-out)
(hash-set! request-bindings id return-channel)
(flush-output conn-out)]
[(? input-port? in)
(define res (read in))
(match-define `(,id ,status ,result) res)
(async-channel-put (hash-ref request-bindings id)
`(,status ,result))
(hash-remove! request-bindings id)])
(loop)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment