package hvsock

  1. Overview
  2. Docs

Source file af_common.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

module type S = sig
  (** A low-level socket interface, common to both Windows and Linux kernels *)

  type t

  type sockaddr
  (** A socket address *)

  val string_of_sockaddr: sockaddr -> string

  val create: unit -> t
  (** [create ()] creates an unbound socket *)

  val bind: t -> sockaddr -> unit
  (** [bind socket sockaddr] binds [socket] to [sockaddr] *)

  val listen: t -> int -> unit
  (** [listen socket queue_length] sets the socket to listening mode with the
      given maximum queue length *)

  val accept: t -> t * sockaddr
  (** [accept fd] accepts a single connection *)

  val connect: ?timeout_ms:int -> t -> sockaddr -> unit
  (** [connect ?timeout_ms fd sockaddr] connects to a remote socket.
      On Windows the raw connect call can block forever if the server is not
      running when the call is executed (even if the server starts up afterwards)
      there is a default timeout of 300ms. On timeout this will raise
      [Unix_error(Unix.ETIMEDOUT)] *)

  val writev: t -> Cstruct.t list -> int
  (** Write a list of buffers *)

  val read_into: t -> Cstruct.t -> int
  (** Read into a buffer, returning the number of bytes written *)

  val shutdown_read: t -> unit
  (** Close the read half of the connection *)

  val shutdown_write: t -> unit
  (** Close the write half of the connection *)

  val close: t -> unit
  (** Close both halves of the connection *)
end

type buffer = (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t

external stub_ba_sendv: Unix.file_descr -> (buffer * int * int) list -> int = "stub_hvsock_ba_sendv"
external stub_ba_recv: Unix.file_descr -> buffer -> int -> int -> int = "stub_hvsock_ba_recv"

let writev fd bs =
  let bs' = List.map (fun b -> b.Cstruct.buffer, b.Cstruct.off, b.Cstruct.len) bs in
  stub_ba_sendv fd bs'
let read_into fd b = stub_ba_recv fd b.Cstruct.buffer b.Cstruct.off b.Cstruct.len
OCaml

Innovation. Community. Security.