module Sound.OpenSoundControl.Transport.File (File, open) where
import Sound.OpenSoundControl.Transport (Transport(..))
import Sound.OpenSoundControl.Byte (encode_u32)
import Sound.OpenSoundControl.OSC (encodeOSC, OSC(Message))
import Control.Monad (liftM)
import qualified Data.ByteString.Lazy as B
import System.IO (Handle, openBinaryFile, hClose, IOMode(WriteMode))
data File = File Handle
deriving (Eq, Show)
instance Transport File where
send (File fd) msg =
let b = encodeOSC msg
n = fromIntegral (B.length b)
in B.hPut fd (B.append (encode_u32 n) b)
recv _ = return (Message "/done" [])
close (File fd) = hClose fd
open :: FilePath -> IO File
open fileName =
liftM File $ openBinaryFile fileName WriteMode