File tree Expand file tree Collapse file tree 14 files changed +60
-44
lines changed Expand file tree Collapse file tree 14 files changed +60
-44
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ module Impl = struct
36
36
Switch. on_release sw (fun () -> Eio.Resource. close socket);
37
37
socket
38
38
39
- let connect t ~sw ? bind :_ addr =
39
+ let connect t ~sw ~ options :_ addr =
40
40
traceln " %s: connect to %a" t.label Eio.Net.Sockaddr. pp addr;
41
41
let socket = Handler. run t.on_connect in
42
42
Switch. on_release sw (fun () -> Eio.Flow. close socket);
Original file line number Diff line number Diff line change @@ -180,6 +180,11 @@ type 'tag ty = [`Network | `Platform of 'tag]
180
180
type 'a t = 'a r
181
181
constraint 'a = [> [> `Generic ] ty ]
182
182
183
+ type option = ..
184
+ type option + = Bind of Sockaddr .stream
185
+ | Reuse_addr
186
+ | Reuse_port
187
+
183
188
module Pi = struct
184
189
module type STREAM_SOCKET = sig
185
190
type tag
@@ -235,7 +240,7 @@ module Pi = struct
235
240
type tag
236
241
237
242
val listen : t -> reuse_addr :bool -> reuse_port :bool -> backlog :int -> sw :Switch .t -> Sockaddr .stream -> tag listening_socket_ty r
238
- val connect : t -> sw :Switch .t -> ? bind : Sockaddr .stream -> Sockaddr .stream -> tag stream_socket_ty r
243
+ val connect : t -> sw :Switch .t -> options : option list -> Sockaddr .stream -> tag stream_socket_ty r
239
244
val datagram_socket :
240
245
t
241
246
-> reuse_addr :bool
@@ -295,10 +300,10 @@ let listen (type tag) ?(reuse_addr=false) ?(reuse_port=false) ~backlog ~sw (t:[>
295
300
let module X = (val (Resource. get ops Pi. Network )) in
296
301
X. listen t ~reuse_addr ~reuse_port ~backlog ~sw
297
302
298
- let connect (type tag ) ~sw ?bind (t :[> tag ty] r ) addr =
303
+ let connect (type tag ) ~sw ?( options = [] ) (t :[> tag ty] r ) addr =
299
304
let (Resource. T (t, ops)) = t in
300
305
let module X = (val (Resource. get ops Pi. Network )) in
301
- try X. connect t ~sw ?bind addr
306
+ try X. connect t ~sw ~options addr
302
307
with Exn. Io _ as ex ->
303
308
let bt = Printexc. get_raw_backtrace () in
304
309
Exn. reraise_with_context ex bt " connecting to %a" Sockaddr. pp addr
Original file line number Diff line number Diff line change @@ -129,12 +129,15 @@ type 'a t = 'a r
129
129
130
130
(* * {2 Out-bound Connections} *)
131
131
132
- val connect : sw :Switch .t -> ?bind : Sockaddr .stream -> [> 'tag ty ] t -> Sockaddr .stream -> 'tag stream_socket_ty r
133
- (* * [connect ~sw t addr] is a new socket connected to remote address [addr].
132
+ type option = ..
133
+ type option + = Bind of Sockaddr .stream
134
+ | Reuse_addr
135
+ | Reuse_port
134
136
135
- The new socket will be closed when [sw] finishes, unless closed manually first.
137
+ val connect : sw :Switch .t -> ?options : option list -> [> 'tag ty ] t -> Sockaddr .stream -> 'tag stream_socket_ty r
138
+ (* * [connect ~sw t addr] is a new socket connected to remote address [addr].
136
139
137
- @param bind Set the outbound client address . *)
140
+ The new socket will be closed when [sw] finishes, unless closed manually first . *)
138
141
139
142
val with_tcp_connect :
140
143
?timeout : Time.Timeout .t ->
@@ -348,7 +351,7 @@ module Pi : sig
348
351
t -> reuse_addr :bool -> reuse_port :bool -> backlog :int -> sw :Switch .t ->
349
352
Sockaddr .stream -> tag listening_socket_ty r
350
353
351
- val connect : t -> sw :Switch .t -> ? bind : Sockaddr .stream -> Sockaddr .stream -> tag stream_socket_ty r
354
+ val connect : t -> sw :Switch .t -> options : option list -> Sockaddr .stream -> tag stream_socket_ty r
352
355
353
356
val datagram_socket :
354
357
t
Original file line number Diff line number Diff line change @@ -85,3 +85,15 @@ let socketpair_datagram ~sw ?(domain=Unix.PF_UNIX) ?(protocol=0) () =
85
85
86
86
let fd socket =
87
87
Option. get (Resource. fd_opt socket)
88
+
89
+ let apply_option fd = function
90
+ | Eio.Net. Bind bind_addr ->
91
+ Unix. bind fd (sockaddr_to_unix bind_addr)
92
+ | Eio.Net. Reuse_addr ->
93
+ Unix. setsockopt fd Unix. SO_REUSEADDR true
94
+ | Eio.Net. Reuse_port ->
95
+ Unix. setsockopt fd Unix. SO_REUSEPORT true
96
+ | _ ->
97
+ invalid_arg " Unknown Eio.Net.option"
98
+
99
+ let configure options fd = List. iter (apply_option fd) options
Original file line number Diff line number Diff line change @@ -97,6 +97,9 @@ val socketpair_datagram :
97
97
val getnameinfo : Eio.Net.Sockaddr .t -> (string * string )
98
98
(* * [getnameinfo sockaddr] returns domain name and service for [sockaddr]. *)
99
99
100
+ val configure : Eio.Net .option list -> Unix .file_descr -> unit
101
+ (* * [configure options fd] prepare the socket with the chosen options. *)
102
+
100
103
type _ Effect.t + =
101
104
| Import_socket_stream :
102
105
Switch .t * bool * Unix .file_descr -> [`Unix_fd | stream_socket_ty ] r Effect .t (* * See {!import_socket_stream} *)
Original file line number Diff line number Diff line change @@ -256,12 +256,11 @@ let socket_domain_of = function
256
256
~v4: (fun _ -> Unix. PF_INET )
257
257
~v6: (fun _ -> Unix. PF_INET6 )
258
258
259
- let connect ~sw ? bind connect_addr =
259
+ let connect ~sw ~ options connect_addr =
260
260
let addr = Eio_unix.Net. sockaddr_to_unix connect_addr in
261
261
let sock_unix = Unix. socket ~cloexec: true (socket_domain_of connect_addr) Unix. SOCK_STREAM 0 in
262
262
let sock = Fd. of_unix ~sw ~seekable: false ~close_unix: true sock_unix in
263
- let bind = Option. map Eio_unix.Net. sockaddr_to_unix bind in
264
- Low_level. connect sock ?bind addr;
263
+ Low_level. connect sock ~options addr;
265
264
(flow sock :> _ Eio_unix.Net.stream_socket )
266
265
267
266
module Impl = struct
@@ -297,8 +296,8 @@ module Impl = struct
297
296
Unix. listen sock_unix backlog;
298
297
(listening_socket sock :> _ Eio.Net.listening_socket_ty r )
299
298
300
- let connect () ~sw ? bind addr =
301
- (connect ~sw ?bind addr :> [`Generic | `Unix ] Eio.Net. stream_socket_ty r)
299
+ let connect () ~sw ~ options addr =
300
+ (connect ~sw ~options addr :> [`Generic | `Unix ] Eio.Net. stream_socket_ty r)
302
301
303
302
let datagram_socket () ~reuse_addr ~reuse_port ~sw saddr =
304
303
if reuse_addr then (
Original file line number Diff line number Diff line change @@ -221,15 +221,16 @@ let splice src ~dst ~len =
221
221
else if res = 0 then raise End_of_file
222
222
else raise @@ Err. wrap (Uring. error_of_errno res) " splice" " "
223
223
224
- let try_bind fd = function
225
- | None -> ()
226
- | Some bind_addr ->
227
- try Unix. bind fd bind_addr
228
- with Unix. Unix_error (code , name , arg ) -> raise @@ Err. wrap_fs code name arg
229
-
230
- let connect fd ?bind addr =
224
+ let apply_option fd = function
225
+ | Eio.Net. Bind bind_addr ->
226
+ let bind_addr = Eio_unix.Net. sockaddr_to_unix bind_addr in
227
+ (try Unix. bind fd bind_addr
228
+ with Unix. Unix_error (code , name , arg ) -> raise @@ Err. wrap_fs code name arg)
229
+ | _ -> invalid_arg " Unknown option"
230
+
231
+ let connect fd ~options addr =
231
232
Fd. use_exn " connect" fd @@ fun fd ->
232
- try_bind fd bind ;
233
+ List. iter (apply_option fd) options ;
233
234
let res = Sched. enter " connect" (enqueue_connect fd addr) in
234
235
if res < 0 then (
235
236
let ex =
Original file line number Diff line number Diff line change @@ -111,8 +111,9 @@ val splice : fd -> dst:fd -> len:int -> int
111
111
@raise End_of_file [src] is at the end of the file.
112
112
@raise Unix.Unix_error(EINVAL, "splice", _) if splice is not supported for these FDs. *)
113
113
114
- val connect : fd -> ?bind : Unix .sockaddr -> Unix .sockaddr -> unit
115
- (* * [connect fd addr] attempts to connect socket [fd] to [addr]. *)
114
+ val connect : fd -> options :Eio .Net .option list -> Unix .sockaddr -> unit
115
+ (* * [connect fd ~options addr] attempts to connect socket [fd] to [addr]
116
+ after configuring the socket with [options]. *)
116
117
117
118
val await_readable : fd -> unit
118
119
(* * [await_readable fd] blocks until [fd] is readable (or has an error). *)
Original file line number Diff line number Diff line change @@ -67,10 +67,10 @@ let socket ~sw socket_domain socket_type protocol =
67
67
Unix. set_nonblock sock_unix;
68
68
Fd. of_unix ~sw ~blocking: false ~close_unix: true sock_unix
69
69
70
- let connect fd ? bind addr =
70
+ let connect fd ~ options addr =
71
71
try
72
72
Fd. use_exn " connect" fd @@ fun fd ->
73
- Option. iter ( Unix. bind fd) bind ;
73
+ Eio_unix.Net. configure options fd ;
74
74
Unix. connect fd addr
75
75
with
76
76
| Unix. Unix_error ((EINTR | EAGAIN | EWOULDBLOCK | EINPROGRESS ), _ , _ ) ->
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ val read : fd -> bytes -> int -> int -> int
28
28
val write : fd -> bytes -> int -> int -> int
29
29
30
30
val socket : sw :Switch .t -> Unix .socket_domain -> Unix .socket_type -> int -> fd
31
- val connect : fd -> ? bind : Unix .sockaddr -> Unix .sockaddr -> unit
31
+ val connect : fd -> options : Eio . Net . option list -> Unix .sockaddr -> unit
32
32
val accept : sw :Switch .t -> fd -> fd * Unix .sockaddr
33
33
34
34
val shutdown : fd -> Unix .shutdown_command -> unit
You can’t perform that action at this time.
0 commit comments