Skip to content

Commit

Permalink
fix windows
Browse files Browse the repository at this point in the history
  • Loading branch information
art-w committed Mar 20, 2024
1 parent 594bc12 commit d3f243c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib_eio_windows/low_level.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,24 @@ let socket ~sw socket_domain socket_type protocol =
Unix.set_nonblock sock_unix;
Fd.of_unix ~sw ~blocking:false ~close_unix:true sock_unix

let connect fd addr =
let try_bind fd = function
| None -> ()
| Some bind_addr ->
try Unix.bind fd bind_addr
with Unix.Unix_error (code, name, arg) -> raise @@ Err.wrap_fs code name arg

let connect fd ?bind addr =
try
Fd.use_exn "connect" fd (fun fd -> Unix.connect fd addr)
Fd.use_exn "connect" fd @@ fun fd ->
try_bind fd bind ;
Unix.connect fd addr
with
| Unix.Unix_error ((EINTR | EAGAIN | EWOULDBLOCK | EINPROGRESS), _, _) ->
await_writable fd;
match Fd.use_exn "connect" fd Unix.getsockopt_error with
| None -> ()
| Some code -> raise (Err.wrap code "connect-in-progress" "")
| Unix.Unix_error (code, name, arg) -> raise (Err.wrap code name arg)

let accept ~sw sock =
Fd.use_exn "accept" sock @@ fun sock ->
Expand Down
5 changes: 3 additions & 2 deletions lib_eio_windows/net.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ let listen ~reuse_addr ~reuse_port ~backlog ~sw (listen_addr : Eio.Net.Sockaddr.
);
(listening_socket ~hook sock :> _ Eio.Net.listening_socket_ty r)

let connect ~sw connect_addr =
let connect ~sw ?bind connect_addr =
let socket_type, addr =
match connect_addr with
| `Unix path -> Unix.SOCK_STREAM, Unix.ADDR_UNIX path
Expand All @@ -151,8 +151,9 @@ let connect ~sw connect_addr =
Unix.SOCK_STREAM, Unix.ADDR_INET (host, port)
in
let sock = Low_level.socket ~sw (socket_domain_of connect_addr) socket_type 0 in
let bind = Option.map Eio_unix.Net.sockaddr_to_unix bind in
try
Low_level.connect sock addr;
Low_level.connect sock ?bind addr;
(Flow.of_fd sock :> _ Eio_unix.Net.stream_socket)
with Unix.Unix_error (code, name, arg) -> raise (Err.wrap code name arg)

Expand Down

0 comments on commit d3f243c

Please sign in to comment.