Skip to content

Commit 7dfd83c

Browse files
committed
Switch to poll.
1 parent 8707a05 commit 7dfd83c

File tree

6 files changed

+30
-157
lines changed

6 files changed

+30
-157
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
=====
33
* Raise a proper error when non-blocking connection
44
fails.
5+
* Switch to `poll` for polling implementation.
56

67
1.0.2 (2024-01-08)
78
======

cry.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ bug-reports: "https://github.com/savonet/ocaml-cry/issues"
1212
depends: [
1313
"ocaml" {>= "4.12.0"}
1414
"dune" {>= "2.8"}
15+
"poll"
1516
"base-bytes"
1617
"odoc" {with-doc}
1718
]

dune-project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
(name cry)
1414
(synopsis "OCaml client for the various icecast & shoutcast source protocols")
1515
(description "The cry library is an implementation of the various icecast & shoutcast protocols to connect to streaming servers such as icecast")
16-
(depends (ocaml (>= 4.12.0)) dune base-bytes)
16+
(depends (ocaml (>= 4.12.0)) dune poll base-bytes)
1717
)

src/cry.ml

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,28 @@
2020

2121
(** OCaml low level implementation of the shout source protocol. *)
2222

23-
external poll :
24-
Unix.file_descr array ->
25-
Unix.file_descr array ->
26-
Unix.file_descr array ->
27-
float ->
28-
Unix.file_descr array * Unix.file_descr array * Unix.file_descr array
29-
= "caml_cry_poll"
30-
31-
let poll r w e timeout =
32-
let r = Array.of_list r in
33-
let w = Array.of_list w in
34-
let e = Array.of_list e in
35-
let r, w, e = poll r w e timeout in
36-
(Array.to_list r, Array.to_list w, Array.to_list e)
37-
38-
let select = match Sys.os_type with "Unix" -> poll | _ -> Unix.select
23+
let poll r w timeout =
24+
let timeout =
25+
match timeout with
26+
| x when x < 0. -> Poll.Timeout.never
27+
| 0. -> Poll.Timeout.immediate
28+
| x ->
29+
let frac, int = modf x in
30+
let int = Int64.mul (Int64.of_float int) 1_000_000_000L in
31+
let frac = Int64.of_float (frac *. 1_000_000_000.) in
32+
let timeout = Int64.add int frac in
33+
Poll.Timeout.after timeout
34+
in
35+
let poll = Poll.create () in
36+
List.iter (fun fd -> Poll.set poll fd Poll.Event.read) r;
37+
List.iter (fun fd -> Poll.set poll fd Poll.Event.write) w;
38+
ignore (Poll.wait poll timeout);
39+
let r = ref [] in
40+
let w = ref [] in
41+
Poll.iter_ready poll ~f:(fun fd event ->
42+
if event.Poll.Event.readable then r := fd :: !r;
43+
if event.Poll.Event.writable then w := fd :: !w);
44+
(!r, !w)
3945

4046
type error =
4147
| Create of exn
@@ -90,9 +96,8 @@ let wait_for ?(log = fun _ -> ()) event timeout =
9096
| `Both socket -> ([socket], [socket])
9197
in
9298
let rec wait t =
93-
let r, w, _ =
94-
try select r w [] t
95-
with Unix.Unix_error (Unix.EINTR, _, _) -> ([], [], [])
99+
let r, w =
100+
try poll r w t with Unix.Unix_error (Unix.EINTR, _, _) -> ([], [])
96101
in
97102
if r = [] && w = [] then (
98103
let current_time = Unix.gettimeofday () in
@@ -164,8 +169,8 @@ let connect_sockaddr ?bind_address ?timeout sockaddr =
164169
let do_timeout = timeout <> None in
165170
let check_timeout () =
166171
let timeout = Option.get timeout in
167-
(* Block in a select call for [timeout] seconds. *)
168-
let _, w, _ = select [] [socket] [] timeout in
172+
(* Block in a poll call for [timeout] seconds. *)
173+
let _, w = poll [] [socket] timeout in
169174
if w = [] then raise Timeout;
170175
match Unix.getsockopt_error socket with
171176
| Some err -> raise (Unix.Unix_error (err, "connect", ""))

src/cry_stubs.c

Lines changed: 0 additions & 131 deletions
This file was deleted.

src/dune

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
(library
22
(name cry)
33
(public_name cry)
4-
(libraries bytes unix)
5-
(foreign_stubs
6-
(language c)
7-
(names cry_stubs))
4+
(libraries poll bytes unix)
85
(synopsis
96
"OCaml client for the various icecast & shoutcast source protocols"))

0 commit comments

Comments
 (0)