Skip to content

Commit 90af8f7

Browse files
committed
Remove Unix_fd from unix network socket types
The normal ways of getting a socket don't include it anyway, and we can infer it from something being a Unix socket.
1 parent 57d0888 commit 90af8f7

File tree

14 files changed

+141
-138
lines changed

14 files changed

+141
-138
lines changed

lib_eio/unix/eio_unix.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module Ctf = Ctf_unix
3030

3131
module Process = Process
3232
module Net = Net
33+
module Pi = Pi
3334

3435
module Stdenv = struct
3536
type base = <

lib_eio/unix/eio_unix.mli

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,6 @@ module Resource : sig
2828
(** [fd_opt t] returns the FD being wrapped by a generic resource, if any.
2929
3030
This just probes [t] using {!extension-FD}. *)
31-
32-
module type FLOW = sig
33-
include Eio.Net.Pi.STREAM_SOCKET
34-
include Eio.File.Pi.WRITE with type t := t
35-
include Net.Pi.STREAM_SOCKET with type t := t
36-
37-
val fd : t -> Fd.t
38-
end
39-
40-
val flow_handler :
41-
(module FLOW with type t = 't and type tag = 'tag) ->
42-
('t, [`Unix_fd | 'tag Eio.Net.stream_socket_ty | Eio.File.rw_ty]) Eio.Resource.handler
43-
44-
module type DATAGRAM_SOCKET = sig
45-
include Eio.Net.Pi.DATAGRAM_SOCKET
46-
47-
val fd : t -> Fd.t
48-
end
49-
50-
val datagram_handler :
51-
(module DATAGRAM_SOCKET with type t = 't and type tag = 'tag) ->
52-
('t, [`Unix_fd | 'tag Eio.Net.datagram_socket_ty]) Eio.Resource.handler
53-
54-
module type LISTENING_SOCKET = sig
55-
include Eio.Net.Pi.LISTENING_SOCKET
56-
57-
val fd : t -> Fd.t
58-
end
59-
60-
val listening_socket_handler :
61-
(module LISTENING_SOCKET with type t = 't and type tag = 'tag) ->
62-
('t, [`Unix_fd | 'tag Eio.Net.listening_socket_ty]) Eio.Resource.handler
6331
end
6432

6533
module Net = Net
@@ -130,3 +98,5 @@ module Private : sig
13098
end
13199

132100
module Ctf = Ctf_unix
101+
102+
module Pi = Pi

lib_eio/unix/net.ml

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
open Eio.Std
22

3-
type stream_socket_ty = [`Unix_fd | [`Generic | `Unix] Eio.Net.stream_socket_ty]
4-
type datagram_socket_ty = [`Unix_fd | [`Generic | `Unix] Eio.Net.datagram_socket_ty]
5-
type listening_socket_ty = [`Unix_fd | [`Generic | `Unix] Eio.Net.listening_socket_ty]
3+
type stream_socket_ty = [`Generic | `Unix] Eio.Net.stream_socket_ty
4+
type datagram_socket_ty = [`Generic | `Unix] Eio.Net.datagram_socket_ty
5+
type listening_socket_ty = [`Generic | `Unix] Eio.Net.listening_socket_ty
66
type 'a stream_socket = ([> stream_socket_ty] as 'a) r
77
type 'a datagram_socket = ([> datagram_socket_ty] as 'a) r
88
type 'a listening_socket = ([> listening_socket_ty] as 'a) r
@@ -30,19 +30,6 @@ let sockaddr_of_unix_datagram = function
3030
let host = Ipaddr.of_unix host in
3131
`Udp (host, port)
3232

33-
module Pi = struct
34-
module type STREAM_SOCKET = sig
35-
type t
36-
37-
val send_msg : t -> fds:Fd.t list -> Cstruct.t list -> int
38-
39-
val recv_msg_with_fds : t -> sw:Switch.t -> max_fds:int -> Cstruct.t list -> int * Fd.t list
40-
end
41-
42-
type (_, _, _) Eio.Resource.pi +=
43-
| Stream_socket : ('t, (module STREAM_SOCKET with type t = 't), [> `Platform of [> `Unix] | `Socket | `Stream]) Eio.Resource.pi
44-
end
45-
4633
let send_msg (Eio.Resource.T (t, ops)) ?(fds=[]) bufs =
4734
let module X = (val (Eio.Resource.get ops Pi.Stream_socket)) in
4835
let rec aux ~fds bufs =
@@ -73,15 +60,15 @@ type t = [`Generic | `Unix] Eio.Net.ty r
7360
[@@@alert "-unstable"]
7461

