From 6f34ca0b39146e77922a3cc61466470dddf9f980 Mon Sep 17 00:00:00 2001 From: chr Date: Sun, 29 Mar 2020 01:55:41 +0000 Subject: [PATCH] fix binary backoff, add backoff for minecraft wrapper connection --- ServerWrapper_v0.1.0.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/ServerWrapper_v0.1.0.py b/ServerWrapper_v0.1.0.py index 8e7d4a1..a651f0c 100644 --- a/ServerWrapper_v0.1.0.py +++ b/ServerWrapper_v0.1.0.py @@ -121,8 +121,8 @@ class socket_util(object): class MinecraftWrapper(socket_util): def __init__(self, command, host, port): - super().__init__(host, port) self.logger = LOG.getChild("MinecraftWrapper") + super().__init__(host, port) self.logger.debug(command) self.command = command self.logger.info("Starting Wrapper Polling Thread") @@ -134,7 +134,22 @@ class MinecraftWrapper(socket_util): def socket_reset(self): super().socket_reset() self.logger.debug("Connecting to {}:{}".format(self.host, self.port)) - self.soc.connect((self.host, self.port)) + backoff = 1 + while True: + try: + self.soc.connect((self.host, self.port)) + break + except OSError as e: + if e.errno == 111: + LOG.warning( + "Connection refused by {}:{}, trying again in {} seconds".format( + self.host, self.port, backoff + ) + ) + time.sleep(backoff) + backoff *= 2 + else: + raise e self.logger.info("Socket Connected") def exe_mc(self): @@ -216,8 +231,8 @@ class MinecraftServerBridge(socket_util): def socket_reset(self): super().socket_reset() LOG.info("Server Binding to " + self.host + " " + str(self.port)) + backoff = 1 while True: - backoff = 1 try: self.soc.bind((self.host, self.port)) break