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.
This commit is contained in:
Lonami Exo
2017-09-26 14:36:02 +02:00
parent 2bb26d6389
commit b83cd98ba0
5 changed files with 110 additions and 64 deletions

View File

@@ -18,17 +18,21 @@ class MessageContainer(TLObject):
writer.write_int(0x73f1f8dc, signed=False)
writer.write_int(len(self.requests))
for x in self.requests:
with BinaryWriter() as aux:
x.on_send(aux)
x.request_msg_id = self.session.get_new_msg_id()
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 = aux.get_bytes()
writer.write_int(len(packet))
writer.write(packet)
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):