7562
type _ Effect.t +=
76-
| Import_socket_stream : Switch.t * bool * Unix.file_descr -> stream_socket_ty r Effect.t
77-
| Import_socket_datagram : Switch.t * bool * Unix.file_descr -> datagram_socket_ty r Effect.t
63+
| Import_socket_stream : Switch.t * bool * Unix.file_descr -> [`Unix_fd | stream_socket_ty] r Effect.t
64+
| Import_socket_datagram : Switch.t * bool * Unix.file_descr -> [`Unix_fd | datagram_socket_ty] r Effect.t
7865
| Socketpair_stream : Switch.t * Unix.socket_domain * int ->
79-
(stream_socket_ty r * stream_socket_ty r) Effect.t
66+
([`Unix_fd | stream_socket_ty] r * [`Unix_fd | stream_socket_ty] r) Effect.t
8067
| Socketpair_datagram : Switch.t * Unix.socket_domain * int ->
81-
(datagram_socket_ty r * datagram_socket_ty r) Effect.t
68+
([`Unix_fd | datagram_socket_ty] r * [`Unix_fd | datagram_socket_ty] r) Effect.t
8269

83-
let open_stream s = (s : _ stream_socket :> [< stream_socket_ty] r)
84-
let open_datagram s = (s : _ datagram_socket :> [< datagram_socket_ty] r)
70+
let open_stream s = (s : _ stream_socket :> [< `Unix_fd | stream_socket_ty] r)
71+
let open_datagram s = (s : _ datagram_socket :> [< `Unix_fd | datagram_socket_ty] r)
8572

8673
let import_socket_stream ~sw ~close_unix fd =
8774
open_stream @@ Effect.perform (Import_socket_stream (sw, close_unix, fd))
@@ -95,3 +82,6 @@ let socketpair_stream ~sw ?(domain=Unix.PF_UNIX) ?(protocol=0) () =
9582

9683
let socketpair_datagram ~sw ?(domain=Unix.PF_UNIX) ?(protocol=0) () =
9784
Effect.perform (Socketpair_datagram (sw, domain, protocol))
85+
86+
let fd socket =
87+
Option.get (Resource.fd_opt socket)

lib_eio/unix/net.mli

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ open Eio.Std
44
55
These extend the types in {!Eio.Net} with support for file descriptors. *)
66

