Create a centralized Connection class, replaces TcpTransport (#112)

This commit is contained in:
Lonami Exo
2017-08-28 21:23:31 +02:00
parent bc72e52834
commit fa22a3f848
8 changed files with 143 additions and 138 deletions

View File

@@ -6,17 +6,17 @@ from ..extensions import BinaryReader, BinaryWriter
class MtProtoPlainSender:
"""MTProto Mobile Protocol plain sender (https://core.telegram.org/mtproto/description#unencrypted-messages)"""
def __init__(self, transport):
def __init__(self, connection):
self._sequence = 0
self._time_offset = 0
self._last_msg_id = 0
self._transport = transport
self._connection = connection
def connect(self):
self._transport.connect()
self._connection.connect()
def disconnect(self):
self._transport.close()
self._connection.close()
def send(self, data):
"""Sends a plain packet (auth_key_id = 0) containing the given message body (data)"""
@@ -27,11 +27,11 @@ class MtProtoPlainSender:
writer.write(data)
packet = writer.get_bytes()
self._transport.send(packet)
self._connection.send(packet)
def receive(self):
"""Receives a plain packet, returning the body of the response"""
seq, body = self._transport.receive()
body = self._connection.recv()
with BinaryReader(body) as reader:
reader.read_long() # auth_key_id
reader.read_long() # msg_id