This commit is contained in:
retoor 2025-08-30 15:45:42 +02:00
parent 59c86b3e85
commit 7bea4f3e0e

View File

@ -7,7 +7,7 @@
# MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import asyncio
import json
import logging
import pathlib
@ -52,6 +52,7 @@ class RPC:
self.ws = ws
self.current_call_id = None
self.queue = asyncio.Queue()
self.semaphore = asyncio.Semaphore(1)
def __getattr__(self, name):
async def method(*args, **kwargs):
@ -94,31 +95,32 @@ class RPC:
await self.queue.put(m)
while True:
try:
msg = await self.ws.receive()
except Exception as ex:
logger.exception("Error while receiving:", ex)
break
if msg.type == aiohttp.WSMsgType.CLOSED:
logger.exception("WebSocket closed.")
break
elif msg.type == aiohttp.WSMsgType.ERROR:
logger.exception("WebSocket error.")
break
elif msg.type == aiohttp.WSMsgType.TEXT:
if (
msg.json().get("callId") != self.current_call_id
):
await self.queue.put(msg.json())
continue
async with self.semaphore:
try:
response = self.Response(msg.json())
self.current_call_id = None
return response
msg = await self.ws.receive()
except Exception as ex:
logger.exception(ex)
logger.exception("Error while receiving:", ex)
break
if msg.type == aiohttp.WSMsgType.CLOSED:
logger.exception("WebSocket closed.")
break
elif msg.type == aiohttp.WSMsgType.ERROR:
logger.exception("WebSocket error.")
break
elif msg.type == aiohttp.WSMsgType.TEXT:
if (
msg.json().get("callId") != self.current_call_id
):
await self.queue.put(msg.json())
continue
try:
response = self.Response(msg.json())
self.current_call_id = None
return response
except Exception as ex:
logger.exception(ex)
break
else:
logger.exception("Unexpected message type.")
break
else:
logger.exception("Unexpected message type.")
break
return None