From e9ef282c95a209ff7a8ea3d25b82ef6862ffcbfd Mon Sep 17 00:00:00 2001 From: Sunpy Date: Mon, 30 Mar 2020 00:38:02 +0200 Subject: [PATCH] Keep alive system --- sampy/client/__init__.py | 2 ++ sampy/client/base.py | 23 ++++++++++++++++++++++- sampy/client/query.py | 2 +- sampy/server.py | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/sampy/client/__init__.py b/sampy/client/__init__.py index 6ce789c..e058fe3 100644 --- a/sampy/client/__init__.py +++ b/sampy/client/__init__.py @@ -25,8 +25,10 @@ class Client: if self.state == STATE_UNKNOWN: # We are currently unaware if this is a player client or query client, but we got a packet that will be our check to know if packet.startswith(b"SAMP"): + await self.client.on_state_change() self.set_state(STATE_QUERY) else: + await self.client.on_state_change() self.set_state(STATE_PLAYER) await self.client.on_packet(packet) diff --git a/sampy/client/base.py b/sampy/client/base.py index 2976bdb..02524d7 100644 --- a/sampy/client/base.py +++ b/sampy/client/base.py @@ -1,9 +1,13 @@ import socket import struct +import asyncio +from time import time import logging logger = logging.getLogger(__name__) +TIMEOUT = 30 # assume connection is closed after 30 seconds if inactivity + class BaseClient: def __init__(self, server: "__ServerInstance__", ip: str, port: int): self.server = server @@ -11,10 +15,27 @@ class BaseClient: self.port = port self.ip_uint, = struct.unpack(b"