diff options
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/src/localplatform/localsocket.py | 19 | ||||
| -rw-r--r-- | backend/src/plugin/plugin.py | 22 | ||||
| -rw-r--r-- | backend/src/plugin/sandboxed_plugin.py | 2 |
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 972a535a..8540bebc 100644 --- a/backend/src/plugin/sandboxed_plugin.py +++ b/backend/src/plugin/sandboxed_plugin.py @@ -128,6 +128,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 |
