File: fecomms.ml

package info (click to toggle)
xen-api-libs 0.5.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,940 kB
  • sloc: ml: 13,925; sh: 2,930; ansic: 1,699; makefile: 1,240; python: 83
file content (51 lines) | stat: -rw-r--r-- 1,338 bytes parent folder | download
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
open Fe

let open_unix_domain_sock () =
  Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0

let open_unix_domain_sock_server path =
  Unixext.mkdir_rec (Filename.dirname path) 0o755;
  Unixext.unlink_safe path;
  let sock = open_unix_domain_sock () in
  try
    Unix.bind sock (Unix.ADDR_UNIX path);
    Unix.listen sock 5;
    sock
  with e ->
    Unix.close sock;
    raise e

let open_unix_domain_sock_client path =
  let sock = open_unix_domain_sock () in
  try 
    Unix.connect sock (Unix.ADDR_UNIX path);
    sock
  with e ->
    Unix.close sock;
    raise e

let read_raw_rpc sock =
  let buffer = String.make 12 '\000' in
  Unixext.really_read sock buffer 0 12;
  let len = int_of_string buffer in
  let body = Unixext.really_read_string sock len in
  ferpc_of_rpc (Jsonrpc.of_string body)

let write_raw_rpc sock ferpc =
  let body = Jsonrpc.to_string (rpc_of_ferpc ferpc) in
  let len = String.length body in
  let buffer = Printf.sprintf "%012d%s" len body in
  Unixext.really_write_string sock buffer

exception Connection_closed

let receive_named_fd sock =
  let buffer = String.make 36 '\000' in
  let (len,from,newfd) = Unixext.recv_fd sock buffer 0 36 [] in  
  if len=0 then raise Connection_closed;
  (newfd,buffer)

let send_named_fd sock uuid fd =
  ignore(Unixext.send_fd sock uuid 0 (String.length uuid) [] fd)