3
0
mirror of https://github.com/jlu5/PyLink.git synced 2025-01-23 18:54:05 +01:00

Irc: add internal support for aborting connections

New "aborted" threading event, which can be set to True in order to stop the existing run() loop

Autoconnect confirmed to work with this.
This commit is contained in:
James Lu 2015-09-02 22:05:47 -07:00
parent 320de2079a
commit eac81de176

View File

@ -20,9 +20,11 @@ class ProtocolError(Exception):
class Irc(): class Irc():
def initVars(self): def initVars(self):
self.connected.clear()
self.aborted.clear()
self.pseudoclient = None self.pseudoclient = None
self.connected = threading.Event()
self.lastping = time.time() self.lastping = time.time()
# Server, channel, and user indexes to be populated by our protocol module # Server, channel, and user indexes to be populated by our protocol module
self.servers = {self.sid: IrcServer(None, self.serverdata['hostname'], internal=True)} self.servers = {self.sid: IrcServer(None, self.serverdata['hostname'], internal=True)}
self.users = {} self.users = {}
@ -67,6 +69,9 @@ class Irc():
self.pingfreq = self.serverdata.get('pingfreq') or 30 self.pingfreq = self.serverdata.get('pingfreq') or 30
self.pingtimeout = self.pingfreq * 2 self.pingtimeout = self.pingfreq * 2
self.connected = threading.Event()
self.aborted = threading.Event()
self.initVars() self.initVars()
if world.testing: if world.testing:
@ -151,7 +156,7 @@ class Irc():
except (socket.error, ProtocolError, ConnectionError) as e: except (socket.error, ProtocolError, ConnectionError) as e:
log.warning('(%s) Disconnected from IRC: %s: %s', log.warning('(%s) Disconnected from IRC: %s: %s',
self.name, type(e).__name__, str(e)) self.name, type(e).__name__, str(e))
self.disconnect() self._disconnect()
autoconnect = self.serverdata.get('autoconnect') autoconnect = self.serverdata.get('autoconnect')
log.debug('(%s) Autoconnect delay set to %s seconds.', self.name, autoconnect) log.debug('(%s) Autoconnect delay set to %s seconds.', self.name, autoconnect)
if autoconnect is not None and autoconnect >= 0: if autoconnect is not None and autoconnect >= 0:
@ -160,8 +165,8 @@ class Irc():
else: else:
return return
def disconnect(self): def _disconnect(self):
log.debug('(%s) Canceling pingTimer at %s due to disconnect() call', self.name, time.time()) log.debug('(%s) Canceling pingTimer at %s due to _disconnect() call', self.name, time.time())
self.connected.clear() self.connected.clear()
try: try:
self.socket.close() self.socket.close()
@ -171,10 +176,14 @@ class Irc():
# Internal hook signifying that a network has disconnected. # Internal hook signifying that a network has disconnected.
self.callHooks([None, 'PYLINK_DISCONNECT', {}]) self.callHooks([None, 'PYLINK_DISCONNECT', {}])
def disconnect(self):
"""Closes the IRC connection."""
self.aborted.set()
def run(self): def run(self):
buf = b"" buf = b""
data = b"" data = b""
while True: while not self.aborted.is_set():
data = self.socket.recv(2048) data = self.socket.recv(2048)
buf += data buf += data
if self.connected.is_set() and not data: if self.connected.is_set() and not data:
@ -186,7 +195,7 @@ class Irc():
while b'\n' in buf: while b'\n' in buf:
line, buf = buf.split(b'\n', 1) line, buf = buf.split(b'\n', 1)
line = line.strip(b'\r') line = line.strip(b'\r')
# TODO: respect other encodings? # FIXME: respect other encodings?
line = line.decode("utf-8", "replace") line = line.decode("utf-8", "replace")
log.debug("(%s) <- %s", self.name, line) log.debug("(%s) <- %s", self.name, line)
hook_args = None hook_args = None