Skip to content

Commit d3f243c

Browse files
committed
fix windows
1 parent 594bc12 commit d3f243c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib_eio_windows/low_level.ml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,24 @@ let socket ~sw socket_domain socket_type protocol =
6060
Unix.set_nonblock sock_unix;
6161
Fd.of_unix ~sw ~blocking:false ~close_unix:true sock_unix
6262

63-
let connect fd addr =
63+
let try_bind fd = function
64+
| None -> ()
65+
| Some bind_addr ->
66+
try Unix.bind fd bind_addr
67+
with Unix.Unix_error (code, name, arg) -> raise @@ Err.wrap_fs code name arg
68+
69+
let connect fd ?bind addr =
6470
try
65-
Fd.use_exn "connect" fd (fun fd -> Unix.connect fd addr)
71+
Fd.use_exn "connect" fd @@ fun fd ->
72+
try_bind fd bind ;
73+
Unix.connect fd addr
6674
with
6775
| Unix.Unix_error ((EINTR | EAGAIN | EWOULDBLOCK | EINPROGRESS), _, _) ->
6876
await_writable fd;
6977
match Fd.use_exn "connect" fd Unix.getsockopt_error with
7078
| None -> ()
7179
| Some code -> raise (Err.wrap code "connect-in-progress" "")
80+
| Unix.Unix_error (code, name, arg) -> raise (Err.wrap code name arg)
7281

7382
let accept ~sw sock =
7483
Fd.use_exn "accept" sock @@ fun sock ->

lib_eio_windows/net.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ let listen ~reuse_addr ~reuse_port ~backlog ~sw (listen_addr : Eio.Net.Sockaddr.
142142
);
143143
(listening_socket ~hook sock :> _ Eio.Net.listening_socket_ty r)
144144

145-
let connect ~sw connect_addr =
145+
let connect ~sw ?bind connect_addr =
146146
let socket_type, addr =
147147
match connect_addr with
148148
| `Unix path -> Unix.SOCK_STREAM, Unix.ADDR_UNIX path
@@ -151,8 +151,9 @@ let connect ~sw connect_addr =
151151
Unix.SOCK_STREAM, Unix.ADDR_INET (host, port)
152152
in
153153
let sock = Low_level.socket ~sw (socket_domain_of connect_addr) socket_type 0 in
154+
let bind = Option.map Eio_unix.Net.sockaddr_to_unix bind in
154155
try
155-
Low_level.connect sock addr;
156+
Low_level.connect sock ?bind addr;
156157
(Flow.of_fd sock :> _ Eio_unix.Net.stream_socket)
157158
with Unix.Unix_error (code, name, arg) -> raise (Err.wrap code name arg)
158159

0 commit comments

Comments
 (0)