module Sound.OpenSoundControl.Transport (Transport(..)
,withTransport
,recvT
,waitFor,wait) where
import Control.Exception
import Sound.OpenSoundControl.Type
import System.Timeout
class Transport t where
send :: t -> OSC -> IO ()
recv :: t -> IO OSC
close :: t -> IO ()
has_address :: String -> OSC -> Bool
has_address x o =
case o of
Message y _ -> x == y
_ -> False
untilM :: Monad m => (a -> Maybe b) -> m a -> m b
untilM f act =
let g p = let q = f p in case q of { Nothing -> rec
; Just r -> return r }
rec = act >>= g
in rec
timeout_r :: Double -> IO a -> IO (Maybe a)
timeout_r t = timeout (floor (t * 1000000))
recvT :: Transport t => Double -> t -> IO (Maybe OSC)
recvT n fd = timeout_r n (recv fd)
waitFor :: Transport t => t -> (OSC -> Maybe a) -> IO a
waitFor t f = untilM f (recv t)
wait :: Transport t => t -> String -> IO OSC
wait t s = waitFor t (\o -> if has_address s o then Just o else Nothing)
withTransport :: Transport t => IO t -> (t -> IO a) -> IO a
withTransport u = bracket u close