Factorio: Allow to reconnect a timed out RCON client connection. (#5421)

This commit is contained in:
CaitSith2
2025-09-21 18:07:33 -07:00
committed by GitHub
parent fb9011da63
commit e256abfdfb

View File

@@ -60,6 +60,19 @@ class FactorioCommandProcessor(ClientCommandProcessor):
"""Toggle sending of chat messages from players on the Factorio server to Archipelago."""
self.ctx.toggle_bridge_chat_out()
def _cmd_rcon_reconnect(self) -> bool:
"""Reconnect the RCON client if its disconnected."""
try:
result = self.ctx.rcon_client.send_command("/help")
if result:
self.output("RCON Client already connected.")
return True
except factorio_rcon.RCONNetworkError:
self.ctx.rcon_client = factorio_rcon.RCONClient("localhost", self.ctx.rcon_port, self.ctx.rcon_password, timeout=5)
self.output("RCON Client successfully reconnected.")
return True
return False
class FactorioContext(CommonContext):
command_processor = FactorioCommandProcessor
@@ -242,7 +255,13 @@ async def game_watcher(ctx: FactorioContext):
if ctx.rcon_client and time.perf_counter() > next_bridge:
next_bridge = time.perf_counter() + 1
ctx.awaiting_bridge = False
try:
data = json.loads(ctx.rcon_client.send_command("/ap-sync"))
except factorio_rcon.RCONNotConnected:
continue
except factorio_rcon.RCONNetworkError:
bridge_logger.warning("RCON Client has unexpectedly lost connection. Please issue /rcon_reconnect.")
continue
if not ctx.auth:
pass # auth failed, wait for new attempt
elif data["slot_name"] != ctx.auth:
@@ -294,8 +313,12 @@ async def game_watcher(ctx: FactorioContext):
"cmd": "Set", "key": ctx.energylink_key, "operations":
[{"operation": "add", "value": value}]
}]))
try:
ctx.rcon_client.send_command(
f"/ap-energylink -{value}")
except factorio_rcon.RCONNetworkError:
bridge_logger.warning("RCON Client has unexpectedly lost connection. Please issue /rcon_reconnect.")
else:
logger.debug(f"EnergyLink: Sent {format_SI_prefix(value)}J")
await asyncio.sleep(0.1)