From 8e59db2ad67164eaf8659c1e84bfde55e97a41d9 Mon Sep 17 00:00:00 2001 From: Sunpy Date: Wed, 15 Mar 2023 07:27:33 +0100 Subject: [PATCH] Interactive server console --- sampy/__main__.py | 4 ++-- sampy/server.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/sampy/__main__.py b/sampy/__main__.py index b700fbb..7eefca9 100644 --- a/sampy/__main__.py +++ b/sampy/__main__.py @@ -5,9 +5,9 @@ import textwrap def main(args: argparse.Namespace) -> int: from sampy.network.protocol import Protocol - from sampy.server import Server + from sampy.server import InteractiveServer - server = Server(Protocol) + server = InteractiveServer(Protocol) server.start() asyncio.get_event_loop().run_forever() diff --git a/sampy/server.py b/sampy/server.py index 03ddc21..4e0f44c 100644 --- a/sampy/server.py +++ b/sampy/server.py @@ -34,6 +34,9 @@ class UDPProtocol: def connection_made(self, transport: asyncio.transports.DatagramTransport): self.transport = transport + def connection_lost(self, exc: Exception | None): + pass + def datagram_received(self, data: bytes, addr: Tuple[str, int]): raise NotImplementedError @@ -61,3 +64,20 @@ class Server(UDPProtocol): @property def players(self) -> list[Player]: # TODO return [] + + +class InteractiveServer(Server): + def __init__(self, protocol: Type[Protocol], config: Config = Config()): + super().__init__(protocol=protocol, config=config) + loop = asyncio.get_event_loop() + loop.create_task(self.run_input_loop()) + + async def run_input_loop(self): + loop = asyncio.get_event_loop() + while True: + command = await loop.run_in_executor(None, input) + + if command in ("quit", "exit", "stop"): + self.stop() + loop.stop() + return