diff --git a/DMBotNetwork/__init__.py b/DMBotNetwork/__init__.py index 2aa9e82..d269bf8 100644 --- a/DMBotNetwork/__init__.py +++ b/DMBotNetwork/__init__.py @@ -3,4 +3,4 @@ from .main.utils.cl_unit import ClUnit __all__ = ["Client", "Server", "ClUnit"] -__version__ = "0.2.0" +__version__ = "0.2.1" diff --git a/DMBotNetwork/main/client.py b/DMBotNetwork/main/client.py index ab378f1..653998d 100644 --- a/DMBotNetwork/main/client.py +++ b/DMBotNetwork/main/client.py @@ -155,6 +155,9 @@ async def disconnect(cls) -> None: cls._server_handler_task.cancel() cls._server_handler_task = None + cls._writer = None + cls._reader = None + download_files = cls._content_path.glob("**/*.download") for file in download_files: file.unlink() @@ -194,6 +197,7 @@ async def _server_handler(cls) -> None: asyncio.CancelledError, ConnectionAbortedError, asyncio.exceptions.IncompleteReadError, + ConnectionResetError, ): pass diff --git a/DMBotNetwork/main/server.py b/DMBotNetwork/main/server.py index cc3c3a1..acac18a 100644 --- a/DMBotNetwork/main/server.py +++ b/DMBotNetwork/main/server.py @@ -20,6 +20,7 @@ class Server: _server_name: str = "Dev_Server" _allow_registration: bool = True _timeout: float = 30.0 + _max_players: int = -1 @classmethod def register_methods_from_class(cls, external_class): @@ -79,10 +80,12 @@ async def setup_server( base_access: Dict[str, bool], allow_registration: bool, timeout: float, + max_player: int = -1, # inf ) -> None: cls._server_name = server_name cls._allow_registration = allow_registration cls._timeout = timeout + cls._max_players = max_player ServerDB.set_db_path(db_path) ServerDB.set_owner_base_password(init_owner_password) @@ -197,6 +200,14 @@ async def _cl_handler( else: await cl_unit.send_log_error("Unknown 'code' for net type.") + except ( + asyncio.CancelledError, + ConnectionAbortedError, + asyncio.exceptions.IncompleteReadError, + ConnectionResetError, + ): + pass + except Exception as err: await cl_unit.send_log_error(f"An unexpected error occurred: {err}") @@ -207,6 +218,9 @@ async def _cl_handler( @classmethod async def _auth(cls, cl_unit: ClUnit) -> None: + if cls._max_players != -1 and cls._max_players <= len(cls._cl_units): + raise ValueError("Server is full.") + await cl_unit.send_package(ResponseCode.AUTH_REQ) receive_package = await asyncio.wait_for( cl_unit.receive_package(), cls._timeout diff --git a/server_test.py b/server_test.py index 1e2006f..a52c3fe 100644 --- a/server_test.py +++ b/server_test.py @@ -19,6 +19,7 @@ async def main(): base_access={}, allow_registration=False, timeout=5.0, + max_player=0, ) Server.register_methods_from_class(NetClassPong) diff --git a/setup.py b/setup.py index fb38db9..79927a8 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="DMBotNetwork", - version="0.2.0", + version="0.2.1", packages=find_packages(), install_requires=["aiosqlite", "aiofiles", "bcrypt", "msgpack"], author="Angels And Demons dev team", diff --git a/test.py b/test.py deleted file mode 100644 index e69de29..0000000