Skip to content

Commit 3e67f7d

Browse files
authored
Merge pull request #733 from alyssais/listening
Add Eio_unix.Net.import_socket_listening
2 parents 2c5eb61 + 3b2a679 commit 3e67f7d

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

lib_eio/unix/net.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,23 @@ type t = [`Generic | `Unix] Eio.Net.ty r
6161

6262
type _ Effect.t +=
6363
| Import_socket_stream : Switch.t * bool * Unix.file_descr -> [`Unix_fd | stream_socket_ty] r Effect.t
64+
| Import_socket_listening : Switch.t * bool * Unix.file_descr -> [`Unix_fd | listening_socket_ty] r Effect.t
6465
| Import_socket_datagram : Switch.t * bool * Unix.file_descr -> [`Unix_fd | datagram_socket_ty] r Effect.t
6566
| Socketpair_stream : Switch.t * Unix.socket_domain * int ->
6667
([`Unix_fd | stream_socket_ty] r * [`Unix_fd | stream_socket_ty] r) Effect.t
6768
| Socketpair_datagram : Switch.t * Unix.socket_domain * int ->
6869
([`Unix_fd | datagram_socket_ty] r * [`Unix_fd | datagram_socket_ty] r) Effect.t
6970

7071
let open_stream s = (s : _ stream_socket :> [< `Unix_fd | stream_socket_ty] r)
72+
let open_listening s = (s : _ listening_socket :> [< `Unix_fd | listening_socket_ty] r)
7173
let open_datagram s = (s : _ datagram_socket :> [< `Unix_fd | datagram_socket_ty] r)
7274

7375
let import_socket_stream ~sw ~close_unix fd =
7476
open_stream @@ Effect.perform (Import_socket_stream (sw, close_unix, fd))
7577

78+
let import_socket_listening ~sw ~close_unix fd =
79+
open_listening @@ Effect.perform (Import_socket_listening (sw, close_unix, fd))
80+
7681
let import_socket_datagram ~sw ~close_unix fd =
7782
open_datagram @@ Effect.perform (Import_socket_datagram (sw, close_unix, fd))
7883

lib_eio/unix/net.mli

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,22 @@ end
5656
(** {2 Creating or importing sockets} *)
5757

5858
val import_socket_stream : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [`Unix_fd | stream_socket_ty] r
59-
(** [import_socket_stream ~sw ~close_unix:true fd] is an Eio flow that uses [fd].
59+
(** [import_socket_stream ~sw ~close_unix fd] is an Eio flow that uses [fd].
6060
6161
It can be cast to e.g. {!source} for a one-way flow.
6262
The socket object will be closed when [sw] finishes.
6363
6464
The [close_unix] and [sw] arguments are passed to {!Fd.of_unix}. *)
6565

66+
val import_socket_listening : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [`Unix_fd | listening_socket_ty] r
67+
(** [import_socket_listening ~sw ~close_unix fd] is an Eio listening socket that uses [fd].
68+
69+
The socket object will be closed when [sw] finishes.
70+
71+
The [close_unix] and [sw] arguments are passed to {!Fd.of_unix}. *)
72+
6673
val import_socket_datagram : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [`Unix_fd | datagram_socket_ty] r
67-
(** [import_socket_datagram ~sw ~close_unix:true fd] is an Eio datagram socket that uses [fd].
74+
(** [import_socket_datagram ~sw ~close_unix fd] is an Eio datagram socket that uses [fd].
6875
6976
The socket object will be closed when [sw] finishes.
7077
@@ -100,6 +107,8 @@ val getnameinfo : Eio.Net.Sockaddr.t -> (string * string)
100107
type _ Effect.t +=
101108
| Import_socket_stream :
102109
Switch.t * bool * Unix.file_descr -> [`Unix_fd | stream_socket_ty] r Effect.t (** See {!import_socket_stream} *)
110+
| Import_socket_listening :
111+
Switch.t * bool * Unix.file_descr -> [`Unix_fd | listening_socket_ty] r Effect.t (** See {!import_socket_listening} *)
103112
| Import_socket_datagram :
104113
Switch.t * bool * Unix.file_descr -> [`Unix_fd | datagram_socket_ty] r Effect.t (** See {!import_socket_datagram} *)
105114
| Socketpair_stream : Eio.Switch.t * Unix.socket_domain * int ->

