4 | import Data.Buffer.Indexed
7 | data Act = ActSend (List Bits8) | ActReceive
11 | show (ActSend _) = "ActSend <data not shown>"
12 | show ActReceive = "ActReceive"
15 | data Response = RSent | RReceived (List Bits8)
19 | show RSent = "RSent"
20 | show (RReceived dat) = "RReceived " <+> show (utf8Decode (bufferL dat))
23 | collectSends : DIterator Response Act i2 o2 r -> (List Bits8, DIterator Response Act i2 o2 r)
24 | collectSends r@(Result _) = ([], r)
25 | collectSends r@(Susp2 _ _) = ([], r)
26 | collectSends r@(Susp ActReceive _) = ([], r)
27 | collectSends r@(Susp (ActSend firstSend) k) =
29 | (laterSends, newIter) = collectSends (k RSent)
31 | (firstSend <+> laterSends, newIter)
34 | sendMuchM : Monad m => (List Bits8 -> m Unit) -> DIterator Response Act i2 o2 r -> m (DIterator Response Act i2 o2 r)
35 | sendMuchM sender it = do
36 | let (sends, newIter) = collectSends it
39 | nonEmpty => sender nonEmpty
42 | yieldUntilRecv: List Response -> Cont (DIterator Response Act altOut altIn a) (Pair (List Bits8) (List Response))
43 | yieldUntilRecv (RReceived bytes :: xs) = pure $
MkPair bytes xs
44 | yieldUntilRecv (RSent :: xs) =
46 | yieldUntilRecv [] = do
47 | resp <- yieldGet ActReceive
48 | yieldUntilRecv [resp]
51 | mkSendRecvIterator : DIterator (List Bits8) (List Bits8) altOut altIn a
52 | -> DIterator Response Act altOut altIn a
53 | mkSendRecvIterator initialState =
54 | toIterator (loop initialState [])
56 | loop: DIterator (List Bits8) (List Bits8) altOut altIn a -> List Response -> Cont (DIterator Response Act altOut altIn a) a
57 | loop (Result a) resps = pure a
58 | loop (Susp2 sql cont) resps = do
59 | dataRows <- yieldGet2 sql
60 | loop (cont dataRows) resps
61 | loop (Susp toSend cont) resps =
64 | MkPair bytes xs <- yieldUntilRecv resps
65 | loop (cont $
toList bytes) xs
67 | newResp <- yieldGet (ActSend nonEmpty)
68 | loop (cont []) (newResp :: resps)