Debian Codegraph @

Sound/Osc/Transport/Fd.hs

raw symbols SCIP
1 -- | An abstract transport layer with implementations for @Udp@ and @Tcp@ transport.
2 module Sound.Osc.Transport.Fd where
3
4 import Control.Exception {- base -}
5 import Data.List {- base -}
6 import Data.Maybe {- base -}
7
8 import Sound.Osc.Datum {- hosc -}
9 import Sound.Osc.Packet {- hosc -}
10 import qualified Sound.Osc.Wait as Wait {- hosc -}
11
12 -- | Abstract over the underlying transport protocol.
13 class Transport t where
14 -- | Encode and send an Osc packet.
15 sendPacket :: t -> PacketOf Message -> IO ()
16
17 -- | Receive and decode an Osc packet.
18 recvPacket :: t -> IO (PacketOf Message)
19
20 -- | Receive and either decode an Osc packet.
21 recvPacketOr :: t -> IO (Either String Packet)
22
23 -- | Close an existing connection.
24 close :: t -> IO ()
25
26 -- | Bracket Osc communication.
27 withTransport :: Transport t => IO t -> (t -> IO a) -> IO a
28 withTransport u = bracket u close
29
30 -- * Send
31
32 -- | 'sendPacket' of 'Packet_Message'.
33 sendMessage :: Transport t => t -> Message -> IO ()
34 sendMessage t = sendPacket t . Packet_Message
35
36 -- | 'sendPacket' of 'Packet_Bundle'.
37 sendBundle :: Transport t => t -> BundleOf Message -> IO ()
38 sendBundle t = sendPacket t . Packet_Bundle
39
40 -- * Receive
41
42 -- | Variant of 'recvPacket' that runs 'packet_to_bundle'.
43 recvBundle :: (Transport t) => t -> IO (BundleOf Message)
44 recvBundle = fmap packet_to_bundle . recvPacket
45
46 -- | Variant of 'recvPacket' that runs 'packet_to_message'.
47 recvMessage :: (Transport t) => t -> IO (Maybe Message)
48 recvMessage = fmap packet_to_message . recvPacket
49
50 -- | Erroring variant.
51 recvMessage_err :: (Transport t) => t -> IO Message
52 recvMessage_err = fmap (fromMaybe (error "recvMessage")) . recvMessage
53
54 -- | Variant of 'recvPacket' that runs 'packetMessages'.
55 recvMessages :: (Transport t) => t -> IO [Message]
56 recvMessages = fmap packetMessages . recvPacket
57
58 -- * Wait
59
60 {- | Wait for a 'Packet' where the supplied predicate is 'True',
61 discarding intervening packets.
62 -}
63 waitUntil :: (Transport t) => t -> (PacketOf Message -> Bool) -> IO (PacketOf Message)
64 waitUntil t f = Wait.untilPredicate f (recvPacket t)
65
66 {- | Wait for a 'Packet' where the supplied function does not give
67 'Nothing', discarding intervening packets.
68 -}
69 waitFor :: (Transport t) => t -> (PacketOf Message -> Maybe a) -> IO a
70 waitFor t f = Wait.untilMaybe f (recvPacket t)
71
72 -- | 'waitUntil' 'packet_is_immediate'.
73 waitImmediate :: Transport t => t -> IO (PacketOf Message)
74 waitImmediate t = waitUntil t packet_is_immediate
75
76 {- | 'waitFor' 'packet_to_message', ie. an incoming 'Message' or
77 immediate mode 'Bundle' with one element.
78 -}
79 waitMessage :: Transport t => t -> IO Message
80 waitMessage t = waitFor t packet_to_message
81
82 {- | A 'waitFor' for variant using 'packet_has_address' to match on
83 the 'Address_Pattern' of incoming 'Packets'.
84 -}
85 waitAddress :: Transport t => t -> Address_Pattern -> IO (PacketOf Message)
86 waitAddress t s =
87 let f o = if packet_has_address s o then Just o else Nothing
88 in waitFor t f
89
90 -- | Variant on 'waitAddress' that returns matching 'Message'.
91 waitReply :: Transport t => t -> Address_Pattern -> IO Message
92 waitReply t s =
93 let f =
94 fromMaybe (error "waitReply: message not located?")
95 . find (message_has_address s)
96 . packetMessages
97 in fmap f (waitAddress t s)
98
99 -- | Variant of 'waitReply' that runs 'messageDatum'.
100 waitDatum :: Transport t => t -> Address_Pattern -> IO [Datum]
101 waitDatum t = fmap messageDatum . waitReply t