From 140e8897b1acb234d6537445132d735890399d3c Mon Sep 17 00:00:00 2001 From: haskal Date: Sun, 3 Oct 2021 21:55:25 -0400 Subject: [PATCH] retry rcon connection indefinitely --- factorio-bridge.service | 4 ++-- factorio_bridge.ml | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/factorio-bridge.service b/factorio-bridge.service index adf770f..f9df506 100644 --- a/factorio-bridge.service +++ b/factorio-bridge.service @@ -1,7 +1,7 @@ [Unit] Description=factorio-matrix bridge -Requires=factorio.service -After=factorio.service +#Requires=factorio.service +#After=factorio.service [Service] ExecStart=/opt/factorio-bridge/factorio_bridge.exe -config /opt/factorio-bridge/bridge.conf diff --git a/factorio_bridge.ml b/factorio_bridge.ml index 71b75d0..57f138b 100644 --- a/factorio_bridge.ml +++ b/factorio_bridge.ml @@ -168,9 +168,13 @@ let matrix_message_stream ~homeserver ~token ~(state : string option) let rcon_writer ~(host : string) ~(port : int) ~(password : string) : string Pipe.Writer.t = let reader_func pipe_reader = - let%bind (socket, reader, writer) = - Tcp.connect (Tcp.Where_to_connect.of_host_and_port {host; port;}) + let rec retry_conn () = + try_with (fun () -> Tcp.connect (Tcp.Where_to_connect.of_host_and_port {host; port;})) + >>= function + | Ok x -> return x + | Error _ -> let%bind () = Clock.after (Time.Span.of_sec 10.) in retry_conn () in + let%bind (socket, reader, writer) = retry_conn () in ignore socket; let send_packet (id : int) (typ : int) (body : string) = let body = String.concat [body; "\x00\x00"] in