3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-24 19:52:53 +01:00

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.
This commit is contained in:
James Lu 2015-08-12 21:31:46 -07:00
parent d3e335edee
commit 18576ad488

15
main.py
View File

@ -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')