0 | module IO.Async.Posix
2 | import public IO.Async
3 | import public IO.Async.Loop.PollH
4 | import public IO.Async.Loop.TimerH
5 | import public System.Posix.File
6 | import public System.Posix.Poll.Types
7 | import public System.Posix.Time
10 | import System.Posix.Dir
14 | parameters {auto has : Has Errno es}
16 | {auto fd : FileDesc f}
26 | poll : (ev : PollEvent) -> Async e es PollEvent
29 | primAsync $
\cb => primPoll st (cast fd) ev False $
\case
30 | Right x => cb (Right x)
31 | Left x => cb (Left $
inject x)
38 | onEvent : PollEvent -> Async e es a -> Async e es a
41 | case hasEvent evt ev of
43 | False => throw EINVAL
55 | readnb : (0 r : Type) -> FromBuf r => Bits32 -> Async e es r
57 | attempt (read {es = [Errno]} fd r n) >>= \case
59 | if x == EAGAIN || x == EWOULDBLOCK
60 | then onEvent POLLIN (read fd r n)
62 | Right res => pure res
71 | readRawNb : Buf -> Async e es EMBuffer
73 | attempt (readRaw {es = [Errno]} fd buf) >>= \case
75 | if x == EAGAIN || x == EWOULDBLOCK
76 | then onEvent POLLIN (readRaw fd buf)
78 | Right res => pure res
90 | readPtrNB : (0 r : Type) -> FromPtr r => CPtr -> Async e es r
92 | attempt (readPtr {es = [Errno]} fd r p) >>= \case
94 | if x == EAGAIN || x == EWOULDBLOCK
95 | then onEvent POLLIN (readPtr fd r p)
97 | Right res => pure res
110 | writenb : ToBuf r => r -> Async e es Bits32
112 | attempt (write {es = [Errno]} fd v) >>= \case
114 | if x == EAGAIN || x == EWOULDBLOCK
115 | then onEvent POLLOUT (write fd v)
117 | Right res => pure res
127 | fwritenb : ToBuf r => r -> Async e es ()
129 | case (unsafeToBuf v) of
130 | Left (CP sz p) => goPtr p sz
134 | goPtr : AnyPtr -> Bits32 -> Async e es ()
135 | goPtr p 0 = pure ()
137 | m <- writenb (CP sz p)
138 | goPtr (prim__inc_ptr p m 1) (assert_smaller sz $
sz - m)
140 | go : ByteString -> Async e es ()
141 | go (BS 0 _) = pure ()
144 | go (assert_smaller bs $
drop (cast m) bs)
151 | -> {auto frp : FromBuf r}
153 | -> (r -> Async e es ())
156 | onEvent POLLIN (read fd Buf buf) >>= \case
159 | v <- runIO (fromBuf b)
174 | -> {auto frp : FromPtr r}
176 | -> (r -> Async e es ())
179 | onEvent POLLIN (readPtr fd CPtr cp) >>= \case
182 | v <- runIO (fromPtr cp2)
189 | sleepTill : TimerH e => (Tm -> Clock UTC) -> Async e es ()
191 | now <- liftIO (clockTime UTC)
192 | sleep (timeDifference (fun $
fromUTC now) now)
196 | nextMinute : Tm -> Clock UTC
197 | nextMinute x = addDuration (toUTC ({sec := 0} x)) 60.s
201 | nextHour : Tm -> Clock UTC
202 | nextHour tm = addDuration (toUTC ({sec := 0, min := 0} tm)) 1.h
206 | nextDay : Tm -> Clock UTC
207 | nextDay tm = addDuration (toUTC ({sec := 0, min := 0, hour := 0} tm)) 1.d
211 | nextYear : Tm -> Clock UTC
212 | nextYear tm = toUTC $
{year := tm.year + 1} blank
216 | nextMonth : Tm -> Clock UTC
220 | else toUTC $
{mon := tm.mon + 1, year := tm.year} blank
224 | nextWeek : Tm -> Clock UTC
226 | addDuration (toUTC ({sec := 0, min := 0, hour := 0} tm)) (cast $
7 - tm.wday).d