Browse Source

[server] implement --wait-connect (see vshaxe/vshaxe#217) (#8730)

* [server] implement --wait-connect (see vshaxe/vshaxe#217)

* [CLI] remove rogue parens

* [CLI] change to --server-listen and --server-connect
Dan Korostelev 6 years ago
parent
commit
196bdf7cd8
2 changed files with 39 additions and 18 deletions
  1. 14 5
      src/compiler/main.ml
  2. 25 13
      src/compiler/server.ml

+ 14 - 5
src/compiler/main.ml

@@ -619,6 +619,11 @@ let check_auxiliary_output com xml_out json_out =
 			Genjson.generate com.types file
 	end
 
+let parse_host_port hp =
+	let host, port = (try ExtString.String.split hp ":" with _ -> "127.0.0.1", hp) in
+	let port = try int_of_string port with _ -> raise (Arg.Bad "Invalid port") in
+	host, port
+
 let rec process_params create pl =
 	let each_params = ref [] in
 	let rec loop acc = function
@@ -915,20 +920,24 @@ try
 			force_typing := true;
 			config_macros := e :: !config_macros
 		),"<macro>","call the given macro before typing anything else");
-		("Compilation Server",["--wait"],[], Arg.String (fun hp ->
+		("Compilation Server",["--server-listen"],["--wait"], Arg.String (fun hp ->
 			let accept = match hp with
 				| "stdio" ->
 					Server.init_wait_stdio()
 				| _ ->
-					let host, port = (try ExtString.String.split hp ":" with _ -> "127.0.0.1", hp) in
-					let port = try int_of_string port with _ -> raise (Arg.Bad "Invalid port") in
+					let host, port = parse_host_port hp in
 					init_wait_socket host port
 			in
 			wait_loop process_params com.verbose accept
-		),"[[host:]port]|stdio]","wait on the given port (or use standard i/o) for commands to run)");
+		),"[[host:]port]|stdio]","wait on the given port (or use standard i/o) for commands to run");
+		("Compilation Server",["--server-connect"],[], Arg.String (fun hp ->
+			let host, port = parse_host_port hp in
+			let accept = Server.init_wait_connect host port in
+			wait_loop process_params com.verbose accept
+		),"[host:]port]","connect to the given port and wait for commands to run");
 		("Compilation Server",["--connect"],[],Arg.String (fun _ ->
 			assert false
-		),"<[host:]port>","connect on the given port and run commands there)");
+		),"<[host:]port>","connect on the given port and run commands there");
 		("Compilation",["-C";"--cwd"],[], Arg.String (fun dir ->
 			assert false
 		),"<dir>","set current working directory");

+ 25 - 13
src/compiler/server.ml

@@ -636,29 +636,41 @@ let wait_loop process_params verbose accept =
 		end else Gc.minor();
 	done
 
-(* The accept-function to wait for a stdio connection. *)
-let init_wait_stdio() =
-	set_binary_mode_in stdin true;
-	set_binary_mode_out stderr true;
+let mk_length_prefixed_communication chin chout =
+	let chin = IO.input_channel chin in
+	let chout = IO.output_channel chout in
 
-	let chin = IO.input_channel stdin in
-	let cherr = IO.output_channel stderr in
+	let bout = Buffer.create 0 in
 
-	let berr = Buffer.create 0 in
 	let read = fun () ->
 		let len = IO.read_i32 chin in
 		IO.really_nread_string chin len
 	in
-	let write = Buffer.add_string berr in
+
+	let write = Buffer.add_string bout in
+
 	let close = fun() ->
-		IO.write_i32 cherr (Buffer.length berr);
-		IO.nwrite_string cherr (Buffer.contents berr);
-		IO.flush cherr
+		IO.write_i32 chout (Buffer.length bout);
+		IO.nwrite_string chout (Buffer.contents bout);
+		IO.flush chout
 	in
-	fun() ->
-		Buffer.clear berr;
+
+	fun () ->
+		Buffer.clear bout;
 		read, write, close
 
+(* The accept-function to wait for a stdio connection. *)
+let init_wait_stdio() =
+	set_binary_mode_in stdin true;
+	set_binary_mode_out stderr true;
+	mk_length_prefixed_communication stdin stderr
+
+(* Connect to given host/port and return accept function for communication *)
+let init_wait_connect host port =
+	let host = Unix.inet_addr_of_string host in
+	let chin, chout = Unix.open_connection (Unix.ADDR_INET (host,port)) in
+	mk_length_prefixed_communication chin chout
+
 (* The accept-function to wait for a socket connection. *)
 let init_wait_socket host port =
 	let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in