mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-27 21:19:31 +01:00
Irc: wrap (re)connecting in a while loop instead of calling it recursively
Also, remove unnecessary comments / ENDBURST hook from relay. Ping handling seems to work fine now! Closes #57. Closes #42.
This commit is contained in:
parent
99fd6060a7
commit
836d0e9701
51
main.py
51
main.py
@ -64,22 +64,29 @@ class Irc():
|
||||
def connect(self):
|
||||
ip = self.serverdata["ip"]
|
||||
port = self.serverdata["port"]
|
||||
log.info("Connecting to network %r on %s:%s", self.name, ip, port)
|
||||
try:
|
||||
# Initial connection timeout is a lot smaller than the timeout after
|
||||
# we've connected; this is intentional.
|
||||
self.socket = socket.create_connection((ip, port), timeout=1)
|
||||
self.socket.setblocking(0)
|
||||
self.socket.settimeout(self.pingtimeout)
|
||||
self.proto.connect(self)
|
||||
except (socket.error, classes.ProtocolError, ConnectionError) as e:
|
||||
log.warning('(%s) Failed to connect to IRC: %s: %s',
|
||||
self.name, type(e).__name__, str(e))
|
||||
self.disconnect()
|
||||
else:
|
||||
self.spawnMain()
|
||||
self.schedulePing()
|
||||
self.run()
|
||||
while True:
|
||||
log.info("Connecting to network %r on %s:%s", self.name, ip, port)
|
||||
try:
|
||||
# Initial connection timeout is a lot smaller than the timeout after
|
||||
# we've connected; this is intentional.
|
||||
self.socket = socket.create_connection((ip, port), timeout=1)
|
||||
self.socket.setblocking(0)
|
||||
self.socket.settimeout(self.pingtimeout)
|
||||
self.proto.connect(self)
|
||||
self.spawnMain()
|
||||
self.schedulePing()
|
||||
self.run()
|
||||
except (socket.error, classes.ProtocolError, ConnectionError) as e:
|
||||
log.warning('(%s) Failed to connect to IRC: %s: %s',
|
||||
self.name, type(e).__name__, str(e))
|
||||
self.disconnect()
|
||||
autoconnect = self.serverdata.get('autoconnect')
|
||||
if autoconnect is not None and autoconnect >= 0:
|
||||
log.info('(%s) Going to auto-reconnect in %s seconds.', self.name, autoconnect)
|
||||
time.sleep(autoconnect)
|
||||
else:
|
||||
return
|
||||
|
||||
|
||||
def disconnect(self):
|
||||
log.debug('(%s) Canceling pingTimer at %s due to disconnect() call', self.name, time.time())
|
||||
@ -89,11 +96,6 @@ class Irc():
|
||||
self.pingTimer.cancel()
|
||||
except: # Socket timed out during creation; ignore
|
||||
pass
|
||||
autoconnect = self.serverdata.get('autoconnect')
|
||||
if autoconnect is not None and autoconnect >= 1110:
|
||||
log.info('(%s) Going to auto-reconnect in %s seconds.', self.name, autoconnect)
|
||||
time.sleep(autoconnect)
|
||||
self.connect()
|
||||
|
||||
def run(self):
|
||||
buf = ""
|
||||
@ -122,12 +124,7 @@ class Irc():
|
||||
data = data.replace('\n', ' ')
|
||||
data = data.encode("utf-8") + b"\n"
|
||||
log.debug("(%s) -> %s", self.name, data.decode("utf-8").strip("\n"))
|
||||
try:
|
||||
self.socket.send(data)
|
||||
except (socket.error, classes.ProtocolError, ConnectionError) as e:
|
||||
log.warning('(%s) Disconnected from IRC: %s: %s',
|
||||
self.name, type(e).__name__, str(e))
|
||||
raise ProtocolError
|
||||
self.socket.send(data)
|
||||
|
||||
def schedulePing(self):
|
||||
self.proto.pingServer(self)
|
||||
|
@ -18,6 +18,7 @@ relayusers = defaultdict(dict)
|
||||
def normalizeNick(irc, netname, nick, separator="/"):
|
||||
# Block until we know the IRC network's nick length (after capabilities
|
||||
# are sent)
|
||||
log.debug('(%s) normalizeNick: waiting for irc.connected', irc.name)
|
||||
irc.connected.wait()
|
||||
|
||||
orig_nick = nick
|
||||
@ -675,6 +676,7 @@ def delink(irc, source, args):
|
||||
utils.msg(irc, source, 'Error: no such relay %r.' % channel)
|
||||
|
||||
def initializeAll(irc):
|
||||
log.debug('(%s) initializeAll: waiting for utils.started', irc.name)
|
||||
utils.started.wait()
|
||||
for chanpair, entrydata in db.items():
|
||||
network, channel = chanpair
|
||||
@ -691,18 +693,3 @@ def main():
|
||||
thread = threading.Thread(target=scheduler.run)
|
||||
thread.daemon = True
|
||||
thread.start()
|
||||
'''
|
||||
for ircobj in utils.networkobjects.values():
|
||||
initializeAll(irc)
|
||||
|
||||
# Same goes for all the other initialization stuff; we only
|
||||
# want it to happen once.
|
||||
for network, ircobj in utils.networkobjects.items():
|
||||
if ircobj.name != irc.name:
|
||||
irc.proto.spawnServer(irc, '%s.relay' % network)
|
||||
'''
|
||||
|
||||
def handle_endburst(irc, numeric, command, args):
|
||||
thread = threading.Thread(target=initializeAll, args=(irc,))
|
||||
thread.start()
|
||||
utils.add_hook(handle_endburst, "ENDBURST")
|
||||
|
Loading…
Reference in New Issue
Block a user