use usocket in osc-examples for portability

This commit is contained in:
rick 2017-11-30 15:01:08 -08:00 committed by nik gaffney
parent 829bf7cfc1
commit 52040ea82a
Signed by: nik
GPG key ID: 989F5E6EDB478160

View file

@ -14,75 +14,78 @@
;; Commentry ;; Commentry
;; ;;
;; These examples are currently sbcl specific, but should be easily ported to ;; These examples are currently sbcl specific, but should be easily ported to
;; work with trivial-sockets, acl-compat or something similar. They should be ;; work with trivial-sockets, acl-compat or something similar.
;; able to explain enough to get you started. .. ;; They should be enough to get you started.
;; ;;
;; eg. listen on port 6667 for incoming messages ;; eg. listen on port 6667 for incoming messages
;; ;;
;; (osc-listen 6667) ;; (osc-receive-test 6667)
;; ;;
;; eg. listen on port 6667 and send to 10.0.89:6668 ;; send a test message to localhost port 6668
;; note the ip# is formatted as a vector
;; ;;
;; (osc-reflector 6667 #(10 0 0 89) 6668) ;; (osc-send-test "localhost" 6668)
;;
;; listen on port 6667 and send to 10.0.89:6668
;; (note the ip# is formatted as a vector)
;;
;; (osc-reflector-test 6667 #(10 0 0 89) 6668)
;; ;;
;;;;;:::;;: ; ; ;::: ; ;; ;; ; ;; ; ;;;;;:::;;: ; ; ;::: ; ;; ;; ; ;; ;
(require :sb-bsd-sockets) (eval-when (:compile-toplevel :load-toplevel)
(use-package :osc) (ql:quickload :osc)
(use-package :sb-bsd-sockets) (ql:quickload :usocket)
(use-package :osc)
(use-package :usocket))
(defun osc-listen (port) (defun osc-listen-test (port)
"a basic test function which attempts to decode an osc message a given port." "a basic test function which attempts to decode an osc message a given port."
(let ((s (make-udp-socket)) (let ((s (socket-connect nil nil
:local-port port
:protocol :datagram
:element-type '(unsigned-byte 8)))
(buffer (make-sequence '(vector (unsigned-byte 8)) 1024))) (buffer (make-sequence '(vector (unsigned-byte 8)) 1024)))
(socket-bind s #(0 0 0 0) port)
(format t "listening on localhost port ~A~%~%" port) (format t "listening on localhost port ~A~%~%" port)
(unwind-protect (unwind-protect
(loop do (loop do
(socket-receive s buffer nil) (socket-receive s buffer (length buffer))
(format t "receiveded -=> ~S~%" (osc:decode-bundle buffer))) (format t "received -=> ~S~%" (osc:decode-bundle buffer)))
(when s (socket-close s))))) (when s (socket-close s)))))
(defun osc-reflector (listen-port send-ip send-port) (defun osc-send-test (host port)
"reflector.. . listens on a given port and sends out on another "a basic test function which sends osc test message to a given port/hostname."
note ip#s need to be in the format #(127 0 0 1) for now.. ." (let ((s (socket-connect host port
(let ((in (make-udp-socket)) :protocol :datagram
(out (make-udp-socket)) :element-type '(unsigned-byte 8)))
(buffer (make-sequence '(vector (unsigned-byte 8)) 512))) (b (osc:encode-message "/foo/bar" "baz" 1 2 3 (coerce PI 'single-float))))
(socket-bind in #(0 0 0 0) listen-port) (format t "listening on localhost port ~A~%~%" port)
(socket-connect out send-ip send-port) (unwind-protect
(let ((stream (socket-send s b (length b))
(socket-make-stream (when s (socket-close s)))))
out :input t :output t
:element-type '(unsigned-byte 8) :buffering :full)))
(unwind-protect
(loop do
(socket-receive in buffer nil)
(let ((oscuff (osc:decode-bundle buffer)))
(format t "glonked -=> message with ~S~% arg(s)"
(length oscuff))
(write-stream-t1 stream oscuff)))
(when in (socket-close in))
(when out (socket-close out))))))
(defun make-udp-socket() (defun osc-reflector-test (listen-port send-host send-port)
(make-instance 'inet-socket :type :datagram :protocol :udp)) "reflector. listens on a given port and sends out on another."
(let ((in (socket-connect nil nil
:local-port listen-port
:protocol :datagram
:element-type '(unsigned-byte 8)))
(out (socket-connect send-host send-port
:protocol :datagram
:element-type '(unsigned-byte 8)))
(buffer (make-sequence '(vector (unsigned-byte 8)) 1024)))
(unwind-protect
(loop do
(socket-receive in buffer (length buffer))
(format t "glonked -=> message: ~{~A, ~}~%"
(osc:decode-bundle buffer))
(let ((mess (apply #'osc:encode-message
(cons "/echo"
(osc:decode-message buffer)))))
(socket-send out mess (length mess))))
(when in (socket-close in))
(when out (socket-close out)))))
(defun write-stream-t1 (stream osc-message) ;;end
"writes a given message to a stream. keep in mind that when using a
buffered stream any funtion writing to the stream should
call (finish-output stream) after it sends the mesages,. ."
(write-sequence
(osc:encode-message "/bzzp" "got" "it" )
stream)
(finish-output stream))
(defmacro osc-write-to-stream (stream &body args)
`(progn (write-sequence (osc:encode-message ,@args) ,stream)
(finish-output ,stream)))
;end