CommonClient: implement active keep-alive

This commit is contained in:
Fabian Dill 2021-09-11 03:59:12 +02:00
parent 494cfb3c04
commit b1196885d7
1 changed files with 20 additions and 5 deletions

View File

@ -17,6 +17,7 @@ logger = logging.getLogger("Client")
gui_enabled = Utils.is_frozen() or "--nogui" not in sys.argv
class ClientCommandProcessor(CommandProcessor):
def __init__(self, ctx: CommonContext):
self.ctx = ctx
@ -86,11 +87,12 @@ class ClientCommandProcessor(CommandProcessor):
class CommonContext():
starting_reconnect_delay = 5
current_reconnect_delay = starting_reconnect_delay
command_processor = ClientCommandProcessor
game: None
ui: None
starting_reconnect_delay: int = 5
current_reconnect_delay: int = starting_reconnect_delay
command_processor: int = ClientCommandProcessor
game = None
ui = None
keep_alive_task = None
def __init__(self, server_address, password):
# server state
@ -127,6 +129,9 @@ class CommonContext():
self.jsontotextparser = JSONtoTextParser(self)
self.set_getters(network_data_package)
# execution
self.keep_alive_task = asyncio.create_task(keep_alive(self))
async def connection_closed(self):
self.auth = None
self.items_received = []
@ -219,6 +224,16 @@ class CommonContext():
pass
async def keep_alive(ctx: CommonContext):
""" some ISPs/network configurations drop TCP connections if no payload is sent (ignore TCP-keep-alive)
so we send a payload to prevent drop and if we were dropped anyway this will cause a reconnect."""
while not ctx.exit_event.is_set():
await asyncio.sleep(100)
if ctx.server and ctx.slot:
await ctx.send_msgs([{"cmd": "Bounce", "slots": [ctx.slot]}])
logger.info("Bouncy")
async def server_loop(ctx: CommonContext, address=None):
cached_address = None
if ctx.server and ctx.server.socket: