diff options
| author | Jonas Dellinger <jonas.dellinger@2trde.com> | 2022-06-16 18:33:43 +0200 |
|---|---|---|
| committer | Jonas Dellinger <jonas.dellinger@2trde.com> | 2022-06-16 18:33:43 +0200 |
| commit | 0a12fe6102da33977548ba0c277bd4fe34e262ab (patch) | |
| tree | 7ff803d0d106db43ce206a6cdfc74c187f0d901a /backend/plugin_protocol.py | |
| parent | a95bf94d878f61869895bb22cbff1b4f524c5dca (diff) | |
| download | decky-loader-0a12fe6102da33977548ba0c277bd4fe34e262ab.tar.gz decky-loader-0a12fe6102da33977548ba0c277bd4fe34e262ab.zip | |
First draft of backend independent plugins
Diffstat (limited to 'backend/plugin_protocol.py')
| -rw-r--r-- | backend/plugin_protocol.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/backend/plugin_protocol.py b/backend/plugin_protocol.py new file mode 100644 index 00000000..445a1e3a --- /dev/null +++ b/backend/plugin_protocol.py @@ -0,0 +1,46 @@ +import json +import uuid +from asyncio import Protocol, TimeoutError, get_event_loop, wait_for +from gc import callbacks +from subprocess import call + + +class PluginProtocolServer(Protocol): + def __init__(self, backend) -> None: + super().__init__() + self.backend = backend + self.callbacks = {} + + def connection_made(self, transport): + self.transport = transport + + def data_received(self, data: bytes) -> None: + message = json.loads(data.decode("utf-8")) + message_id = str(uuid.UUID(message["id"])) + message_type = message["type"] + payload = message["payload"] + + if message_type == "method_response": + get_event_loop().create_task(self.handle_method_response(message_id, payload["success"], payload["result"])) + + async def handle_method_response(self, message_id, success, result): + if message_id in self.callbacks: + self.callbacks[message_id].set_result(dict(success = success, result = result)) + del self.callbacks[message_id] + + async def send_message(self, type, payload): + id = str(uuid.uuid4()) + callback = get_event_loop().create_future() + message = json.dumps(dict(id = id, type = type, payload = payload)) + + self.callbacks[id] = callback + self.transport.write(message.encode('utf-8')) + + try: + return await wait_for(callback, 10) + except TimeoutError as e: + del self.callbacks[id] + raise e + + def call_method(self, method_name, method_args): + return self.send_message("method_call", dict(name = method_name, args = method_args)) |
