mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-29 12:53:44 +03:00
Set a known buffer size if possible on BinaryWriter()s
This commit is contained in:
parent
7ad2ece168
commit
b4811261e9
|
@ -1,4 +1,4 @@
|
||||||
from io import BufferedWriter, BytesIO
|
from io import BufferedWriter, BytesIO, DEFAULT_BUFFER_SIZE
|
||||||
from struct import pack
|
from struct import pack
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,11 +8,16 @@ class BinaryWriter:
|
||||||
Also creates a "Memory Stream" if necessary
|
Also creates a "Memory Stream" if necessary
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, stream=None):
|
def __init__(self, stream=None, known_length=None):
|
||||||
if not stream:
|
if not stream:
|
||||||
stream = BytesIO()
|
stream = BytesIO()
|
||||||
|
|
||||||
self.writer = BufferedWriter(stream)
|
if known_length is None:
|
||||||
|
# On some systems, DEFAULT_BUFFER_SIZE defaults to 8192
|
||||||
|
# That's over 16 times as big as necessary for most messages
|
||||||
|
known_length = max(DEFAULT_BUFFER_SIZE, 1024)
|
||||||
|
|
||||||
|
self.writer = BufferedWriter(stream, buffer_size=known_length)
|
||||||
self.written_count = 0
|
self.written_count = 0
|
||||||
|
|
||||||
# region Writing
|
# region Writing
|
||||||
|
|
|
@ -19,7 +19,7 @@ def do_authentication(connection):
|
||||||
|
|
||||||
# Step 1 sending: PQ Request
|
# Step 1 sending: PQ Request
|
||||||
nonce = os.urandom(16)
|
nonce = os.urandom(16)
|
||||||
with BinaryWriter() as writer:
|
with BinaryWriter(known_length=20) as writer:
|
||||||
writer.write_int(0x60469778, signed=False) # Constructor number
|
writer.write_int(0x60469778, signed=False) # Constructor number
|
||||||
writer.write(nonce)
|
writer.write(nonce)
|
||||||
sender.send(writer.get_bytes())
|
sender.send(writer.get_bytes())
|
||||||
|
|
|
@ -151,8 +151,9 @@ class Connection:
|
||||||
def _send_tcp_full(self, message):
|
def _send_tcp_full(self, message):
|
||||||
# https://core.telegram.org/mtproto#tcp-transport
|
# https://core.telegram.org/mtproto#tcp-transport
|
||||||
# total length, sequence number, packet and checksum (CRC32)
|
# total length, sequence number, packet and checksum (CRC32)
|
||||||
with BinaryWriter() as writer:
|
length = len(message) + 12
|
||||||
writer.write_int(len(message) + 12)
|
with BinaryWriter(known_length=length) as writer:
|
||||||
|
writer.write_int(length)
|
||||||
writer.write_int(self._send_counter)
|
writer.write_int(self._send_counter)
|
||||||
writer.write(message)
|
writer.write(message)
|
||||||
writer.write_int(crc32(writer.get_bytes()), signed=False)
|
writer.write_int(crc32(writer.get_bytes()), signed=False)
|
||||||
|
@ -160,13 +161,13 @@ class Connection:
|
||||||
self.write(writer.get_bytes())
|
self.write(writer.get_bytes())
|
||||||
|
|
||||||
def _send_intermediate(self, message):
|
def _send_intermediate(self, message):
|
||||||
with BinaryWriter() as writer:
|
with BinaryWriter(known_length=len(message) + 4) as writer:
|
||||||
writer.write_int(len(message))
|
writer.write_int(len(message))
|
||||||
writer.write(message)
|
writer.write(message)
|
||||||
self.write(writer.get_bytes())
|
self.write(writer.get_bytes())
|
||||||
|
|
||||||
def _send_abridged(self, message):
|
def _send_abridged(self, message):
|
||||||
with BinaryWriter() as writer:
|
with BinaryWriter(known_length=len(message) + 4) as writer:
|
||||||
length = len(message) >> 2
|
length = len(message) >> 2
|
||||||
if length < 127:
|
if length < 127:
|
||||||
writer.write_byte(length)
|
writer.write_byte(length)
|
||||||
|
|
|
@ -20,7 +20,7 @@ class MtProtoPlainSender:
|
||||||
|
|
||||||
def send(self, data):
|
def send(self, data):
|
||||||
"""Sends a plain packet (auth_key_id = 0) containing the given message body (data)"""
|
"""Sends a plain packet (auth_key_id = 0) containing the given message body (data)"""
|
||||||
with BinaryWriter() as writer:
|
with BinaryWriter(known_length=len(data) + 20) as writer:
|
||||||
writer.write_long(0)
|
writer.write_long(0)
|
||||||
writer.write_long(self._get_new_msg_id())
|
writer.write_long(self._get_new_msg_id())
|
||||||
writer.write_int(len(data))
|
writer.write_int(len(data))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user