Skip to content

Commit

Permalink
[FIX] Fixed all issues in PR tayler6000#39
Browse files Browse the repository at this point in the history
[CHANGE] Changed request_port to run release_ports() if none are available to make sure no calls missed cleanup due to calls that failed to init.
  • Loading branch information
tayler6000 committed Apr 16, 2022
1 parent 73d559e commit b0ac959
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 2 additions & 4 deletions pyVoIP/SIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,11 +589,9 @@ def stop(self):
self._close_sockets()

def _close_sockets(self):
if hasattr(self, 's'):
if self.s:
if hasattr(self, 's') and self.s:
self.s.close()
if hasattr(self, 'out'):
if self.out:
if hasattr(self, 'out') and self.out:
self.out.close()

def genCallID(self):
Expand Down
12 changes: 10 additions & 2 deletions pyVoIP/VoIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ def __init__(self, phone, callstate, request, session_id, myIP, ms = None):
self.assignedPorts[m] = self.ms[m]

def __del__(self):
self.phone.release_ports(call=self)
if hasattr(self, 'phone'):
self.phone.release_ports(call=self)

def dtmfCallback(self, code):
self.dtmfLock.acquire()
Expand Down Expand Up @@ -315,7 +316,7 @@ def callback(self, request):
sess_id = proposed
message = self.sip.genRinging(request)
self.sip.out.sendto(message.encode('utf8'), (self.server, self.port))
self.calls[call_id] = VoIPCall(self, CallState.RINGING, request, sess_id, self.myIP, portRange=(self.rtpPortLow, self.rtpPortHigh))
self.calls[call_id] = VoIPCall(self, CallState.RINGING, request, sess_id, self.myIP))
try:
t = Timer(1, self.callCallback, [self.calls[call_id]])
t.name = "Phone Call: "+call_id
Expand Down Expand Up @@ -362,12 +363,19 @@ def callback(self, request):
def request_port(self, blocking=True) -> int:
ports_available = [port for port in range(self.rtpPortLow,
self.rtpPortHigh + 1) if port not in self.assignedPorts]
if len(ports_available) == 0:
# If no ports are available attempt to cleanup any missed calls.
self.release_ports()
ports_available = [port for port in range(self.rtpPortLow,
self.rtpPortHigh + 1) if (port not in
self.assignedPorts)]

while self.NSD and blocking and len(ports_available) == 0:
ports_available = [port for port in range(self.rtpPortLow,
self.rtpPortHigh + 1) if (port not in
self.assignedPorts)]
time.sleep(.5)
self.release_ports()

if len(ports_available) == 0:
raise NoPortsAvailableError("No ports were available to be assigned")
Expand Down

0 comments on commit b0ac959

Please sign in to comment.