module Sound.OSC.Transport.FD where
import Control.Exception
import Data.List
import Data.Maybe
import Sound.OSC.Datum
import Sound.OSC.Packet
import qualified Sound.OSC.Wait as Wait
class Transport t where
sendPacket :: t -> Packet -> IO ()
recvPacket :: t -> IO Packet
close :: t -> IO ()
withTransport :: Transport t => IO t -> (t -> IO a) -> IO a
withTransport u = bracket u close
sendMessage :: Transport t => t -> Message -> IO ()
sendMessage t = sendPacket t . Packet_Message
sendBundle :: Transport t => t -> Bundle -> IO ()
sendBundle t = sendPacket t . Packet_Bundle
recvBundle :: (Transport t) => t -> IO Bundle
recvBundle = fmap packet_to_bundle . recvPacket
recvMessage :: (Transport t) => t -> IO (Maybe Message)
recvMessage = fmap packet_to_message . recvPacket
recvMessages :: (Transport t) => t -> IO [Message]
recvMessages = fmap packetMessages . recvPacket
recvPacketTimeout :: (Transport t) => Double -> t -> IO (Maybe Packet)
recvPacketTimeout n fd = Wait.timeout_r n (recvPacket fd)
waitUntil :: (Transport t) => t -> (Packet -> Bool) -> IO Packet
waitUntil t f = Wait.untilPredicate f (recvPacket t)
waitFor :: (Transport t) => t -> (Packet -> Maybe a) -> IO a
waitFor t f = Wait.untilMaybe f (recvPacket t)
waitImmediate :: Transport t => t -> IO Packet
waitImmediate t = waitUntil t packet_is_immediate
waitMessage :: Transport t => t -> IO Message
waitMessage t = waitFor t packet_to_message
waitAddress :: Transport t => t -> Address_Pattern -> IO Packet
waitAddress t s =
let f o = if packet_has_address s o then Just o else Nothing
in waitFor t f
waitReply :: Transport t => t -> Address_Pattern -> IO Message
waitReply t s =
let f = fromMaybe (error "waitReply: message not located?") .
find (message_has_address s) .
packetMessages
in fmap f (waitAddress t s)
waitDatum :: Transport t => t -> Address_Pattern -> IO [Datum]
waitDatum t = fmap messageDatum . waitReply t