-- | OSC over UDP implementation.
module Sound.OSC.Transport.FD.UDP where

import Control.Exception {- base -}
import Data.Bifunctor {- base -}

import qualified Network.Socket as N {- network -}
import qualified Network.Socket.ByteString as C {- network -}

import qualified Sound.OSC.Coding.Decode.Binary as Binary {- hosc -}
import qualified Sound.OSC.Coding.Encode.Builder as Builder {- hosc -}
import qualified Sound.OSC.Packet as Packet {- hosc -}
import qualified Sound.OSC.Transport.FD as FD {- hosc -}

-- | The UDP transport handle data type.
newtype UDP = UDP {UDP -> Socket
udpSocket :: N.Socket}

-- | Return the port number associated with the UDP socket.
udpPort :: Integral n => UDP -> IO n
udpPort :: UDP -> IO n
udpPort = (PortNumber -> n) -> IO PortNumber -> IO n
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PortNumber -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral (IO PortNumber -> IO n) -> (UDP -> IO PortNumber) -> UDP -> IO n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Socket -> IO PortNumber
N.socketPort (Socket -> IO PortNumber)
-> (UDP -> Socket) -> UDP -> IO PortNumber
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UDP -> Socket
udpSocket

-- | Send packet over UDP using 'C.sendAll'.
upd_send_packet :: UDP -> Packet.Packet -> IO ()
upd_send_packet :: UDP -> Packet -> IO ()
upd_send_packet (UDP Socket
fd) Packet
p = Socket -> ByteString -> IO ()
C.sendAll Socket
fd (Packet -> ByteString
Builder.encodePacket_strict Packet
p)

-- | Receive packet over UDP.
udp_recv_packet :: UDP -> IO Packet.Packet
udp_recv_packet :: UDP -> IO Packet
udp_recv_packet (UDP Socket
fd) = (ByteString -> Packet) -> IO ByteString -> IO Packet
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Packet
Binary.decodePacket_strict (Socket -> Int -> IO ByteString
C.recv Socket
fd Int
8192)

-- | Close UDP.
udp_close :: UDP -> IO ()
udp_close :: UDP -> IO ()
udp_close (UDP Socket
fd) = Socket -> IO ()
N.close Socket
fd

-- | 'UDP' is an instance of 'FD.Transport'.
instance FD.Transport UDP where
   sendPacket :: UDP -> Packet -> IO ()
sendPacket = UDP -> Packet -> IO ()
upd_send_packet
   recvPacket :: UDP -> IO Packet
recvPacket = UDP -> IO Packet
udp_recv_packet
   close :: UDP -> IO ()
close = UDP -> IO ()
udp_close

-- | Bracket UDP communication.
with_udp :: IO UDP -> (UDP -> IO t) -> IO t
with_udp :: IO UDP -> (UDP -> IO t) -> IO t
with_udp IO UDP
u = IO UDP -> (UDP -> IO ()) -> (UDP -> IO t) -> IO t
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO UDP
u UDP -> IO ()
udp_close

-- | Create and initialise UDP socket.
udp_socket :: (N.Socket -> N.SockAddr -> IO ()) -> String -> Int -> IO UDP
udp_socket :: (Socket -> SockAddr -> IO ()) -> String -> Int -> IO UDP
udp_socket Socket -> SockAddr -> IO ()
f String
host Int
port = do
  Socket
fd <- Family -> SocketType -> ProtocolNumber -> IO Socket
N.socket Family
N.AF_INET SocketType
N.Datagram ProtocolNumber
0
  let hints :: AddrInfo
hints = AddrInfo
N.defaultHints {addrFamily :: Family
N.addrFamily = Family
N.AF_INET} -- localhost=ipv4
  AddrInfo
i:[AddrInfo]
_ <- Maybe AddrInfo -> Maybe String -> Maybe String -> IO [AddrInfo]
N.getAddrInfo (AddrInfo -> Maybe AddrInfo
forall a. a -> Maybe a
Just AddrInfo
hints) (String -> Maybe String
forall a. a -> Maybe a
Just String
host) (String -> Maybe String
forall a. a -> Maybe a
Just (Int -> String
forall a. Show a => a -> String
show Int
port))
  let sa :: SockAddr
sa = AddrInfo -> SockAddr
N.addrAddress AddrInfo
i
  Socket -> SockAddr -> IO ()
f Socket
fd SockAddr
sa
  UDP -> IO UDP
forall (m :: * -> *) a. Monad m => a -> m a
return (Socket -> UDP
UDP Socket
fd)

