Skip to content

Commit

Permalink
Handle disconnects in AsyncioClient
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbaynham committed May 26, 2020
1 parent a0de059 commit 451d09a
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions sipyco/pc_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -269,8 +271,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):
Expand Down

0 comments on commit 451d09a

Please sign in to comment.