Telethon/telethon/tl/message_container.py
Lonami Exo b83cd98ba0 Replace TLObject.on_send with the new .to_bytes()
This also replaces some int.to_bytes() calls with a faster
struct.pack (up to x4 faster). This approach is also around
x3 faster than creating a BinaryWriter just to serialize a
TLObject as bytes.
2017-09-26 14:36:02 +02:00

46 lines
1.5 KiB
Python

from . import TLObject
from ..extensions import BinaryWriter
class MessageContainer(TLObject):
constructor_id = 0x8953ad37
# TODO Currently it's a bit of a hack, since the container actually holds
# messages (message id, sequence number, request body), not requests.
# Probably create a proper "Message" class
def __init__(self, session, requests):
super().__init__()
self.content_related = False
self.session = session
self.requests = requests
def on_send(self, writer):
writer.write_int(0x73f1f8dc, signed=False)
writer.write_int(len(self.requests))
for x in self.requests:
x.request_msg_id = self.session.get_new_msg_id()
writer.write_long(x.request_msg_id)
writer.write_int(
self.session.generate_sequence(x.content_related)
)
packet = x.to_bytes()
writer.write_int(len(packet))
writer.write(packet)
def to_bytes(self):
# TODO Change this to delete the on_send from this class
with BinaryWriter() as writer:
self.on_send(writer)
return writer.get_bytes()
@staticmethod
def iter_read(reader):
reader.read_int(signed=False) # code
size = reader.read_int()
for _ in range(size):
inner_msg_id = reader.read_long()
inner_sequence = reader.read_int()
inner_length = reader.read_int()
yield inner_msg_id, inner_sequence, inner_length