module Sound.OpenSoundControl.Transport.Monad
   (Transport, send, recv, wait, close, withTransport, IO) where

import qualified Sound.OpenSoundControl.Transport as Trans
import Sound.OpenSoundControl.Transport (Transport)
import Sound.OpenSoundControl.OSC (OSC)

import Control.Monad.Reader (ReaderT(ReaderT, runReaderT))

import Prelude hiding (IO)
import qualified System.IO as Sys


type IO t a = ReaderT t Sys.IO a


-- | Encode and send an OSC packet over a UDP connection.
send :: Transport t => OSC -> IO t ()
send = ReaderT . flip Trans.send

-- | Receive and decode an OSC packet over a UDP connection.
recv :: Transport t => IO t OSC
recv = ReaderT Trans.recv

-- | Wait for an OSC message with the specified address, discard intervening messages.
wait :: Transport t => String -> IO t OSC
wait = ReaderT . flip Trans.wait

{-
Like wait but ignores the returned message.
Thus it can be used for non-realtime mode.

simpleWait :: Transport t => String -> IO t ()
simpleWait = ReaderT . flip Trans.simpleWait
-}

-- | Close a UDP connection.
close :: Transport t => IO t ()
close = ReaderT Trans.close

-- | Bracket UDP activity.
withTransport :: Transport t => Sys.IO t -> IO t a -> Sys.IO a
withTransport u = Trans.withTransport u . runReaderT