-- | Set option, ie. 'N.Broadcast' or 'N.RecvTimeOut'.
set_udp_opt :: N.SocketOption -> Int -> UDP -> IO ()
set_udp_opt :: SocketOption -> Int -> UDP -> IO ()
set_udp_opt SocketOption
k Int
v (UDP Socket
s) = Socket -> SocketOption -> Int -> IO ()
N.setSocketOption Socket
s SocketOption
k Int
v

-- | Get option.
get_udp_opt :: N.SocketOption -> UDP -> IO Int
get_udp_opt :: SocketOption -> UDP -> IO Int
get_udp_opt SocketOption
k (UDP Socket
s) = Socket -> SocketOption -> IO Int
N.getSocketOption Socket
s SocketOption
k

-- | Make a 'UDP' connection.
openUDP :: String -> Int -> IO UDP
openUDP :: String -> Int -> IO UDP
openUDP = (Socket -> SockAddr -> IO ()) -> String -> Int -> IO UDP
udp_socket Socket -> SockAddr -> IO ()
N.connect

{- | Trivial 'UDP' server socket.

> import Control.Concurrent {- base -}

> let u0 = udpServer "127.0.0.1" 57300
> t0 <- forkIO (FD.withTransport u0 (\fd -> forever (FD.recvMessage fd >>= print)))

> let u1 = openUDP "127.0.0.1" 57300
> FD.withTransport u1 (\fd -> FD.sendMessage fd (Packet.message "/n" []))
-}
udpServer :: String -> Int -> IO UDP
udpServer :: String -> Int -> IO UDP
udpServer = (Socket -> SockAddr -> IO ()) -> String -> Int -> IO UDP
udp_socket Socket -> SockAddr -> IO ()
N.bind

-- | Variant of 'udpServer' that doesn't require the host address.
udp_server :: Int -> IO UDP
udp_server :: Int -> IO UDP
udp_server Int
p = do
  let hints :: AddrInfo
hints =
        AddrInfo
N.defaultHints
        {addrFamily :: Family
N.addrFamily = Family
N.AF_INET -- localhost=ipv4
        ,addrFlags :: [AddrInfoFlag]
N.addrFlags = [AddrInfoFlag
N.AI_PASSIVE,AddrInfoFlag
N.AI_NUMERICSERV]
        ,addrSocketType :: SocketType
N.addrSocketType = SocketType
N.Datagram}
  AddrInfo
a:[AddrInfo]
_ <- Maybe AddrInfo -> Maybe String -> Maybe String -> IO [AddrInfo]
N.getAddrInfo (AddrInfo -> Maybe AddrInfo
forall a. a -> Maybe a
Just AddrInfo
hints) Maybe String
forall a. Maybe a
Nothing (String -> Maybe String
forall a. a -> Maybe a
Just (Int -> String
forall a. Show a => a -> String
show Int
p))
  Socket
s <- Family -> SocketType -> ProtocolNumber -> IO Socket
N.socket (AddrInfo -> Family
N.addrFamily AddrInfo
a) (AddrInfo -> SocketType
N.addrSocketType AddrInfo
a) (AddrInfo -> ProtocolNumber
N.addrProtocol AddrInfo
a)
  Socket -> SocketOption -> Int -> IO ()
N.setSocketOption Socket
s SocketOption
N.ReuseAddr Int
1
  Socket -> SockAddr -> IO ()
N.bind Socket
s (AddrInfo -> SockAddr
N.addrAddress AddrInfo
a)
  UDP -> IO UDP
forall (m :: * -> *) a. Monad m => a -> m a
return (Socket -> UDP
UDP Socket
s)

-- | Send to specified address using 'C.sendAllTo.
sendTo :: UDP -> Packet.Packet -> N.SockAddr -> IO ()
sendTo :: UDP -> Packet -> SockAddr -> IO ()
sendTo (UDP Socket
fd) Packet
p = Socket -> ByteString -> SockAddr -> IO ()
C.sendAllTo Socket
fd (Packet -> ByteString
Builder.encodePacket_strict Packet
p)

-- | Recv variant to collect message source address.
recvFrom :: UDP -> IO (Packet.Packet, N.SockAddr)
recvFrom :: UDP -> IO (Packet, SockAddr)
recvFrom (UDP Socket
fd) = ((ByteString, SockAddr) -> (Packet, SockAddr))
-> IO (ByteString, SockAddr) -> IO (Packet, SockAddr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ByteString -> Packet)
-> (ByteString, SockAddr) -> (Packet, SockAddr)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ByteString -> Packet
Binary.decodePacket_strict) (Socket -> Int -> IO (ByteString, SockAddr)
C.recvFrom Socket
fd Int
8192)