7-
type stream_socket_ty = [`Unix_fd | [`Generic | `Unix] Eio.Net.stream_socket_ty]
8-
type datagram_socket_ty = [`Unix_fd | [`Generic | `Unix] Eio.Net.datagram_socket_ty]
9-
type listening_socket_ty = [`Unix_fd | [`Generic | `Unix] Eio.Net.listening_socket_ty]
7+
type stream_socket_ty = [`Generic | `Unix] Eio.Net.stream_socket_ty
8+
type datagram_socket_ty = [`Generic | `Unix] Eio.Net.datagram_socket_ty
9+
type listening_socket_ty = [`Generic | `Unix] Eio.Net.listening_socket_ty
1010
type 'a stream_socket = ([> stream_socket_ty] as 'a) r
1111
type 'a datagram_socket = ([> datagram_socket_ty] as 'a) r
1212
type 'a listening_socket = ([> listening_socket_ty] as 'a) r
@@ -31,6 +31,9 @@ val recv_msg_with_fds :
3131
3232
@param max_fds The maximum number of file descriptors to accept (additional ones will be closed). *)
3333

34+
val fd : [> `Platform of [> `Unix] | `Socket] r -> Fd.t
35+
(** [fd socket] is the underlying FD of [socket]. *)
36+
3437
(** {2 Unix address conversions}
3538
3639
Note: OCaml's {!Unix.sockaddr} type considers e.g. TCP port 80 and UDP port
@@ -52,15 +55,15 @@ end
5255

5356
(** {2 Creating or importing sockets} *)
5457

55-
val import_socket_stream : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> stream_socket_ty r
58+
val import_socket_stream : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [`Unix_fd | stream_socket_ty] r
5659
(** [import_socket_stream ~sw ~close_unix:true fd] is an Eio flow that uses [fd].
5760
5861
It can be cast to e.g. {!source} for a one-way flow.
5962
The socket object will be closed when [sw] finishes.
6063
6164
The [close_unix] and [sw] arguments are passed to {!Fd.of_unix}. *)
6265

63-
val import_socket_datagram : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> datagram_socket_ty r
66+
val import_socket_datagram : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [`Unix_fd | datagram_socket_ty] r
6467
(** [import_socket_datagram ~sw ~close_unix:true fd] is an Eio datagram socket that uses [fd].
6568
6669
The socket object will be closed when [sw] finishes.
@@ -72,7 +75,7 @@ val socketpair_stream :
7275
?domain:Unix.socket_domain ->
7376
?protocol:int ->
7477
unit ->
75-
stream_socket_ty r * stream_socket_ty r
78+
[`Unix_fd | stream_socket_ty] r * [`Unix_fd | stream_socket_ty] r
7679
(** [socketpair_stream ~sw ()] returns a connected pair of flows, such that writes to one can be read by the other.
7780
7881
This creates OS-level resources using [socketpair(2)].
@@ -83,37 +86,24 @@ val socketpair_datagram :
8386
?domain:Unix.socket_domain ->
8487
?protocol:int ->
8588
unit ->
86-
datagram_socket_ty r * datagram_socket_ty r
89+
[`Unix_fd | datagram_socket_ty] r * [`Unix_fd | datagram_socket_ty] r
8790
(** [socketpair_datagram ~sw ()] returns a connected pair of flows, such that writes to one can be read by the other.
8891
8992
This creates OS-level resources using [socketpair(2)].
9093
Note that, like all FDs created by Eio, they are both marked as close-on-exec by default. *)
9194

92-
module Pi : sig
93-
module type STREAM_SOCKET = sig
94-
type t
95-
96-
val send_msg : t -> fds:Fd.t list -> Cstruct.t list -> int
97-
98-
val recv_msg_with_fds : t -> sw:Switch.t -> max_fds:int -> Cstruct.t list -> int * Fd.t list
99-
end
100-
101-
type (_, _, _) Eio.Resource.pi +=
102-
| Stream_socket : ('t, (module STREAM_SOCKET with type t = 't), [> `Platform of [> `Unix] | `Socket | `Stream]) Eio.Resource.pi
103-
end
104-
10595
(** {2 Private API for backends} *)
10696

10797
val getnameinfo : Eio.Net.Sockaddr.t -> (string * string)
10898
(** [getnameinfo sockaddr] returns domain name and service for [sockaddr]. *)
10999

110100
type _ Effect.t +=
111101
| Import_socket_stream :
112-
Switch.t * bool * Unix.file_descr -> stream_socket_ty r Effect.t (** See {!import_socket_stream} *)
102+
Switch.t * bool * Unix.file_descr -> [`Unix_fd | stream_socket_ty] r Effect.t (** See {!import_socket_stream} *)
113103
| Import_socket_datagram :
114-
Switch.t * bool * Unix.file_descr -> datagram_socket_ty r Effect.t (** See {!import_socket_datagram} *)
104+
Switch.t * bool * Unix.file_descr -> [`Unix_fd | datagram_socket_ty] r Effect.t (** See {!import_socket_datagram} *)
115105
| Socketpair_stream : Eio.Switch.t * Unix.socket_domain * int ->
116-
(stream_socket_ty r * stream_socket_ty r) Effect.t (** See {!socketpair_stream} *)
106+
([`Unix_fd | stream_socket_ty] r * [`Unix_fd | stream_socket_ty] r) Effect.t (** See {!socketpair_stream} *)
117107
| Socketpair_datagram : Eio.Switch.t * Unix.socket_domain * int ->
118-
(datagram_socket_ty r * datagram_socket_ty r) Effect.t (** See {!socketpair_datagram} *)
108+
([`Unix_fd | datagram_socket_ty] r * [`Unix_fd | datagram_socket_ty] r) Effect.t (** See {!socketpair_datagram} *)
119109
[@@alert "-unstable"]

lib_eio/unix/pi.ml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
open Eio.Std
2+
3+
module type STREAM_SOCKET = sig
4+
include Eio.Net.Pi.STREAM_SOCKET
5+
6+
val send_msg : t -> fds:Fd.t list -> Cstruct.t list -> int
7+
val recv_msg_with_fds : t -> sw:Switch.t -> max_fds:int -> Cstruct.t list -> int * Fd.t list
8+
9+
val fd : t -> Fd.t
10+
end
11+
12+
type (_, _, _) Eio.Resource.pi +=
13+
| Stream_socket : ('t, (module STREAM_SOCKET with type t = 't), [> `Platform of [> `Unix] | `Socket | `Stream]) Eio.Resource.pi
14+
15+
module type FLOW = sig
16+
include Eio.File.Pi.WRITE
17+
include STREAM_SOCKET with type t := t
18+
end
19+
20+
let flow_handler (type t tag) (module X : FLOW with type t = t and type tag = tag) : (t, _) Eio.Resource.handler =
21+
Eio.Resource.handler @@
22+
Eio.Resource.bindings (Eio.Net.Pi.stream_socket (module X)) @
23+
Eio.Resource.bindings (Eio.File.Pi.rw (module X)) @ [
24+
H (Resource.T, X.fd);
25+
H (Stream_socket, (module X));
26+
]
27+
28+
module type DATAGRAM_SOCKET = sig
29+
include Eio.Net.Pi.DATAGRAM_SOCKET
30+
31+
val fd : t -> Fd.t
32+
end
33+
34+
let datagram_handler (type t tag) (module X : DATAGRAM_SOCKET with type t = t and type tag = tag) : (t, _) Eio.Resource.handler =
35+
Eio.Resource.handler @@
36+
Eio.Resource.bindings (Eio.Net.Pi.datagram_socket (module X)) @ [
37+
H (Resource.T, X.fd);
38+
]
39+
40+
module type LISTENING_SOCKET = sig
41+
include Eio.Net.Pi.LISTENING_SOCKET
42+
43+
val fd : t -> Fd.t
44+
end
45+
46+
let listening_socket_handler (type t tag) (module X : LISTENING_SOCKET with type t = t and type tag = tag)
47+
: (t, _) Eio.Resource.handler =
48+
Eio.Resource.handler @@
49+
Eio.Resource.bindings (Eio.Net.Pi.listening_socket (module X)) @ [
50+
H (Resource.T, X.fd);
51+
]