lib_eio_linux/eio_linux.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,10 @@ let run_event_loop (type a) ?fallback config (main : _ -> a) arg : a =
504504
let fd = Fd.of_unix ~sw ~seekable:false ~close_unix fd in
505505
continue k (Flow.of_fd fd :> _ Eio_unix.Net.stream_socket)
506506
)
507+
| Eio_unix.Net.Import_socket_listening (sw, close_unix, fd) -> Some (fun k ->
508+
let fd = Fd.of_unix ~sw ~seekable:false ~close_unix fd in
509+
continue k (listening_socket fd)
510+
)
507511
| Eio_unix.Net.Import_socket_datagram (sw, close_unix, fd) -> Some (fun k ->
508512
let fd = Fd.of_unix ~sw ~seekable:false ~close_unix fd in
509513
continue k (datagram_socket fd)

lib_eio_posix/domain_mgr.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ let run_event_loop fn x =
4848
Unix.set_nonblock unix_fd;
4949
continue k (Flow.of_fd fd :> _ Eio_unix.Net.stream_socket)
5050
)
51+
| Eio_unix.Net.Import_socket_listening (sw, close_unix, unix_fd) -> Some (fun k ->
52+
let fd = Fd.of_unix ~sw ~blocking:false ~close_unix unix_fd in
53+
Unix.set_nonblock unix_fd;
54+
continue k (Net.listening_socket ~hook:Switch.null_hook fd)
55+
)
5156
| Eio_unix.Net.Import_socket_datagram (sw, close_unix, unix_fd) -> Some (fun k ->
5257
let fd = Fd.of_unix ~sw ~blocking:false ~close_unix unix_fd in
5358
Unix.set_nonblock unix_fd;

lib_eio_windows/domain_mgr.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ let run_event_loop fn x =
4848
(try Unix.set_nonblock unix_fd with Unix.Unix_error (Unix.ENOTSOCK, _, _) -> ());
4949
continue k (Flow.of_fd fd :> _ Eio_unix.Net.stream_socket)
5050
)
51+
| Eio_unix.Net.Import_socket_listening (sw, close_unix, unix_fd) -> Some (fun k ->
52+
let fd = Fd.of_unix ~sw ~blocking:false ~close_unix unix_fd in
53+
Unix.set_nonblock unix_fd;
54+
continue k (Net.listening_socket ~hook:Switch.null_hook fd)
55+
)
5156
| Eio_unix.Net.Import_socket_datagram (sw, close_unix, unix_fd) -> Some (fun k ->
5257
let fd = Fd.of_unix ~sw ~blocking:false ~close_unix unix_fd in
5358
Unix.set_nonblock unix_fd;

tests/network.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,29 @@ Wrapping a Unix FD as an Eio stream socket:
373373
- : unit = ()
374374
```
375375

376+
Wrapping a Unix FD as a listening Eio socket:
377+
378+
```ocaml
379+
# run @@ fun ~net sw ->
380+
let l = Unix.(socket PF_INET SOCK_STREAM 0) in
381+
Unix.bind l (Unix.ADDR_INET (Unix.inet_addr_loopback, 8082));
382+
Unix.listen l 40;
383+
let l = Eio_unix.Net.import_socket_listening ~sw ~close_unix:true l in
384+
Fiber.both
385+
(fun () -> run_server ~sw l)
386+
(fun () ->
387+
run_client ~sw ~net ~addr:(`Tcp (Eio.Net.Ipaddr.V4.loopback, 8082));
388+
traceln "Client finished - cancelling server";
389+
raise Graceful_shutdown
390+
);;
391+
+Connecting to server...
392+
+Server accepted connection from client
393+
+Server received: "Hello from client"
394+
+Client received: "Bye"
395+
+Client finished - cancelling server
396+
Exception: Graceful_shutdown.
397+
```
398+
376399
Wrapping a Unix FD as an datagram Eio socket:
377400

378401
```ocaml

0 commit comments

Comments
 (0)