1 (** Lwt support for Curl *)
7 all_events
: (Unix.file_descr
, Lwt_engine.event list
) Hashtbl.t
;
8 wakeners
: (Curl.t
, Curl.curlCode
Lwt.u
) Hashtbl.t
;
12 let mt = M.create () in
13 let timer_event = ref Lwt_engine.fake_event
in
14 let all_events = Hashtbl.create 32 in
15 let wakeners = Hashtbl.create 32 in
18 match M.remove_finished
mt with
22 let w = Hashtbl.find
wakeners h
in
23 Hashtbl.remove
wakeners h
;
26 prerr_endline
"curl_lwt: orphan handle, how come?"
32 let on_readable fd _
=
33 let (_
:int) = M.action
mt fd
M.EV_IN
in
34 finished "on_readable";
36 let on_writable fd _
=
37 let (_
:int) = M.action
mt fd
M.EV_OUT
in
38 finished "on_writable";
41 Lwt_engine.stop_event
!timer_event;
45 M.set_timer_function
mt begin fun timeout
->
46 Lwt_engine.stop_event
!timer_event; (* duplicate stop_event is ok *)
47 timer_event := Lwt_engine.on_timer (float_of_int timeout
/. 1000.) false on_timer
49 M.set_socket_function
mt begin fun fd what
->
52 List.iter
Lwt_engine.stop_event
(Hashtbl.find
all_events fd
);
53 Hashtbl.remove
all_events fd
;
55 Not_found
-> () (* first event for the socket - no association *)
57 let events = match what
with
58 | M.POLL_REMOVE
| M.POLL_NONE
-> []
59 | M.POLL_IN
-> [Lwt_engine.on_readable fd
(on_readable fd
)]
60 | M.POLL_OUT
-> [Lwt_engine.on_writable fd
(on_writable fd
)]
61 | M.POLL_INOUT
-> [Lwt_engine.on_readable fd
(on_readable fd
); Lwt_engine.on_writable fd
(on_writable fd
)]
65 | _
-> Hashtbl.add
all_events fd
events;
67 { mt; all_events; wakeners; }
69 (* lwt may not run in parallel so one global is OK'ish *)
70 let global = lazy (create ())
73 let t = Lazy.force
global in
77 let t = Lazy.force
global in
78 let (waiter
,wakener
) = Lwt.wait
() in
79 let waiter = Lwt.protected
waiter in
80 Lwt.on_cancel
waiter (fun () ->
81 Curl.Multi.remove
t.mt h
;
82 Hashtbl.remove
t.wakeners h
;
84 Hashtbl.add
t.wakeners h wakener
;