lib_eio/unix/pi.mli

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
open Eio.Std
2+
3+
module type STREAM_SOCKET = sig
4+
include Eio.Net.Pi.STREAM_SOCKET
5+
6+
val send_msg : t -> fds:Fd.t list -> Cstruct.t list -> int
7+
val recv_msg_with_fds : t -> sw:Switch.t -> max_fds:int -> Cstruct.t list -> int * Fd.t list
8+
9+
val fd : t -> Fd.t
10+
end
11+
12+
type (_, _, _) Eio.Resource.pi +=
13+
| Stream_socket : ('t, (module STREAM_SOCKET with type t = 't), [> `Platform of [> `Unix] | `Socket | `Stream]) Eio.Resource.pi
14+
15+
module type FLOW = sig
16+
include Eio.File.Pi.WRITE
17+
include STREAM_SOCKET with type t := t
18+
end
19+
20+
val flow_handler :
21+
(module FLOW with type t = 't and type tag = 'tag) ->
22+
('t, [`Unix_fd | 'tag Eio.Net.stream_socket_ty | Eio.File.rw_ty]) Eio.Resource.handler
23+
24+
module type DATAGRAM_SOCKET = sig
25+
include Eio.Net.Pi.DATAGRAM_SOCKET
26+
27+
val fd : t -> Fd.t
28+
end
29+
30+
val datagram_handler :
31+
(module DATAGRAM_SOCKET with type t = 't and type tag = 'tag) ->
32+
('t, [`Unix_fd | 'tag Eio.Net.datagram_socket_ty]) Eio.Resource.handler
33+
34+
module type LISTENING_SOCKET = sig
35+
include Eio.Net.Pi.LISTENING_SOCKET
36+
37+
val fd : t -> Fd.t
38+
end
39+
40+
val listening_socket_handler :
41+
(module LISTENING_SOCKET with type t = 't and type tag = 'tag) ->
42+
('t, [`Unix_fd | 'tag Eio.Net.listening_socket_ty]) Eio.Resource.handler

