Expose MtProtoSender.is_connected() and use it on .connect()

This commit is contained in:
Lonami Exo 2017-09-17 16:39:29 +02:00
parent 05a8723d36
commit 58fe39f060

View File

@ -103,15 +103,8 @@ class TelegramBareClient:
If 'exported_auth' is not None, it will be used instead to
determine the authorization key for the current session.
"""
if self._sender and self._sender.is_connected():
# Try sending a ping to make sure we're connected already
# TODO Maybe there's a better way to check this
try:
self(PingRequest(utils.generate_random_long()))
return True
except:
# If ping failed, ensure we're disconnected first
self.disconnect()
if self.is_connected():
return True
connection = Connection(
self.session.server_address, self.session.port,
@ -168,6 +161,9 @@ class TelegramBareClient:
)
return False
def is_connected(self):
return self._sender is not None and self._sender.is_connected()
def _init_connection(self, query=None):
result = self(InvokeWithLayerRequest(LAYER, InitConnectionRequest(
api_id=self.api_id,