Pre-pack outgoing TLMessage

This has several benefits:
- The message can be resent without re-calling bytes(),
  which for some requests may be expensive.
- Converting requests to bytes early lets us detect
  errors early, such as OverflowError on bad requests.
- Containers can't exceed 1044456 bytes so knowing their
  length is important. This can now be done in O(1).

But also several drawbacks:
- If the object is modified the bytes won't reflect this.
  This isn't an issue because it's only done for in msgs.
- Incoming messages can no longer be reconverted into
  bytes but this was never needed anyway.
This commit is contained in:
Lonami Exo
2018-07-07 11:46:21 +02:00
parent b237947af1
commit 33ce702ab9
3 changed files with 49 additions and 15 deletions

View File

@@ -507,7 +507,6 @@ class MTProtoSender:
rpc_result.req_msg_id)
if rpc_result.error:
# TODO Report errors if possible/enabled
error = rpc_message_to_error(rpc_result.error)
self._send_queue.put_nowait(self.state.create_message(
MsgsAck([message.msg_id])
@@ -517,10 +516,13 @@ class MTProtoSender:
message.future.set_exception(error)
return
elif message:
# TODO Would be nice to avoid accessing a per-obj read_result
# Instead have a variable that indicated how the result should
# be read (an enum) and dispatch to read the result, mostly
# always it's just a normal TLObject.
with BinaryReader(rpc_result.body) as reader:
result = message.obj.read_result(reader)
# TODO Process entities
if not message.future.cancelled():
message.future.set_result(result)
return