lib_eio/unix/resource.ml

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,3 @@ let fd_opt (Eio.Resource.T (t, ops)) =
77
match Eio.Resource.get_opt ops T with
88
| Some f -> Some (f t)
99
| None -> None
10-
11-
module type FLOW = sig
12-
include Eio.Net.Pi.STREAM_SOCKET
13-
include Eio.File.Pi.WRITE with type t := t
14-
include Net.Pi.STREAM_SOCKET with type t := t
15-
16-
val fd : t -> Fd.t
17-
end
18-
19-
let flow_handler (type t tag) (module X : FLOW with type t = t and type tag = tag) : (t, _) Eio.Resource.handler =
20-
Eio.Resource.handler @@
21-
Eio.Resource.bindings (Eio.Net.Pi.stream_socket (module X)) @
22-
Eio.Resource.bindings (Eio.File.Pi.rw (module X)) @ [
23-
H (T, X.fd);
24-
H (Net.Pi.Stream_socket, (module X));
25-
]
26-
27-
module type DATAGRAM_SOCKET = sig
28-
include Eio.Net.Pi.DATAGRAM_SOCKET
29-
30-
val fd : t -> Fd.t
31-
end
32-
33-
let datagram_handler (type t tag) (module X : DATAGRAM_SOCKET with type t = t and type tag = tag) : (t, _) Eio.Resource.handler =
34-
Eio.Resource.handler @@
35-
Eio.Resource.bindings (Eio.Net.Pi.datagram_socket (module X)) @ [
36-
H (T, X.fd);
37-
]
38-
39-
module type LISTENING_SOCKET = sig
40-
include Eio.Net.Pi.LISTENING_SOCKET
41-
42-
val fd : t -> Fd.t
43-
end
44-
45-
let listening_socket_handler (type t tag) (module X : LISTENING_SOCKET with type t = t and type tag = tag)
46-
: (t, _) Eio.Resource.handler =
47-
Eio.Resource.handler @@
48-
Eio.Resource.bindings (Eio.Net.Pi.listening_socket (module X)) @ [
49-
H (T, X.fd);
50-
]

lib_eio_linux/eio_linux.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ module Datagram_socket = struct
131131
| `All -> Unix.SHUTDOWN_ALL
132132
end
133133

134-
let datagram_handler = Eio_unix.Resource.datagram_handler (module Datagram_socket)
134+
let datagram_handler = Eio_unix.Pi.datagram_handler (module Datagram_socket)
135135

136136
let datagram_socket fd =
137137
Eio.Resource.T (fd, datagram_handler)
@@ -194,12 +194,12 @@ module Flow = struct
194194
n, fds
195195
end
196196

197-
let flow_handler = Eio_unix.Resource.flow_handler (module Flow)
197+
let flow_handler = Eio_unix.Pi.flow_handler (module Flow)
198198

199199
let flow fd =
200200
let r = Eio.Resource.T (fd, flow_handler) in
201-
(r : [Eio_unix.Net.stream_socket_ty | Eio.File.rw_ty] r :>
202-
[< Eio_unix.Net.stream_socket_ty | Eio.File.rw_ty] r)
201+
(r : [`Unix_fd | Eio_unix.Net.stream_socket_ty | Eio.File.rw_ty] r :>
202+
[< `Unix_fd | Eio_unix.Net.stream_socket_ty | Eio.File.rw_ty] r)
203203

204204
let source fd = (flow fd :> _ Eio_unix.source)
205205
let sink fd = (flow fd :> _ Eio_unix.sink)
@@ -224,7 +224,7 @@ module Listening_socket = struct
224224
flow, client_addr
225225
end
226226

227-
let listening_handler = Eio_unix.Resource.listening_socket_handler (module Listening_socket)
227+
let listening_handler = Eio_unix.Pi.listening_socket_handler (module Listening_socket)
228228

229229
let listening_socket fd =
230230
Eio.Resource.T (fd, listening_handler)

lib_eio_posix/flow.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ module Impl = struct
9393
let close = Eio_unix.Fd.close
9494
end
9595

96-
let handler = Eio_unix.Resource.flow_handler (module Impl)
96+
let handler = Eio_unix.Pi.flow_handler (module Impl)
9797

9898
let of_fd fd =
9999
let r = Eio.Resource.T (fd, handler) in

0 commit comments

Comments
 (0)