2014-02-15 19:03:37 +00:00
|
|
|
#lang racket
|
|
|
|
|
2022-08-24 11:15:13 +00:00
|
|
|
(require "bytevector.rkt"
|
|
|
|
"encoding.rkt"
|
2014-02-15 19:03:37 +00:00
|
|
|
"ip.rkt")
|
|
|
|
|
2022-08-24 11:15:13 +00:00
|
|
|
(provide send
|
|
|
|
recv
|
|
|
|
wait)
|
2014-02-15 19:03:37 +00:00
|
|
|
|
|
|
|
;; socket -> osc -> ()
|
|
|
|
(define send
|
|
|
|
(lambda (fd m)
|
|
|
|
(let ((b (encode-osc m)))
|
|
|
|
(cond ((udp:socket? fd)
|
|
|
|
(udp:send fd b))
|
|
|
|
((tcp:socket? fd)
|
|
|
|
(tcp:send fd (encode-u32 (bytevector-length b)))
|
|
|
|
(tcp:send fd b))))))
|
|
|
|
|
|
|
|
;; port -> maybe osc
|
|
|
|
(define recv
|
|
|
|
(lambda (fd)
|
|
|
|
(cond ((udp:socket? fd)
|
|
|
|
(let ((b (udp:recv fd)))
|
|
|
|
(and b (decode-osc b))))
|
|
|
|
((tcp:socket? fd)
|
|
|
|
(let* ((b (tcp:read fd 4))
|
|
|
|
(n (decode-u32 b)))
|
|
|
|
(decode-osc (tcp:read fd n)))))))
|
|
|
|
|
|
|
|
;; port -> string -> osc
|
|
|
|
(define wait
|
|
|
|
(lambda (fd s)
|
|
|
|
(let ((p (recv fd)))
|
|
|
|
(cond
|
2014-05-27 23:38:19 +00:00
|
|
|
((not p) (error "error" "Could not connect to the SuperCollider server"))
|
2014-02-15 23:48:21 +00:00
|
|
|
((not (string=? (car p) s)) (error "wait" "bad return packet" p s))
|
2014-02-15 19:03:37 +00:00
|
|
|
(else p)))))
|