From 18576ad4881a9f52e6bb0c612e959b1e97fc6d64 Mon Sep 17 00:00:00 2001 From: James Lu Date: Wed, 12 Aug 2015 21:31:46 -0700 Subject: [PATCH] Irc: fix the ping timeout/reconnection logic - reset connection state and last ping time on reconnect - move Ping timeout logic to somewhere where it'd actually run - the code for breaking out of the run() loop on a ping timeout could previously only run when a ping timeout hadn't occured yet - consistently use log.warning() instead of log.warn() This hopefully fixes PyLink going into a disconnect/reconnect loop sometimes instead of reconnecting properly. --- main.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index d377592..bdf34e3 100755 --- a/main.py +++ b/main.py @@ -20,6 +20,8 @@ class Irc(): def initVars(self): # Server, channel, and user indexes to be populated by our protocol module + self.connected = threading.Event() + self.lastping = time.time() self.servers = {self.sid: classes.IrcServer(None, self.serverdata['hostname'], internal=True)} self.users = {} self.channels = defaultdict(classes.IrcChannel) @@ -54,7 +56,6 @@ class Irc(): def __init__(self, netname, proto, conf): # Initialize some variables - self.connected = threading.Event() self.name = netname.lower() self.conf = conf self.serverdata = conf['servers'][netname] @@ -69,7 +70,6 @@ class Irc(): self.connection_thread = threading.Thread(target = self.connect) self.connection_thread.start() self.pingTimer = None - self.lastping = time.time() def connect(self): ip = self.serverdata["ip"] @@ -167,14 +167,15 @@ class Irc(): def run(self): buf = b"" data = b"" - while (time.time() - self.lastping) < self.pingtimeout: - log.debug('(%s) time_since_last_ping: %s', self.name, (time.time() - self.lastping)) - log.debug('(%s) self.pingtimeout: %s', self.name, self.pingtimeout) + while True: data = self.socket.recv(2048) buf += data if self.connected.is_set() and not data: - log.warn('(%s) No data received and self.connected is set; disconnecting!', self.name) - break + log.warning('(%s) No data received and self.connected is set; disconnecting!', self.name) + return + elif (time.time() - self.lastping) > self.pingtimeout: + log.warning('(%s) Connection timed out.', self.name) + return while b'\n' in buf: line, buf = buf.split(b'\n', 1) line = line.strip(b'\r')