@@ -10,27 +10,25 @@ exception No_useful_protocol
1010
1111let copy_all src dst =
1212 let buffer = String. make 16384 '\000' in
13- while_lwt true do
14- lwt n = Lwt_unix. read src buffer 0 (String. length buffer) in
13+ let rec loop () =
14+ Lwt_unix. read src buffer 0 (String. length buffer) >> = fun n ->
1515 if n = 0
16- then raise_lwt End_of_file
16+ then Lwt. fail End_of_file
1717 else
18- lwt m = Lwt_unix. write dst buffer 0 n in
19- if n <> m then raise_lwt (Short_write (m, n))
20- else return ()
21- done
18+ Lwt_unix. write dst buffer 0 n >> = fun m ->
19+ if n <> m then Lwt. fail (Short_write (m, n))
20+ else loop ()
21+ in loop ()
2222
2323let proxy a b =
2424 let copy id src dst =
25- try_lwt
26- copy_all src dst
27- with e ->
28- (try Lwt_unix. shutdown src Lwt_unix. SHUTDOWN_RECEIVE with _ -> () );
29- (try Lwt_unix. shutdown dst Lwt_unix. SHUTDOWN_SEND with _ -> () );
30- return () in
31- let ts = [ copy " ab" a b; copy " ba" b a ] in
32- lwt () = Lwt. join ts in
33- return ()
25+ Lwt. catch (fun () -> copy_all src dst)
26+ (fun e ->
27+ (try Lwt_unix. shutdown src Lwt_unix. SHUTDOWN_RECEIVE with _ -> () );
28+ (try Lwt_unix. shutdown dst Lwt_unix. SHUTDOWN_SEND with _ -> () );
29+ return () ) in
30+ let ts = [ copy " ab" a b; copy " ba" b a ] in
31+ Lwt. join ts
3432
3533let file_descr_of_int (x : int ) : Unix.file_descr =
3634 Obj. magic x (* Keep this in sync with ocaml's file_descr type *)
@@ -56,23 +54,23 @@ let _common_options = "COMMON OPTIONS"
5654open Cmdliner
5755
5856(* Options common to all commands *)
59- let common_options_t =
60- let docs = _common_options in
61- let debug =
57+ let common_options_t =
58+ let docs = _common_options in
59+ let debug =
6260 let doc = " Give only debug output." in
6361 Arg. (value & flag & info [" debug" ] ~docs ~doc ) in
6462 let verb =
6563 let doc = " Give verbose output." in
66- let verbose = true , Arg. info [" v" ; " verbose" ] ~docs ~doc in
67- Arg. (last & vflag_all [false ] [verbose]) in
68- let port =
64+ let verbose = true , Arg. info [" v" ; " verbose" ] ~docs ~doc in
65+ Arg. (last & vflag_all [false ] [verbose]) in
66+ let port =
6967 let doc = Printf. sprintf " Specify port to connect to the message switch." in
7068 Arg. (value & opt int 8080 & info [" port" ] ~docs ~doc ) in
7169 Term. (pure Common. make $ debug $ verb $ port)
7270
7371(* Help sections common to all commands *)
74- let help = [
75- `S _common_options;
72+ let help = [
73+ `S _common_options;
7674 `P " These options are common to all commands." ;
7775 `S " MORE HELP" ;
7876 `P " Use `$(mname) $(i,COMMAND) --help' for help on a single command." ; `Noblank ;
@@ -112,23 +110,22 @@ let advertise_t common_options_t proxy_socket =
112110 Printf. fprintf stdout " %s\n %!" (Jsonrpc. to_string (Xcp_channel. rpc_of_protocols protocols));
113111
114112 let t_ip =
115- lwt fd, peer = Lwt_unix. accept s_ip in
116- lwt () = Lwt_unix. close s_ip in
113+ Lwt_unix. accept s_ip >> = fun ( fd , peer ) ->
114+ Lwt_unix. close s_ip >> = fun () ->
117115 proxy fd (Lwt_unix. of_unix_file_descr proxy_socket) in
118116 let t_unix =
119- lwt fd, peer = Lwt_unix. accept s_unix in
117+ Lwt_unix. accept s_unix >> = fun ( fd , peer ) ->
120118 let buffer = String. make (String. length token) '\000' in
121119 let io_vector = Lwt_unix. io_vector ~buffer ~offset: 0 ~length: (String. length buffer) in
122- lwt n, fds = Lwt_unix. recv_msg ~socket: fd ~io_vectors: [io_vector] in
120+ Lwt_unix. recv_msg ~socket: fd ~io_vectors: [io_vector] >> = fun ( n , fds ) ->
123121 List. iter Unix. close fds;
124122 let token' = String. sub buffer 0 n in
125123 let io_vector' = Lwt_unix. io_vector ~buffer: token' ~offset: 0 ~length: (String. length token') in
126124 if token = token'
127125 then
128- lwt _ = Lwt_unix. send_msg ~socket: fd ~io_vectors: [io_vector'] ~fds: [proxy_socket] in
129- return ()
126+ Lwt_unix. send_msg ~socket: fd ~io_vectors: [io_vector'] ~fds: [proxy_socket] >> = fun _ -> return ()
130127 else return () in
131- lwt () = Lwt. pick [ t_ip; t_unix ] in
128+ Lwt. pick [ t_ip; t_unix ] >> = fun () ->
132129 Unix. unlink path;
133130 return ()
134131
@@ -152,12 +149,12 @@ let advertise_cmd =
152149 Term. info " advertise" ~sdocs: _common_options ~doc ~man
153150
154151let connect_t common_options_t =
155- lwt advertisement = match_lwt Lwt_io. read_line_opt Lwt_io. stdin with None -> return " " | Some x -> return x in
152+ Lwt_io. read_line_opt Lwt_io. stdin >> = ( function | None -> return " " | Some x -> return x) >> = fun advertisement ->
156153 let open Xcp_channel in
157154 let fd = Lwt_unix. of_unix_file_descr (file_descr_of_t (t_of_rpc (Jsonrpc. of_string advertisement))) in
158155 let a = copy_all Lwt_unix. stdin fd in
159156 let b = copy_all fd Lwt_unix. stdout in
160- Lwt. join [a; b]
157+ Lwt. join [a; b]
161158
162159let connect common_options_t =
163160 Lwt_main. run(connect_t common_options_t);
@@ -172,15 +169,15 @@ let connect_cmd =
172169 Term. (ret(pure connect $ common_options_t)),
173170 Term. info " connect" ~sdocs: _common_options ~doc ~man
174171
175- let default_cmd =
176- let doc = " channel (file-descriptor) passing helper program" in
172+ let default_cmd =
173+ let doc = " channel (file-descriptor) passing helper program" in
177174 let man = help in
178175 Term. (ret (pure (fun _ -> `Help (`Pager , None )) $ common_options_t)),
179176 Term. info " proxy" ~version: " 1.0.0" ~sdocs: _common_options ~doc ~man
180-
177+
181178let cmds = [advertise_cmd; connect_cmd]
182179
183180let _ =
184- match Term. eval_choice default_cmd cmds with
181+ match Term. eval_choice default_cmd cmds with
185182 | `Error _ -> exit 1
186183 | _ -> exit 0
0 commit comments