Keep alive system

This commit is contained in:
Emily 2020-03-30 00:38:02 +02:00
parent f55275a627
commit e9ef282c95
4 changed files with 26 additions and 2 deletions

View File

@ -25,8 +25,10 @@ class Client:
if self.state == STATE_UNKNOWN: 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 # 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"): if packet.startswith(b"SAMP"):
await self.client.on_state_change()
self.set_state(STATE_QUERY) self.set_state(STATE_QUERY)
else: else:
await self.client.on_state_change()
self.set_state(STATE_PLAYER) self.set_state(STATE_PLAYER)
await self.client.on_packet(packet) await self.client.on_packet(packet)

View File

@ -1,9 +1,13 @@
import socket import socket
import struct import struct
import asyncio
from time import time
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
TIMEOUT = 30 # assume connection is closed after 30 seconds if inactivity
class BaseClient: class BaseClient:
def __init__(self, server: "__ServerInstance__", ip: str, port: int): def __init__(self, server: "__ServerInstance__", ip: str, port: int):
self.server = server self.server = server
@ -12,9 +16,26 @@ class BaseClient:
self.ip_uint, = struct.unpack(b"<I", bytes(int(x) for x in self.ip.split("."))) self.ip_uint, = struct.unpack(b"<I", bytes(int(x) for x in self.ip.split(".")))
self.last_active = time()
self.keep_alive_task = asyncio.create_task( self.keep_alive() )
self.connected = True # keep_alive will set this to False if connection has not been interacted with for a while (allowing server loop to remove their reference)
async def on_packet(self, packet: bytes): async def on_packet(self, packet: bytes):
logger.debug("on_packet(%s)" % packet) logger.debug("on_packet(%s)" % packet)
self.last_active = time()
async def send(self, packet: bytes): async def send(self, packet: bytes):
sock: socket.socket = self.server.socket sock: socket.socket = self.server.socket
sock.sendto(packet, (self.ip, self.port)) sock.sendto(packet, (self.ip, self.port))
async def on_state_change(self): # Stop the keep alive task as the whole class is being replaced, TODO: Find out if this could be automated with a magic method
self.keep_alive_task.cancel()
async def keep_alive(self):
while True:
timestamp = time()
if self.last_active + TIMEOUT - timestamp < 0:
self.connected = False
return
await asyncio.sleep(self.last_active + TIMEOUT - timestamp)

View File

@ -22,7 +22,7 @@ class QueryClient(BaseClient):
} }
async def on_packet(self, packet: bytes): async def on_packet(self, packet: bytes):
logger.debug("on_packet(%s)" % packet) await super().on_packet(packet)
if len(packet) <= 10: # Invalid if len(packet) <= 10: # Invalid
return return

View File

@ -12,6 +12,7 @@ class Server:
def __init__(self, config: ServerConfig): def __init__(self, config: ServerConfig):
self.config = config self.config = config
self.clients = {} self.clients = {}
self.rcon_clients = {}
async def create_socket(self): async def create_socket(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)