Exception response in recv na close

This commit is contained in:
Marcel Nijenhof
2020-11-07 13:47:54 +01:00
parent ad1fed8340
commit 5968b6ff5b
2 changed files with 19 additions and 6 deletions

View File

@@ -194,16 +194,19 @@ send a sip command to the server
recieve a answer from the sip server
"""
c = 0
bytebuf=b''
stringbuf=""
while (self._socket != None) and (stringbuf.find("\r") == -1):
while (self._socket != None) and (stringbuf.find("\r") == -1) and (c < 3):
try:
self.log.debug("LmwSip.recv: %s: Waiting for data" % self.cleartelnet);
bytebuf = self._socket.recv(4096)
self.log.debug("recv: bytebuf: %s" % bytebuf)
if self.cleartelnet:
if bytebuf[0] == 255:
bytebuf = b''
if (len(bytebuf) == 0):
c+=1
self.log.debug("recv: bytebuf: %s" % bytebuf)
if self.cleartelnet:
if bytebuf[0] == 255:
bytebuf = b''
except Exception as e:
self.log.error("SipLmw.recv: socket timeout: %s", e)
self.closesocket()
@@ -215,7 +218,11 @@ recieve a answer from the sip server
self.log.error("SipLmw.recv: decode error: %s", e)
self.closesocket()
raise LmwSipDecodeError("LmwSip.recv: decode error", bytebuf)
if self._socket == None:
if (c>=3) and (len(stringbuf) == 0):
self.log.warning("LmwSip.recv: No data recieved")
self.closesocket()
raise LmwSipConnectError("LmwSip.recv: socket close")
elif self._socket == None:
self.log.warning("LmwSip.recv: No connection")
elif len(stringbuf) == 0:
self.log.warning("LmwSip.recv: No data")

View File

@@ -75,6 +75,12 @@ class lmwsipTest(unittest.TestCase):
self.assertEqual(self.sip._roundtime_(t1, "D10"), t1)
self.assertEqual(self.sip._roundtime_(t2, "D10"), t1)
def test_closerecv(self):
self.login()
self.sip.send("CLOSE")
with self.assertRaises(lmwsip.LmwSipConnectError):
self.sip.recv()
def test_run(self):
capturedOutput = io.StringIO()
sys.stdout = capturedOutput