diff --git a/sipyco/pc_rpc.py b/sipyco/pc_rpc.py index 2ca41e2..df5f660 100644 --- a/sipyco/pc_rpc.py +++ b/sipyco/pc_rpc.py @@ -153,15 +153,14 @@ def __send(self, obj): self.__socket.sendall(line.encode()) def __recv(self): - if self.__closed: - raise CONNECTION_CLOSED_ERR - buf = self.__socket.recv(4096).decode() - while "\n" not in buf: - more = self.__socket.recv(4096) - if not more: - break - buf += more.decode() - if not buf: + if not self.__closed: + buf = self.__socket.recv(4096).decode() + while "\n" not in buf: + more = self.__socket.recv(4096) + if not more: + break + buf += more.decode() + if self.__closed or not buf: self.__closed = True raise CONNECTION_CLOSED_ERR return pyon.decode(buf) @@ -198,7 +197,10 @@ class AsyncioClient: """This class is similar to :class:`sipyco.pc_rpc.Client`, but uses ``asyncio`` instead of blocking calls. - All RPC methods are coroutines. + All RPC methods are coroutines. As with :class:`sipyco.pc_rpc.Client`, + methods will raise ConnectionAbortedError if the server closes the + connection. The user should call :meth:`~sipyco.pc_rpc.AsyncioClient.close_rpc` + and then discard this object. Concurrent access from different asyncio tasks is supported; all calls use a single lock. @@ -210,6 +212,7 @@ def __init__(self): self.__writer = None self.__target_names = None self.__description = None + self.__closed = False async def connect_rpc(self, host, port, target_name): """Connects to the server. This cannot be done in __init__ because @@ -269,8 +272,12 @@ def __send(self, obj): line = pyon.encode(obj) + "\n" self.__writer.write(line.encode()) - async def __recv(self): - line = await self.__reader.readline() + async def __recv(self): + if not self.__closed: + line = await self.__reader.readline() + if self.__closed or not line: + self.__closed = True + raise CONNECTION_CLOSED_ERR return pyon.decode(line.decode()) async def __do_rpc(self, name, args, kwargs):