Merge pull request #4 from boqs/master

Make osc-examples use usocket for portability
This commit is contained in:
nik gaffney 2017-12-10 13:42:02 +01:00 committed by GitHub
commit 1f6340aef2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,7 @@
;; -*- mode: lisp -*- ;; -*- mode: lisp -*-
;; ;;
;; Examples of how to send OSC messages. .. ;; Examples of how to send OSC messages. ..
;; ;;
;; Copyright (C) 2004 FoAM vzw ;; Copyright (C) 2004 FoAM vzw
;; ;;
;; Authors ;; Authors
@ -17,71 +17,76 @@
;; work with trivial-sockets, acl-compat or something similar. They should be ;; work with trivial-sockets, acl-compat or something similar. They should be
;; able to explain enough to get you started. .. ;; able to explain enough to get you started. ..
;; ;;
;; eg. listen on port 6667 for incoming msgs ;; eg. listen on port 6667 for incoming msgs
;; ;;
;; (osc-listen 6667) ;; (osc-receive-test 6667)
;; eg. send a test message to localhost port 6668
;;
;; (osc-send-test #(127 0 0 1) 6668)
;; ;;
;; eg. listen on port 6667 and send to 10.0.89:6668 ;; eg. listen on port 6667 and send to 10.0.89:6668
;; note the ip# is formatted as a vector ;; note the ip# is formatted as a vector
;; ;;
;; (osc-reflector 6667 #(10 0 0 89) 6668) ;; (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)
(defpackage osc-examples (:use :cl :osc :usocket)))
(in-package :osc-examples)
(defun osc-receive-test (port)
(defun osc-listen (port) "a basic test function which attempts to decode an osc message on given port.
"a basic test function which attempts to decode an osc message a given port." note ip#s need to be in the format #(127 0 0 1) for now.. ."
(let ((s (make-udp-socket)) (let ((s (socket-connect nil nil
(buffer (make-sequence '(vector (unsigned-byte 8)) 1024))) :local-port port
(socket-bind s #(0 0 0 0) port) :local-host #(127 0 0 1)
:protocol :datagram
:element-type '(unsigned-byte 8)))
(buffer (make-sequence '(vector (unsigned-byte 8)) 1024)))
(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-send-test (host port)
"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
:protocol :datagram
:element-type '(unsigned-byte 8)))
(b (osc:encode-message "/foo/bar" "baz" 1 2 3 (coerce PI 'single-float))))
(format t "sending to ~a on port ~A~%~%" host port)
(unwind-protect
(socket-send s b (length b))
(when s (socket-close s)))))
(defun osc-reflector (listen-port send-ip send-port) (defun osc-reflector-test (listen-port send-host send-port)
"reflector.. . listens on a given port and sends out on another "reflector.. . listens on a given port and sends out on another
note ip#s need to be in the format #(127 0 0 1) for now.. ." note ip#s need to be in the format #(127 0 0 1) for now.. ."
(let ((in (make-udp-socket)) (let ((in (socket-connect nil nil
(out (make-udp-socket)) :local-port listen-port
(buffer (make-sequence '(vector (unsigned-byte 8)) 512))) :local-host #(127 0 0 1)
(socket-bind in #(0 0 0 0) listen-port) :protocol :datagram
(socket-connect out send-ip send-port) :element-type '(unsigned-byte 8)))
(let ((stream (out (socket-connect send-host send-port
(socket-make-stream :protocol :datagram
out :input t :output t :element-type '(unsigned-byte 8)))
:element-type '(unsigned-byte 8) :buffering :full))) (buffer (make-sequence '(vector (unsigned-byte 8)) 1024)))
(unwind-protect (unwind-protect
(loop do (loop do
(socket-receive in buffer nil) (socket-receive in buffer (length buffer))
(let ((oscuff (osc:decode-bundle buffer))) (format t "glonked -=> message: ~{~A, ~}~%"
(format t "glonked -=> message with ~S~% arg(s)" (length oscuff)) (osc:decode-bundle buffer))
(write-stream-t1 stream oscuff))) (let ((mess (apply #'osc:encode-message
(when in (socket-close in)) (cons "/echo"
(when out (socket-close out)))))) (osc:decode-message buffer)))))
(socket-send out mess (length mess))))
(when in (socket-close in))
(defun make-udp-socket() (when out (socket-close out)))))
(make-instance 'inet-socket :type :datagram :protocol :udp))
(defun write-stream-t1 (stream osc-message)
"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 ;end