-
-
Save jgreco/0992a96da09ddb80d7b8582be869f66b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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