module Sound.OSC.Transport.FD.UDP where
import Control.Monad
import qualified Network.Socket as N
import qualified Network.Socket.ByteString as C
import Sound.OSC.Coding.Class
import Sound.OSC.Packet
import Sound.OSC.Packet.Class
import Sound.OSC.Transport.FD
data UDP = UDP {udpSocket :: N.Socket}
udpPort :: Integral n => UDP -> IO n
udpPort (UDP fd) = fmap fromIntegral (N.socketPort fd)
instance Transport UDP where
sendOSC (UDP fd) msg = void (C.send fd (encodeOSC msg))
recvPacket (UDP fd) = liftM decodePacket (C.recv fd 8192)
close (UDP fd) = N.close fd
udp_socket :: (N.Socket -> N.SockAddr -> IO ()) -> String -> Int -> IO UDP
udp_socket f host port = do
fd <- N.socket N.AF_INET N.Datagram 0
a <- N.inet_addr host
let sa = N.SockAddrInet (fromIntegral port) a
f fd sa
return (UDP fd)
set_udp_opt :: N.SocketOption -> Int -> UDP -> IO ()
set_udp_opt k v (UDP s) = N.setSocketOption s k v
get_udp_opt :: N.SocketOption -> UDP -> IO Int
get_udp_opt k (UDP s) = N.getSocketOption s k
openUDP :: String -> Int -> IO UDP
openUDP = udp_socket N.connect
udpServer :: String -> Int -> IO UDP
udpServer = udp_socket N.bind
sendTo :: OSC o => UDP -> o -> N.SockAddr -> IO ()
sendTo (UDP fd) o a = do
void (C.sendTo fd (encodeOSC o) a)
recvFrom :: UDP -> IO (Packet, N.SockAddr)
recvFrom (UDP fd) = do
(s,a) <- C.recvFrom fd 8192
return (decodePacket s,a)