2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-26 20:07:27 +00:00
pyrogram/pyrogram/connection/connection.py

76 lines
2.5 KiB
Python
Raw Normal View History

2020-03-21 15:43:32 +01:00
# Pyrogram - Telegram MTProto API Client Library for Python
2022-01-07 10:23:45 +01:00
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
2017-12-05 12:37:30 +01:00
#
2020-03-21 15:43:32 +01:00
# This file is part of Pyrogram.
2017-12-05 12:37:30 +01:00
#
2020-03-21 15:43:32 +01:00
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2017-12-05 12:37:30 +01:00
#
2020-03-21 15:43:32 +01:00
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
2017-12-05 12:37:30 +01:00
#
2020-03-21 15:43:32 +01:00
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
2017-12-05 12:37:30 +01:00
import asyncio
2017-12-05 12:37:30 +01:00
import logging
from typing import Optional
2022-12-26 16:29:05 +01:00
from .transport import TCP, TCPAbridgedO
from ..session.internals import DataCenter
2017-12-05 12:37:30 +01:00
log = logging.getLogger(__name__)
2017-12-05 12:37:30 +01:00
class Connection:
2022-12-26 16:29:05 +01:00
MAX_CONNECTION_ATTEMPTS = 3
2022-02-10 06:44:42 +01:00
2022-12-26 16:29:05 +01:00
def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict, media: bool = False):
2018-09-08 19:33:47 +02:00
self.dc_id = dc_id
self.test_mode = test_mode
self.ipv6 = ipv6
2018-01-16 22:05:19 +01:00
self.proxy = proxy
2021-04-13 15:53:53 +02:00
self.media = media
2022-12-26 16:29:05 +01:00
self.address = DataCenter(dc_id, test_mode, ipv6, media)
self.protocol: TCP = None
2017-12-05 12:37:30 +01:00
async def connect(self):
2022-12-26 16:29:05 +01:00
for i in range(Connection.MAX_CONNECTION_ATTEMPTS):
self.protocol = TCPAbridgedO(self.ipv6, self.proxy)
2017-12-05 12:37:30 +01:00
try:
log.info("Connecting...")
await self.protocol.connect(self.address)
except OSError as e:
2022-12-26 16:29:05 +01:00
log.warning("Unable to connect due to network issues: %s", e)
2022-12-26 22:26:55 +01:00
self.protocol.close()
await asyncio.sleep(1)
2017-12-05 12:37:30 +01:00
else:
2022-12-26 16:29:05 +01:00
log.info("Connected! %s DC%s%s - IPv%s",
"Test" if self.test_mode else "Production",
self.dc_id,
" (media)" if self.media else "",
"6" if self.ipv6 else "4")
2017-12-05 12:37:30 +01:00
break
2022-02-10 06:44:42 +01:00
else:
log.warning("Connection failed! Trying again...")
2022-12-26 22:26:55 +01:00
raise TimeoutError
2017-12-05 12:37:30 +01:00
2022-12-26 22:26:55 +01:00
def close(self):
self.protocol.close()
log.info("Disconnected")
2017-12-05 12:37:30 +01:00
async def send(self, data: bytes):
2022-12-26 22:26:55 +01:00
try:
await self.protocol.send(data)
except Exception as e:
raise OSError(e)
2017-12-05 12:37:30 +01:00
async def recv(self) -> Optional[bytes]:
return await self.protocol.recv()