1
0
mirror of https://github.com/JoelBender/bacpypes synced 2025-09-28 22:15:23 +08:00

sync with changes in py3 code

This commit is contained in:
Joel Bender 2017-04-27 11:36:44 -04:00
parent 6aa6330c85
commit 6565627b98

View File

@ -25,6 +25,7 @@ _log = ModuleLogger(globals())
# globals # globals
REBIND_SLEEP_INTERVAL = 2.0 REBIND_SLEEP_INTERVAL = 2.0
CONNECT_TIMEOUT = 30.0
# #
# PickleActorMixIn # PickleActorMixIn
@ -93,7 +94,7 @@ class PickleActorMixIn:
@bacpypes_debugging @bacpypes_debugging
class TCPClient(asyncore.dispatcher): class TCPClient(asyncore.dispatcher):
_connect_timeout = None _connect_timeout = CONNECT_TIMEOUT
def __init__(self, peer): def __init__(self, peer):
if _debug: TCPClient._debug("__init__ %r", peer) if _debug: TCPClient._debug("__init__ %r", peer)
@ -103,8 +104,7 @@ class TCPClient(asyncore.dispatcher):
self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
# set the timeout # set the timeout
if self._connect_timeout is not None: self.socket.settimeout(self._connect_timeout)
self.settimeout(self._connect_timeout)
if _debug: TCPClient._debug(" - timeout: %r", self._connect_timeout) if _debug: TCPClient._debug(" - timeout: %r", self._connect_timeout)
# save the peer # save the peer
@ -116,11 +116,11 @@ class TCPClient(asyncore.dispatcher):
# try to connect # try to connect
try: try:
rslt = self.connect_ex(peer) rslt = self.socket.connect_ex(peer)
if (rslt == 0): if (rslt == 0):
if _debug: TCPClient._debug(" - connected") if _debug: TCPClient._debug(" - connected")
self.connected = True self.connected = True
if (rslt == errno.EINPROGRESS): elif (rslt == errno.EINPROGRESS):
if _debug: TCPClient._debug(" - in progress") if _debug: TCPClient._debug(" - in progress")
elif (rslt in (errno.ECONNREFUSED, 111)): elif (rslt in (errno.ECONNREFUSED, 111)):
if _debug: TCPClient._debug(" - connection refused") if _debug: TCPClient._debug(" - connection refused")
@ -216,10 +216,13 @@ class TCPClient(asyncore.dispatcher):
if _debug: TCPClient._debug(" - err: %r", err) if _debug: TCPClient._debug(" - err: %r", err)
# check for connection refused # check for connection refused
if (err in (errno.ECONNREFUSED, 111)): if err == 0:
if not self.connected:
if _debug: TCPClient._debug(" - connected")
self.connected = True
elif (err in (errno.ECONNREFUSED, 111)):
if _debug: TCPClient._debug(" - connection to %r refused", self.peer) if _debug: TCPClient._debug(" - connection to %r refused", self.peer)
self.handle_error(socket.error(err, "connection refused")) self.handle_error(socket.error(err, "connection refused"))
self.handle_close()
return return
# pass along # pass along
@ -229,7 +232,7 @@ class TCPClient(asyncore.dispatcher):
if _debug: TCPClient._debug("handle_close") if _debug: TCPClient._debug("handle_close")
# close the socket # close the socket
self.close() self.socket.close()
# make sure other routines know the socket is closed # make sure other routines know the socket is closed
self.socket = None self.socket = None
@ -238,6 +241,11 @@ class TCPClient(asyncore.dispatcher):
"""Trap for TCPClient errors, otherwise continue.""" """Trap for TCPClient errors, otherwise continue."""
if _debug: TCPClient._debug("handle_error %r", error) if _debug: TCPClient._debug("handle_error %r", error)
# if there is no socket, it was closed
if not self.socket:
if _debug: TCPClient._debug(" - error already handled")
return
# core does not take parameters # core does not take parameters
asyncore.dispatcher.handle_error(self) asyncore.dispatcher.handle_error(self)
@ -276,7 +284,7 @@ class TCPClientActor(TCPClient):
# add a timer # add a timer
self._idle_timeout = director.idle_timeout self._idle_timeout = director.idle_timeout
if self._idle_timeout > 0: if self._idle_timeout:
self.idle_timeout_task = FunctionTask(self.idle_timeout) self.idle_timeout_task = FunctionTask(self.idle_timeout)
self.idle_timeout_task.install_task(_time() + self._idle_timeout) self.idle_timeout_task.install_task(_time() + self._idle_timeout)
else: else:
@ -310,6 +318,14 @@ class TCPClientActor(TCPClient):
def handle_close(self): def handle_close(self):
if _debug: TCPClientActor._debug("handle_close") if _debug: TCPClientActor._debug("handle_close")
# if _debug:
# stack_information = list(traceback.extract_stack())
# stack_information.reverse()
# stack_information = '\n'.join(" %-15s%s:%s" % (
# fn, filename.split('/')[-1], lineno) for filename, lineno, fn, _ in stack_information
# )
# TCPClientActor._debug(" - stack_information: %s", stack_information)
# if there's a flush task, cancel it # if there's a flush task, cancel it
if self.flush_task: if self.flush_task:
self.flush_task.suspend_task() self.flush_task.suspend_task()
@ -394,7 +410,7 @@ class TCPClientDirector(Server, ServiceAccessPoint, DebugContents):
_debug_contents = ('connect_timeout', 'idle_timeout', 'actorClass', 'clients', 'reconnect') _debug_contents = ('connect_timeout', 'idle_timeout', 'actorClass', 'clients', 'reconnect')
def __init__(self, connect_timeout=None, idle_timeout=0, actorClass=TCPClientActor, sid=None, sapID=None): def __init__(self, connect_timeout=None, idle_timeout=None, actorClass=TCPClientActor, sid=None, sapID=None):
if _debug: if _debug:
TCPClientDirector._debug("__init__ connect_timeout=%r idle_timeout=%r actorClass=%r sid=%r sapID=%r", TCPClientDirector._debug("__init__ connect_timeout=%r idle_timeout=%r actorClass=%r sid=%r sapID=%r",
connect_timeout, idle_timeout, actorClass, sid, sapID, connect_timeout, idle_timeout, actorClass, sid, sapID,
@ -603,7 +619,7 @@ class TCPServerActor(TCPServer):
# add a timer # add a timer
self._idle_timeout = director.idle_timeout self._idle_timeout = director.idle_timeout
if self._idle_timeout > 0: if self._idle_timeout:
self.idle_timeout_task = FunctionTask(self.idle_timeout) self.idle_timeout_task = FunctionTask(self.idle_timeout)
self.idle_timeout_task.install_task(_time() + self._idle_timeout) self.idle_timeout_task.install_task(_time() + self._idle_timeout)
else: else: