CommonClient: implement active keep-alive
This commit is contained in:
		
							parent
							
								
									494cfb3c04
								
							
						
					
					
						commit
						b1196885d7
					
				| 
						 | 
					@ -17,6 +17,7 @@ logger = logging.getLogger("Client")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
gui_enabled = Utils.is_frozen() or "--nogui" not in sys.argv
 | 
					gui_enabled = Utils.is_frozen() or "--nogui" not in sys.argv
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ClientCommandProcessor(CommandProcessor):
 | 
					class ClientCommandProcessor(CommandProcessor):
 | 
				
			||||||
    def __init__(self, ctx: CommonContext):
 | 
					    def __init__(self, ctx: CommonContext):
 | 
				
			||||||
        self.ctx = ctx
 | 
					        self.ctx = ctx
 | 
				
			||||||
| 
						 | 
					@ -86,11 +87,12 @@ class ClientCommandProcessor(CommandProcessor):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CommonContext():
 | 
					class CommonContext():
 | 
				
			||||||
    starting_reconnect_delay = 5
 | 
					    starting_reconnect_delay: int = 5
 | 
				
			||||||
    current_reconnect_delay = starting_reconnect_delay
 | 
					    current_reconnect_delay: int = starting_reconnect_delay
 | 
				
			||||||
    command_processor = ClientCommandProcessor
 | 
					    command_processor: int = ClientCommandProcessor
 | 
				
			||||||
    game: None
 | 
					    game = None
 | 
				
			||||||
    ui: None
 | 
					    ui = None
 | 
				
			||||||
 | 
					    keep_alive_task = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, server_address, password):
 | 
					    def __init__(self, server_address, password):
 | 
				
			||||||
        # server state
 | 
					        # server state
 | 
				
			||||||
| 
						 | 
					@ -127,6 +129,9 @@ class CommonContext():
 | 
				
			||||||
        self.jsontotextparser = JSONtoTextParser(self)
 | 
					        self.jsontotextparser = JSONtoTextParser(self)
 | 
				
			||||||
        self.set_getters(network_data_package)
 | 
					        self.set_getters(network_data_package)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # execution
 | 
				
			||||||
 | 
					        self.keep_alive_task = asyncio.create_task(keep_alive(self))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def connection_closed(self):
 | 
					    async def connection_closed(self):
 | 
				
			||||||
        self.auth = None
 | 
					        self.auth = None
 | 
				
			||||||
        self.items_received = []
 | 
					        self.items_received = []
 | 
				
			||||||
| 
						 | 
					@ -219,6 +224,16 @@ class CommonContext():
 | 
				
			||||||
        pass
 | 
					        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):
 | 
					async def server_loop(ctx: CommonContext, address=None):
 | 
				
			||||||
    cached_address = None
 | 
					    cached_address = None
 | 
				
			||||||
    if ctx.server and ctx.server.socket:
 | 
					    if ctx.server and ctx.server.socket:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue