Debian Codegraph @

Sound/Osc/Transport/Fd/Udp.hs

raw symbols SCIP
1 -- | Osc over Udp implementation.
2 module Sound.Osc.Transport.Fd.Udp where
3
4 import Control.Exception {- base -}
5 import Control.Monad {- base -}
6 import Data.Bifunctor {- base -}
7
8 import qualified Data.ByteString as B {- bytestring -}
9 import qualified Data.ByteString.Lazy as BL {- bytestring -}
10 import qualified Network.Socket as N {- network -}
11 import qualified Network.Socket.ByteString as C {- network -}
12
13 import qualified Sound.Osc.Coding.Decode.Binary as Binary {- hosc -}
14 import qualified Sound.Osc.Coding.Encode.Builder as Builder {- hosc -}
15 import qualified Sound.Osc.Packet as Packet {- hosc -}
16 import qualified Sound.Osc.Transport.Fd as Fd {- hosc -}
17
18 -- | The Udp transport handle data type.
19 newtype Udp = Udp {udpSocket :: N.Socket}
20
21 -- | Return the port number associated with the Udp socket.
22 udpPort :: Integral n => Udp -> IO n
23 udpPort = fmap fromIntegral . N.socketPort . udpSocket
24
25 -- | Send data over Udp using 'C.send'.
26 udp_send_data :: Udp -> B.ByteString -> IO ()
27 udp_send_data (Udp fd) d = do
28 let l = B.length d
29 n <- C.send fd d
30 when (n /= l) (error (show ("udp_send_data", l, n)))
31
32 -- | Send data over Udp using 'C.sendAll'.
33 udp_sendAll_data :: Udp -> B.ByteString -> IO ()
34 udp_sendAll_data (Udp fd) = C.sendAll fd
35
36 -- | Send packet over Udp.
37 udp_send_packet :: Udp -> Packet.PacketOf Packet.Message -> IO ()
38 udp_send_packet udp = udp_sendAll_data udp . Builder.encodePacket_strict
39
40 -- | Receive packet over Udp.
41 udp_recv_packet :: Udp -> IO (Packet.PacketOf Packet.Message)
42 udp_recv_packet (Udp fd) = fmap Binary.decodePacket_strict (C.recv fd 8192)
43
44 udp_recv_packet_or :: Udp -> IO (Either String Packet.Packet)
45 udp_recv_packet_or (Udp fd) = Binary.decodePacketOr . BL.fromStrict <$> C.recv fd 8192
46
47 -- | Close Udp.
48 udp_close :: Udp -> IO ()
49 udp_close (Udp fd) = N.close fd
50
51 -- | 'Udp' is an instance of 'Fd.Transport'.
52 instance Fd.Transport Udp where
53 sendPacket = udp_send_packet
54 recvPacket = udp_recv_packet
55 recvPacketOr = udp_recv_packet_or
56 close = udp_close
57
58 -- | Bracket Udp communication.
59 with_udp :: IO Udp -> (Udp -> IO t) -> IO t
60 with_udp u = bracket u udp_close
61
62 -- | Create and initialise Udp socket.
63 udp_socket :: (N.Socket -> N.SockAddr -> IO ()) -> String -> Int -> IO Udp
64 udp_socket f host port = do
65 fd <- N.socket N.AF_INET N.Datagram 0
66 let hints = N.defaultHints {N.addrFamily = N.AF_INET} -- localhost=ipv4
67 i : _ <- N.getAddrInfo (Just hints) (Just host) (Just (show port))
68 let sa = N.addrAddress i
69 f fd sa
70 return (Udp fd)
71
72 -- | Set option, ie. 'N.Broadcast' or 'N.RecvTimeOut'.
73 set_udp_opt :: N.SocketOption -> Int -> Udp -> IO ()
74 set_udp_opt k v (Udp s) = N.setSocketOption s k v
75
76 -- | Get option.
77 get_udp_opt :: N.SocketOption -> Udp -> IO Int
78 get_udp_opt k (Udp s) = N.getSocketOption s k
79
80 -- | Make a 'Udp' connection.
81 openUdp :: String -> Int -> IO Udp
82 openUdp = udp_socket N.connect
83
84 {- | Trivial 'Udp' server socket.
85
86 > import Control.Concurrent
87
88 > let u0 = udpServer "127.0.0.1" 57300
89 > t0 <- forkIO (Fd.withTransport u0 (\fd -> forever (Fd.recvMessage fd >>= print >> print "Received message, continuing")))
90 > killThread t0
91
92 > let u1 = openUdp "127.0.0.1" 57300
93 > Fd.withTransport u1 (\fd -> Fd.sendMessage fd (Packet.message "/n" []))
94 -}
95 udpServer :: String -> Int -> IO Udp
96 udpServer = udp_socket N.bind
97
98 -- | Variant of 'udpServer' that doesn't require the host address.
99 udp_server :: Int -> IO Udp
100 udp_server p = do
101 let hints =
102 N.defaultHints
103 { N.addrFamily = N.AF_INET -- localhost=ipv4
104 , N.addrFlags = [N.AI_PASSIVE, N.AI_NUMERICSERV]
105 , N.addrSocketType = N.Datagram
106 }
107 a : _ <- N.getAddrInfo (Just hints) Nothing (Just (show p))
108 s <- N.socket (N.addrFamily a) (N.addrSocketType a) (N.addrProtocol a)
109 N.setSocketOption s N.ReuseAddr 1
110 N.bind s (N.addrAddress a)
111 return (Udp s)
112
113 -- | Send to specified address using 'C.sendAllTo.
114 sendTo :: Udp -> Packet.PacketOf Packet.Message -> N.SockAddr -> IO ()
115 sendTo (Udp fd) p = C.sendAllTo fd (Builder.encodePacket_strict p)
116
117 -- | Recv variant to collect message source address.
118 recvFrom :: Udp -> IO (Packet.PacketOf Packet.Message, N.SockAddr)
119 recvFrom (Udp fd) = fmap (first Binary.decodePacket_strict) (C.recvFrom fd 8192)