Don't do anything on .connect() if it's already connected

This commit is contained in:
Lonami Exo 2017-06-19 09:58:03 +02:00
parent daa626aa04
commit 697434be37
3 changed files with 12 additions and 0 deletions

View File

@ -35,6 +35,9 @@ class MtProtoSender:
"""Connects to the server"""
self._transport.connect()
def is_connected(self):
return self._transport.is_connected()
def disconnect(self):
"""Disconnects from the server"""
self._transport.close()

View File

@ -18,6 +18,9 @@ class TcpTransport:
self.send_counter = 0
self.tcp_client.connect(self.ip, self.port)
def is_connected(self):
return self.tcp_client.connected
# Original reference: https://core.telegram.org/mtproto#tcp-transport
# The packets are encoded as: total length, sequence number, packet and checksum (CRC32)
def send(self, packet):

View File

@ -79,6 +79,12 @@ 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():
self._logger.warning(
'Attempted to connect when the client was already connected.'
)
return
transport = TcpTransport(self.session.server_address,
self.session.port, proxy=self.proxy)