Ensure .connect() always returns True/False

This commit is contained in:
Lonami Exo 2017-08-22 23:17:05 +02:00
parent 1a6231472e
commit fb5c43b539

View File

@ -4,6 +4,8 @@ from hashlib import md5
from os import path from os import path
# Import some externalized utilities to work with the Telegram types and more # Import some externalized utilities to work with the Telegram types and more
from telethon.tl.functions import PingRequest
from . import helpers as utils from . import helpers as utils
from .errors import ( from .errors import (
RPCError, FloodWaitError, FileMigrateError, TypeNotFoundError RPCError, FloodWaitError, FileMigrateError, TypeNotFoundError
@ -92,10 +94,14 @@ class TelegramBareClient:
determine the authorization key for the current session. determine the authorization key for the current session.
""" """
if self._sender and self._sender.is_connected(): if self._sender and self._sender.is_connected():
self._logger.debug( # Try sending a ping to make sure we're connected already
'Attempted to connect when the client was already connected.' # TODO Maybe there's a better way to check this
) try:
return self(PingRequest(utils.generate_random_long()))
return True
except:
# If ping failed, ensure we're disconnected first
self.disconnect()
transport = TcpTransport(self.session.server_address, transport = TcpTransport(self.session.server_address,
self.session.port, self.session.port,
@ -146,7 +152,7 @@ class TelegramBareClient:
# This is fine, probably layer migration # This is fine, probably layer migration
self._logger.debug('Found invalid item, probably migrating', e) self._logger.debug('Found invalid item, probably migrating', e)
self.disconnect() self.disconnect()
self.connect(exported_auth=exported_auth) return self.connect(exported_auth=exported_auth)
except (RPCError, ConnectionError) as error: except (RPCError, ConnectionError) as error:
# Probably errors from the previous session, ignore them # Probably errors from the previous session, ignore them