From e3712b071c1978ad9324ec1de62e721903344edb Mon Sep 17 00:00:00 2001 From: themanyfaceddemon Date: Wed, 2 Oct 2024 03:57:05 +0300 Subject: [PATCH 1/5] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B2=20setup=20=D0=BF=D0=BE=D0=B4=20=D0=B4?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0=D0=BC=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D1=83?= =?UTF-8?q?=D1=8E=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8E=20=D0=B8=D0=B1?= =?UTF-8?q?=D0=BE=20=D0=B7=D0=B0=D0=B5=D0=B1=D0=B0=D0=BB=D0=BE=20=D1=83?= =?UTF-8?q?=D0=B6=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMBotNetwork/__init__.py | 2 +- setup.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/DMBotNetwork/__init__.py b/DMBotNetwork/__init__.py index b848945..8ea3f3d 100644 --- a/DMBotNetwork/__init__.py +++ b/DMBotNetwork/__init__.py @@ -5,4 +5,4 @@ from .main.utils.server_db import ServerDB __all__ = ["Client", "Server", "ClUnit", "require_access", "ServerDB"] -__version__ = "0.3.0" +__version__ = "0.3.1" diff --git a/setup.py b/setup.py index 71785ee..2408b41 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,10 @@ from setuptools import find_packages, setup +from DMBotNetwork import __version__ + setup( name="DMBotNetwork", - version="0.3.0", + version=__version__, packages=find_packages(), install_requires=["aiosqlite", "aiofiles", "bcrypt", "msgpack"], author="Angels And Demons dev team", From 79ab093c7e453bcee74400bdcc331bf0eb8b8b59 Mon Sep 17 00:00:00 2001 From: themanyfaceddemon Date: Wed, 2 Oct 2024 03:57:20 +0300 Subject: [PATCH 2/5] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=B0=D0=B3=D0=B0.=200=20?= =?UTF-8?q?=D1=83=20=D0=BD=D0=B0=D1=81=20=D0=BD=D0=B5=20=D1=81=D1=87=D0=B8?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20=D0=B1=D0=BB=D1=8F=D1=82?= =?UTF-8?q?=D1=8C=20=D0=B7=D0=B0=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMBotNetwork/main/utils/response_code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DMBotNetwork/main/utils/response_code.py b/DMBotNetwork/main/utils/response_code.py index 69f44a9..7baa170 100644 --- a/DMBotNetwork/main/utils/response_code.py +++ b/DMBotNetwork/main/utils/response_code.py @@ -3,7 +3,7 @@ class ResponseCode(IntEnum): # Системные члены - DISCONNECT = 0 + DISCONNECT = 1 # Авторизация AUTH_REQ = 10 # Запрос авторизации от сервера From d4ae08a752694b01048833c340b2e082088311d8 Mon Sep 17 00:00:00 2001 From: themanyfaceddemon Date: Wed, 2 Oct 2024 03:57:35 +0300 Subject: [PATCH 3/5] =?UTF-8?q?=D0=A4=D0=BE=D1=80=D0=BC=D0=B0=D1=82=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=BC=D0=B5=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMBotNetwork/main/server.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/DMBotNetwork/main/server.py b/DMBotNetwork/main/server.py index 8f5b038..34b31b7 100644 --- a/DMBotNetwork/main/server.py +++ b/DMBotNetwork/main/server.py @@ -3,17 +3,8 @@ import logging from collections.abc import Callable from pathlib import Path -from typing import ( - Any, - Dict, - List, - Optional, - Type, - Union, - get_args, - get_origin, - get_type_hints, -) +from typing import (Any, Dict, List, Optional, Type, Union, get_args, + get_origin, get_type_hints) from .utils import ClUnit, ResponseCode, ServerDB @@ -221,12 +212,18 @@ async def stop(cls) -> None: logger.info("Server stop.") @classmethod - async def broadcast(cls, func_name: str, cl_units_dict: Optional[Dict[str, ClUnit]] = None, *args, **kwargs) -> None: + async def broadcast( + cls, + func_name: str, + cl_units_dict: Optional[Dict[str, ClUnit]] = None, + *args, + **kwargs, + ) -> None: tasks = [] - + if cl_units_dict is None: cl_units_dict = cls._cl_units - + for cl_unit in cl_units_dict.values(): func = getattr(cl_unit, func_name, None) if callable(func): From 7a7b7ad86aec284180aacab4267ca7efc26deacc Mon Sep 17 00:00:00 2001 From: themanyfaceddemon Date: Wed, 2 Oct 2024 03:58:21 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20API=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20access=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D0=BE=D0=B1=D1=80=D0=B0=D1=89=D0=B0=D1=8F=D1=81=D1=8C?= =?UTF-8?q?=20=D0=BF=D0=BE=2030=20=D1=80=D0=B0=D0=B7=20=D0=BA=20=D1=81?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMBotNetwork/main/client.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/DMBotNetwork/main/client.py b/DMBotNetwork/main/client.py index 16ce2ea..14e27c7 100644 --- a/DMBotNetwork/main/client.py +++ b/DMBotNetwork/main/client.py @@ -35,6 +35,7 @@ class Client: _password: str = "owner_password" _use_registration: bool = False _content_path: Path = Path("") + _access: Dict[str, bool] = {} _callback_on_disconect: Optional[ Callable[[Optional[str]], Awaitable[None]] | Callable[[Optional[str]], None] @@ -151,6 +152,10 @@ def get_server_name(cls) -> str: def get_login(cls) -> str: return cls._login + @classmethod + def get_access(cls) -> Dict[str, bool]: + return cls._access.copy() + @classmethod def set_callback_on_disconect( cls, @@ -228,6 +233,7 @@ async def disconnect(cls) -> None: cls._writer = None cls._reader = None + cls._access = {} download_files = cls._content_path.glob("**/*.download") for file in download_files: @@ -339,6 +345,10 @@ async def _auth_handler(cls, code: int, receive_package: dict) -> None: cls._state = ClientState.AUTHORIZED cls._server_name = server_name + cls._access = await cls.req_get_data( + "get_access", None, login=cls._login + ) # По хорошему я get_access должен вынести в сервер, но мне похуй. # WARNING! + @classmethod async def _file_handler(cls, code: int, receive_package: dict) -> None: if code == ResponseCode.FIL_REQ: From 591d978b7a4b016ad29cfb584fcc157e78845372 Mon Sep 17 00:00:00 2001 From: themanyfaceddemon Date: Wed, 2 Oct 2024 03:59:03 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BB=D0=B1=D0=B5=D0=BA=D0=B0=20on=5Fdisconect=20=D0=BA=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D1=8B=D0=B9=20=D0=BD=D0=B5=20=D0=B2=D1=8B?= =?UTF-8?q?=D0=B7=D1=8B=D0=B2=D0=B0=D0=BB=D1=81=D1=8F=20=D0=B2=20=D1=80?= =?UTF-8?q?=D1=8F=D0=B4=D0=B5=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B5=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMBotNetwork/main/client.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/DMBotNetwork/main/client.py b/DMBotNetwork/main/client.py index 14e27c7..4477944 100644 --- a/DMBotNetwork/main/client.py +++ b/DMBotNetwork/main/client.py @@ -209,13 +209,26 @@ async def connect(cls, host, port) -> None: except Exception as err: logger.error(f"Error while connect to sever: {err}") - await cls.disconnect() + await cls.disconnect(str(err)) @classmethod - async def disconnect(cls) -> None: + async def _on_disconect(cls, reason: Optional[str] = None) -> None: + if cls._callback_on_disconect is None: + return + + if inspect.iscoroutinefunction(cls._callback_on_disconect): + await cls._callback_on_disconect(reason) + + else: + cls._callback_on_disconect(reason) + + @classmethod + async def disconnect(cls, reason: Optional[str] = None) -> None: async with cls._disconnect_lock: cls._state = ClientState.DISCONNECTED + await cls._on_disconect(reason) + if cls._writer: try: cls._writer.close() @@ -243,6 +256,8 @@ async def disconnect(cls) -> None: @classmethod async def _server_handler(cls) -> None: + reason = None + try: while not cls._state & ClientState.DISCONNECTED: receive_package = await cls._receive_package() @@ -254,7 +269,6 @@ async def _server_handler(cls) -> None: if code == ResponseCode.DISCONNECT: reason = receive_package.pop("reason", None) - await cls._on_disconect(reason) break if code == ResponseCode.NET_REQ: @@ -293,18 +307,7 @@ async def _server_handler(cls) -> None: logger.error(str(err)) finally: - await cls.disconnect() - - @classmethod - async def _on_disconect(cls, reason: Optional[str] = None) -> None: - if cls._callback_on_disconect is None: - return - - if inspect.iscoroutinefunction(cls._callback_on_disconect): - await cls._callback_on_disconect(reason) - - else: - cls._callback_on_disconect(reason) + await cls.disconnect(reason) @classmethod def _log_handler(cls, code: int, receive_package: dict) -> None: