From 9c694da00c7d93cbff3aa910ffdeeb8dded691c0 Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Wed, 7 Jun 2023 01:21:23 +0300 Subject: simplified binary expose_ws() --- kvmd/apps/kvmd/api/hid.py | 6 +++--- kvmd/htserver.py | 14 ++++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/kvmd/apps/kvmd/api/hid.py b/kvmd/apps/kvmd/api/hid.py index ea59d8cf..8143deff 100644 --- a/kvmd/apps/kvmd/api/hid.py +++ b/kvmd/apps/kvmd/api/hid.py @@ -154,7 +154,7 @@ class HidApi: # ===== - @exposed_ws(3, binary=True) + @exposed_ws(3) async def __ws_bin_mouse_move_handler(self, _: WsSession, data: bytes) -> None: try: (to_x, to_y) = struct.unpack(">hh", data) @@ -164,11 +164,11 @@ class HidApi: return self.__send_mouse_move_event(to_x, to_y) - @exposed_ws(4, binary=True) + @exposed_ws(4) async def __ws_bin_mouse_relative_handler(self, _: WsSession, data: bytes) -> None: self.__process_ws_bin_delta_request(data, self.__hid.send_mouse_relative_event) - @exposed_ws(5, binary=True) + @exposed_ws(5) async def __ws_bin_mouse_wheel_handler(self, _: WsSession, data: bytes) -> None: self.__process_ws_bin_delta_request(data, self.__hid.send_mouse_wheel_event) diff --git a/kvmd/htserver.py b/kvmd/htserver.py index ef2445f8..fa2aedc4 100644 --- a/kvmd/htserver.py +++ b/kvmd/htserver.py @@ -132,15 +132,10 @@ _WS_BINARY = "_ws_binary" _WS_EVENT_TYPE = "_ws_event_type" -def exposed_ws(event_type: (str | int), binary: bool=False) -> Callable: - if binary: - assert isinstance(event_type, int) - else: - assert isinstance(event_type, str) - +def exposed_ws(event_type: (str | int)) -> Callable: def set_attrs(handler: Callable) -> Callable: setattr(handler, _WS_EXPOSED, True) - setattr(handler, _WS_BINARY, binary) + setattr(handler, _WS_BINARY, isinstance(event_type, int)) setattr(handler, _WS_EVENT_TYPE, str(event_type)) return handler return set_attrs @@ -341,8 +336,11 @@ class HttpServer: def __add_exposed_ws(self, exposed: WsExposed) -> None: if exposed.binary: - self.__ws_bin_handlers[int(exposed.event_type)] = exposed.handler + event_type = int(exposed.event_type) + assert event_type not in self.__ws_bin_handlers + self.__ws_bin_handlers[event_type] = exposed.handler else: + assert exposed.event_type not in self.__ws_handlers self.__ws_handlers[exposed.event_type] = exposed.handler # ===== -- cgit v1.2.3