@@ -602,7 +602,7 @@ type servercmd =
602
602
let serverCmds = ref (Util.StringMap. empty : servercmd Util.StringMap.t )
603
603
604
604
type serverstream =
605
- connection -> Bytearray .t -> unit
605
+ connection option -> Bytearray .t -> unit
606
606
let serverStreams = ref (Util.StringMap. empty : serverstream Util.StringMap.t )
607
607
608
608
type header =
@@ -660,7 +660,7 @@ let processStream conn id cmdName buf =
660
660
try Util.StringMap. find cmdName ! serverStreams
661
661
with Not_found -> raise (Util. Fatal (cmdName ^ " not registered!" ))
662
662
in
663
- cmd conn buf;
663
+ cmd ( Some conn) buf;
664
664
Lwt. return ()
665
665
with e ->
666
666
Hashtbl. add streamError id e;
@@ -789,6 +789,17 @@ let registerServerCmd name f =
789
789
registerSpecialServerCmd
790
790
name defaultMarshalingFunctions defaultMarshalingFunctions f
791
791
792
+ (* Same as [registerServerCmd] but returns a function that runs either
793
+ the proxy or the local version, depending on whether the call is to
794
+ the local host (in this case [conn] is None) or a remote one. *)
795
+ let registerServerCmd' name f =
796
+ let serverSide = (fun conn args -> f (Some conn) args) in
797
+ let client0 = registerServerCmd name serverSide in
798
+ fun conn args ->
799
+ match conn with
800
+ | None -> f None args
801
+ | Some conn -> client0 conn args
802
+
792
803
(* RegisterHostCmd is a simpler version of registerClientServer [registerServerCmd?].
793
804
It is used to create remote procedure calls: the only communication
794
805
between the client and server is the sending of arguments from
@@ -826,16 +837,16 @@ let registerRootCmd (cmdName : string) (cmd : (Fspath.t * 'a) -> 'b) =
826
837
fun root args -> r (hostOfRoot root) ((snd root), args)
827
838
828
839
let registerRootCmdWithConnection
829
- (cmdName : string ) (cmd : connection -> 'a -> 'b ) =
830
- let client0 = registerServerCmd cmdName cmd in
840
+ (cmdName : string ) (cmd : connection option -> 'a -> 'b ) =
841
+ let serverSide = (fun conn args -> cmd (Some conn) args) in
842
+ let client0 = registerServerCmd cmdName serverSide in
831
843
(* Return a function that runs either the proxy or the local version,
832
844
depending on whether the call is to the local host or a remote one *)
833
845
fun localRoot remoteRoot args ->
834
- match (hostOfRoot localRoot) with
835
- "" -> let conn = hostConnection (hostOfRoot remoteRoot) in
836
- cmd conn args
837
- | _ -> let conn = hostConnection (hostOfRoot localRoot) in
838
- client0 conn args
846
+ match hostOfRoot localRoot, hostOfRoot remoteRoot with
847
+ | "" , "" -> cmd None args
848
+ | "" , _ -> cmd (Some (connectionToRoot remoteRoot)) args
849
+ | _ -> client0 (connectionToRoot localRoot) args
839
850
840
851
let streamReg = Lwt_util. make_region 1
841
852
@@ -849,12 +860,12 @@ let streamingActivated =
849
860
let registerStreamCmd
850
861
(cmdName : string )
851
862
marshalingFunctionsArgs
852
- (serverSide : connection -> 'a -> unit )
863
+ (serverSide : connection option -> 'a -> unit )
853
864
=
854
865
let cmd =
855
866
registerSpecialServerCmd
856
867
cmdName marshalingFunctionsArgs defaultMarshalingFunctions
857
- (fun conn v -> serverSide conn v; Lwt. return () )
868
+ (fun conn v -> serverSide ( Some conn) v; Lwt. return () )
858
869
in
859
870
let ping =
860
871
registerServerCmd (cmdName ^ " Ping" )
@@ -889,7 +900,7 @@ let registerStreamCmd
889
900
in
890
901
dumpIdle conn request
891
902
in
892
- fun conn sender ->
903
+ let proxy conn sender =
893
904
if not (Prefs. read streamingActivated) then
894
905
sender (fun v -> cmd conn v)
895
906
else begin
@@ -905,6 +916,11 @@ let registerStreamCmd
905
916
Util. msg " Pinging remote end after streaming error\n " );
906
917
ping conn id >> = fun () -> Lwt. fail e)
907
918
end
919
+ in
920
+ fun conn sender ->
921
+ match conn with
922
+ | None -> sender (fun v -> Lwt. return (serverSide conn v))
923
+ | Some conn -> proxy conn sender
908
924
909
925
let commandAvailable =
910
926
registerRootCmd " commandAvailable"
0 commit comments