-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore conversions between libuv and Unix fds
The conversions were originally removed in 705ec14. This commit restores them, but puts them into a separate opam package luv_unix. The conversions can be accessed as, for example, Luv_unix.Os_fd.Fd.from_unix. The main package Luv also provides "neater" paths Luv.Unix.Os_fd.Fd.from_unix through a module alias. Resolves #90. In the future, luv_unix needs a constraint to require the next release of Luv (probably 0.5.7). The module also needs to be linked from the API docs.
- Loading branch information
Showing
12 changed files
with
233 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
opam-version: "2.0" | ||
|
||
synopsis: "Helpers for interfacing Luv and Unix" | ||
|
||
version: "0.5.0" | ||
license: "MIT" | ||
homepage: "https://github.com/aantron/luv" | ||
doc: "https://aantron.github.io/luv" | ||
bug-reports: "https://github.com/aantron/luv/issues" | ||
|
||
authors: "Anton Bachin <[email protected]>" | ||
maintainer: "Anton Bachin <[email protected]>" | ||
dev-repo: "git+https://github.com/aantron/luv.git" | ||
|
||
depends: [ | ||
"base-unix" | ||
"dune" {>= "2.0.0"} | ||
"luv" | ||
"ocaml" {>= "4.02.0"} | ||
"result" | ||
] | ||
|
||
build: [ | ||
["dune" "build" "-p" name "-j" jobs] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
(libraries luv.c result) | ||
(flags (:standard -w -49 -open Result))) | ||
|
||
(documentation) | ||
(documentation | ||
(package luv)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,3 +64,4 @@ module Env = Env | |
module Time = Time | ||
module Random = Random | ||
module Metrics = Metrics | ||
module Unix = Luv_unix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(library | ||
(public_name luv_unix) | ||
(libraries luv luv.c unix) | ||
(foreign_stubs (language c) (names luv_unix))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// This file is part of Luv, released under the MIT license. See LICENSE.md for | ||
// details, or visit https://github.com/aantron/luv/blob/master/LICENSE.md. | ||
|
||
|
||
|
||
#include <caml/mlvalues.h> | ||
#include <uv.h> | ||
|
||
|
||
|
||
CAMLprim value luv_unix_fd_to_os_fd(value unix_fd, value os_fd_storage) | ||
{ | ||
uv_os_fd_t *os_fd = (uv_os_fd_t*)Nativeint_val(os_fd_storage); | ||
|
||
#ifndef _WIN32 | ||
*os_fd = Int_val(unix_fd); | ||
#else | ||
if (Descr_kind_val(unix_fd) == KIND_HANDLE) | ||
*os_fd = Handle_val(unix_fd); | ||
else | ||
*os_fd = -1; | ||
#endif | ||
|
||
return Val_unit; | ||
} | ||
|
||
CAMLprim value luv_unix_fd_to_os_socket(value unix_fd, value os_socket_storage) | ||
{ | ||
uv_os_sock_t *os_socket = (uv_os_sock_t*)Nativeint_val(os_socket_storage); | ||
|
||
#ifndef _WIN32 | ||
*os_socket = Int_val(unix_fd); | ||
#else | ||
if (Descr_kind_val(unix_fd) == KIND_SOCKET) | ||
*os_socket = Handle_val(unix_fd); | ||
else | ||
*os_socket = -1; | ||
#endif | ||
|
||
return Val_unit; | ||
} | ||
|
||
CAMLprim value luv_os_fd_to_unix_fd(value os_fd_storage) | ||
{ | ||
uv_os_fd_t *os_fd = (uv_os_fd_t*)Nativeint_val(os_fd_storage); | ||
|
||
#ifndef _WIN32 | ||
return Val_int(*os_fd); | ||
#else | ||
return win_alloc_handle(*os_fd); | ||
#endif | ||
} | ||
|
||
CAMLprim value luv_os_socket_to_unix_fd(value os_socket_storage) | ||
{ | ||
uv_os_sock_t *os_socket = (uv_os_sock_t*)Nativeint_val(os_socket_storage); | ||
|
||
#ifndef _WIN32 | ||
return Val_int(*os_socket); | ||
#else | ||
return win_alloc_socket(*os_socket); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
(* This file is part of Luv, released under the MIT license. See LICENSE.md for | ||
details, or visit https://github.com/aantron/luv/blob/master/LICENSE.md. *) | ||
|
||
|
||
|
||
module C = Luv__C | ||
module Error = Luv.Error | ||
|
||
module Os_fd = | ||
struct | ||
module Fd = | ||
struct | ||
include Luv.Os_fd.Fd | ||
|
||
external from_unix_helper : Unix.file_descr -> nativeint -> unit = | ||
"luv_unix_fd_to_os_fd" | ||
|
||
let from_unix unix_fd = | ||
let os_fd = Ctypes.make C.Types.Os_fd.t in | ||
let storage = Ctypes.(raw_address_of_ptr (to_voidp (addr os_fd))) in | ||
from_unix_helper unix_fd storage; | ||
if C.Functions.Os_fd.is_invalid_handle_value os_fd then | ||
Result.Error `EBADF | ||
else | ||
Result.Ok os_fd | ||
|
||
external to_unix_helper : nativeint -> Unix.file_descr = | ||
"luv_os_fd_to_unix_fd" | ||
|
||
let to_unix os_fd = | ||
to_unix_helper (Ctypes.(raw_address_of_ptr (to_voidp (addr os_fd)))) | ||
end | ||
|
||
module Socket = | ||
struct | ||
include Luv.Os_fd.Socket | ||
|
||
external from_unix_helper : Unix.file_descr -> nativeint -> unit = | ||
"luv_unix_fd_to_os_socket" | ||
|
||
let from_unix unix_fd = | ||
let os_socket = Ctypes.make C.Types.Os_socket.t in | ||
let storage = Ctypes.(raw_address_of_ptr (to_voidp (addr os_socket))) in | ||
from_unix_helper unix_fd storage; | ||
if C.Functions.Os_fd.is_invalid_socket_value os_socket then | ||
Result.Error `EBADF | ||
else | ||
Result.Ok os_socket | ||
|
||
external to_unix_helper : nativeint -> Unix.file_descr = | ||
"luv_os_socket_to_unix_fd" | ||
|
||
let to_unix os_socket = | ||
to_unix_helper (Ctypes.(raw_address_of_ptr (to_voidp (addr os_socket)))) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
(* This file is part of Luv, released under the MIT license. See LICENSE.md for | ||
details, or visit https://github.com/aantron/luv/blob/master/LICENSE.md. *) | ||
|
||
|
||
|
||
module Error = Luv.Error | ||
|
||
module Os_fd : | ||
sig | ||
module Fd : | ||
sig | ||
include module type of Luv.Os_fd.Fd | ||
|
||
val from_unix : Unix.file_descr -> (t, Error.t) result | ||
(** Attempts to convert from a | ||
{{:https://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#TYPEfile_descr} | ||
[Unix.file_descr]} to a libuv [uv_os_fd_t]. Fails on Windows if the | ||
descriptor is a [SOCKET] rather than a [HANDLE]. *) | ||
|
||
val to_unix : t -> Unix.file_descr | ||
(** Converts a [uv_os_fd_t] to a | ||
{{:https://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#TYPEfile_descr} | ||
[Unix.file_descr]}. *) | ||
end | ||
|
||
module Socket : | ||
sig | ||
include module type of Luv.Os_fd.Socket | ||
|
||
val from_unix : Unix.file_descr -> (t, Error.t) result | ||
(** Attempts to convert from a | ||
{{:https://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#TYPEfile_descr} | ||
[Unix.file_descr]} to a libuv [uv_os_sock_t]. Fails on Windows if the | ||
descriptor is a [HANDLE] rather than a [SOCKET]. *) | ||
|
||
val to_unix : t -> Unix.file_descr | ||
(** Converts a [uv_os_sock_t] to a | ||
{{:https://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#TYPEfile_descr} | ||
[Unix.file_descr]}. *) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters