summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarios8543 <marios8543@gmail.com>2023-10-18 19:38:07 +0300
committermarios8543 <marios8543@gmail.com>2023-10-31 23:18:31 +0200
commite4b1efc44dc2f8dc718c809ea50680de626474c4 (patch)
tree8012bc64ca7278b7cb7ad335dd718e98822b9f02
parent85f4604bfdf3b5403f5f65e0c1e7f512b5b95ec1 (diff)
downloaddecky-loader-e4b1efc44dc2f8dc718c809ea50680de626474c4.tar.gz
decky-loader-e4b1efc44dc2f8dc718c809ea50680de626474c4.zip
run method calls asynchronously
-rw-r--r--backend/src/localplatform/localsocket.py19
-rw-r--r--backend/src/plugin/plugin.py22
-rw-r--r--backend/src/plugin/sandboxed_plugin.py2
3 files changed, 23 insertions, 20 deletions
diff --git a/backend/src/localplatform/localsocket.py b/backend/src/localplatform/localsocket.py
index f38fe5e7..2ef039ee 100644
--- a/backend/src/localplatform/localsocket.py
+++ b/backend/src/localplatform/localsocket.py
@@ -1,5 +1,5 @@
import asyncio, time
-from typing import Awaitable, Callable
+from typing import Any, Callable, Coroutine
import random
from .localplatform import ON_WINDOWS
@@ -7,7 +7,7 @@ from .localplatform import ON_WINDOWS
BUFFER_LIMIT = 2 ** 20 # 1 MiB
class UnixSocket:
- def __init__(self, on_new_message: Callable[[str], Awaitable[str|None]]):
+ def __init__(self, on_new_message: Callable[[str], Coroutine[Any, Any, Any]]):
'''
on_new_message takes 1 string argument.
It's return value gets used, if not None, to write data to the socket.
@@ -93,18 +93,17 @@ class UnixSocket:
async def _listen_for_method_call(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
while True:
- line = await self._read_single_line(reader)
- try:
- res = await self.on_new_message(line)
- except Exception:
- return
+ def _(task: asyncio.Task[str|None]):
+ res = task.result()
+ if res is not None:
+ asyncio.create_task(self._write_single_line(writer, res))
- if res != None:
- await self._write_single_line(writer, res)
+ line = await self._read_single_line(reader)
+ asyncio.create_task(self.on_new_message(line)).add_done_callback(_)
class PortSocket (UnixSocket):
- def __init__(self, on_new_message: Callable[[str], Awaitable[str|None]]):
+ def __init__(self, on_new_message: Callable[[str], Coroutine[Any, Any, Any]]):
'''
on_new_message takes 1 string argument.
It's return value gets used, if not None, to write data to the socket.
diff --git a/backend/src/plugin/plugin.py b/backend/src/plugin/plugin.py
index a0f926cd..befd1569 100644
--- a/backend/src/plugin/plugin.py
+++ b/backend/src/plugin/plugin.py
@@ -26,6 +26,7 @@ class PluginWrapper:
self.name = json["name"]
self.author = json["author"]
self.flags = json["flags"]
+
self.passive = not path.isfile(self.file)
self.log = getLogger("plugin")
@@ -43,15 +44,18 @@ class PluginWrapper:
async def _response_listener(self):
while True:
- line = await self._socket.read_single_line()
- if line != None:
- res = loads(line)
- if res["id"] == 0:
- create_task(self.emitted_message_callback(res["payload"]))
- return
- self._method_call_requests.pop(res["id"]).set_result(res)
+ try:
+ line = await self._socket.read_single_line()
+ if line != None:
+ res = loads(line)
+ if res["id"] == "0":
+ create_task(self.emitted_message_callback(res["payload"]))
+ return
+ self._method_call_requests.pop(res["id"]).set_result(res)
+ except:
+ pass
- async def set_emitted_message_callback(self, callback: Callable[[Dict[Any, Any]], Coroutine[Any, Any, Any]]):
+ def set_emitted_message_callback(self, callback: Callable[[Dict[Any, Any]], Coroutine[Any, Any, Any]]):
self.emitted_message_callback = callback
async def execute_method(self, method_name: str, kwargs: Dict[Any, Any]):
@@ -69,7 +73,7 @@ class PluginWrapper:
if self.passive:
return self
Process(target=self.sandboxed_plugin.initialize, args=[self._socket]).start()
- self.listener_task = create_task(self._response_listener())
+ self._listener_task = create_task(self._response_listener())
return self
def stop(self):
diff --git a/backend/src/plugin/sandboxed_plugin.py b/backend/src/plugin/sandboxed_plugin.py
index e4b5f7df..c437f249 100644
--- a/backend/src/plugin/sandboxed_plugin.py
+++ b/backend/src/plugin/sandboxed_plugin.py
@@ -133,6 +133,6 @@ class SandboxedPlugin:
async def emit_message(self, message: Dict[Any, Any]):
await self._socket.write_single_line(dumps({
- "id": 0,
+ "id": "0",
"payload": message
})) \ No newline at end of file