mirror of
https://github.com/SteamDeckHomebrew/decky-loader.git
synced 2026-06-17 08:47:49 +00:00
run method calls asynchronously
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import asyncio, time
|
import asyncio, time
|
||||||
from typing import Awaitable, Callable
|
from typing import Any, Callable, Coroutine
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from .localplatform import ON_WINDOWS
|
from .localplatform import ON_WINDOWS
|
||||||
@@ -7,7 +7,7 @@ from .localplatform import ON_WINDOWS
|
|||||||
BUFFER_LIMIT = 2 ** 20 # 1 MiB
|
BUFFER_LIMIT = 2 ** 20 # 1 MiB
|
||||||
|
|
||||||
class UnixSocket:
|
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.
|
on_new_message takes 1 string argument.
|
||||||
It's return value gets used, if not None, to write data to the socket.
|
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):
|
async def _listen_for_method_call(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
|
def _(task: asyncio.Task[str|None]):
|
||||||
|
res = task.result()
|
||||||
|
if res is not None:
|
||||||
|
asyncio.create_task(self._write_single_line(writer, res))
|
||||||
|
|
||||||
line = await self._read_single_line(reader)
|
line = await self._read_single_line(reader)
|
||||||
|
asyncio.create_task(self.on_new_message(line)).add_done_callback(_)
|
||||||
try:
|
|
||||||
res = await self.on_new_message(line)
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
|
|
||||||
if res != None:
|
|
||||||
await self._write_single_line(writer, res)
|
|
||||||
|
|
||||||
class PortSocket (UnixSocket):
|
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.
|
on_new_message takes 1 string argument.
|
||||||
It's return value gets used, if not None, to write data to the socket.
|
It's return value gets used, if not None, to write data to the socket.
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ class PluginWrapper:
|
|||||||
self.name = json["name"]
|
self.name = json["name"]
|
||||||
self.author = json["author"]
|
self.author = json["author"]
|
||||||
self.flags = json["flags"]
|
self.flags = json["flags"]
|
||||||
|
|
||||||
self.passive = not path.isfile(self.file)
|
self.passive = not path.isfile(self.file)
|
||||||
|
|
||||||
self.log = getLogger("plugin")
|
self.log = getLogger("plugin")
|
||||||
@@ -43,15 +44,18 @@ class PluginWrapper:
|
|||||||
|
|
||||||
async def _response_listener(self):
|
async def _response_listener(self):
|
||||||
while True:
|
while True:
|
||||||
line = await self._socket.read_single_line()
|
try:
|
||||||
if line != None:
|
line = await self._socket.read_single_line()
|
||||||
res = loads(line)
|
if line != None:
|
||||||
if res["id"] == 0:
|
res = loads(line)
|
||||||
create_task(self.emitted_message_callback(res["payload"]))
|
if res["id"] == "0":
|
||||||
return
|
create_task(self.emitted_message_callback(res["payload"]))
|
||||||
self._method_call_requests.pop(res["id"]).set_result(res)
|
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
|
self.emitted_message_callback = callback
|
||||||
|
|
||||||
async def execute_method(self, method_name: str, kwargs: Dict[Any, Any]):
|
async def execute_method(self, method_name: str, kwargs: Dict[Any, Any]):
|
||||||
@@ -69,7 +73,7 @@ class PluginWrapper:
|
|||||||
if self.passive:
|
if self.passive:
|
||||||
return self
|
return self
|
||||||
Process(target=self.sandboxed_plugin.initialize, args=[self._socket]).start()
|
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
|
return self
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
|||||||
@@ -128,6 +128,6 @@ class SandboxedPlugin:
|
|||||||
|
|
||||||
async def emit_message(self, message: Dict[Any, Any]):
|
async def emit_message(self, message: Dict[Any, Any]):
|
||||||
await self._socket.write_single_line(dumps({
|
await self._socket.write_single_line(dumps({
|
||||||
"id": 0,
|
"id": "0",
|
||||||
"payload": message
|
"payload": message
|
||||||
}))
|
}))
|
||||||
Reference in New Issue
Block a user