Reconnect on timeout inside timeout (#61)

This commit is contained in:
Lonami Exo 2017-04-29 11:07:32 +02:00
parent a7595347f3
commit fbe65c2dfb
2 changed files with 27 additions and 6 deletions

View File

@ -49,6 +49,11 @@ class MtProtoSender:
name='UpdatesThread', daemon=True, name='UpdatesThread', daemon=True,
target=self.updates_thread_method) target=self.updates_thread_method)
self.connect()
def connect(self):
"""Connects to the server"""
self.transport.connect()
# The "updates" thread must also be running to make periodic ping requests. # The "updates" thread must also be running to make periodic ping requests.
self.set_updates_thread(running=True) self.set_updates_thread(running=True)
@ -57,6 +62,11 @@ class MtProtoSender:
self.set_updates_thread(running=False) self.set_updates_thread(running=False)
self.transport.close() self.transport.close()
def reconnect(self):
"""Disconnects and connects again (effectively reconnecting)"""
self.disconnect()
self.connect()
def add_update_handler(self, handler): def add_update_handler(self, handler):
"""Adds an update handler (a method with one argument, the received """Adds an update handler (a method with one argument, the received
TLObject) that is fired when there are updates available""" TLObject) that is fired when there are updates available"""
@ -438,14 +448,20 @@ class MtProtoSender:
except TimeoutError: except TimeoutError:
Log.d('Receiving updates timed out') Log.d('Receiving updates timed out')
# TODO Workaround for issue #50 # TODO Workaround for issue #50
Log.d('Sending GetStateRequest (workaround for issue #50)')
r = GetStateRequest() r = GetStateRequest()
self.send(r) try:
self.receive(r) Log.d('Sending GetStateRequest (workaround for issue #50)')
self.send(r)
self.receive(r)
except TimeoutError:
Log.w('Timed out inside a timeout, trying to reconnect...')
self.reconnect()
self.send(r)
self.receive(r)
except ReadCancelledError: except ReadCancelledError:
Log.i('Receiving updates cancelled') Log.i('Receiving updates cancelled')
except OSError as e: except OSError:
Log.w('OSError on updates thread, %s logging out', Log.w('OSError on updates thread, %s logging out',
'was' if self.logging_out else 'was not') 'was' if self.logging_out else 'was not')
@ -454,7 +470,7 @@ class MtProtoSender:
# TODO Not sure why this happens because we call disconnect()… # TODO Not sure why this happens because we call disconnect()…
self.set_updates_thread(running=False) self.set_updates_thread(running=False)
else: else:
raise e raise
Log.d('Updates thread released the lock') Log.d('Updates thread released the lock')
self.updates_thread_receiving = False self.updates_thread_receiving = False

View File

@ -8,10 +8,15 @@ from telethon.utils import BinaryWriter
class TcpTransport: class TcpTransport:
def __init__(self, ip_address, port, proxy=None): def __init__(self, ip_address, port, proxy=None):
self.ip = ip_address
self.port = port
self.tcp_client = TcpClient(proxy) self.tcp_client = TcpClient(proxy)
self.send_counter = 0 self.send_counter = 0
self.tcp_client.connect(ip_address, port) def connect(self):
"""Connects to the specified IP address and port"""
self.send_counter = 0
self.tcp_client.connect(self.ip, self.port)
# Original reference: https://core.telegram.org/mtproto#tcp-transport # Original reference: https://core.telegram.org/mtproto#tcp-transport
# The packets are encoded as: total length, sequence number, packet and checksum (CRC32) # The packets are encoded as: total length, sequence number, packet and checksum (CRC32)