Source file luv_unix.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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