module Sound.OpenSoundControl.Transport.File (T, open, ) where
import qualified Sound.OpenSoundControl.Type as OSC
import Sound.OpenSoundControl.Coding.Byte (encode_u32, )
import Sound.OpenSoundControl.Class (encodeOSC, )
import Sound.OSC.Transport.FD (Transport(..), )
import qualified Data.ByteString.Lazy as B
import System.IO (Handle, openBinaryFile, hClose, IOMode(WriteMode), )
import Control.Monad (liftM, )
data T = Cons OSC.Packet Handle
deriving (Eq, Show)
instance Transport T where
sendOSC (Cons _ h) msg =
let b = encodeOSC msg
n = fromIntegral (B.length b)
in B.hPut h (B.append (encode_u32 n) b)
recvPacket (Cons msg _) = return msg
close (Cons _ h) = hClose h
open :: OSC.Packet -> FilePath -> IO T
open msg fileName =
liftM (Cons msg) $ openBinaryFile fileName WriteMode