comm_analyzer: avoid unnecessary copy

This commit is contained in:
Simon Renblad 2024-07-18 16:24:31 +08:00 committed by Sébastien Bourdeauducq
parent 7b13ffd9f3
commit 8a6a6042fd

View File

@ -181,25 +181,25 @@ class AnalyzerProxyReceiver:
async def _receive_cr(self): async def _receive_cr(self):
try: try:
while True: while True:
endian_byte = await self.reader.read(1) data = bytearray()
if endian_byte == b"E": data.extend(await self.reader.read(1))
endian = '>' if len(data) == 0:
elif endian_byte == b"e":
endian = '<'
elif endian_byte == b"":
# EOF reached, connection lost # EOF reached, connection lost
return return
if data[0] == ord("E"):
endian = '>'
elif data[0] == ord("e"):
endian = '<'
else: else:
raise ValueError raise ValueError
payload_length_word = await self.reader.readexactly(4) data.extend(await self.reader.readexactly(4))
payload_length = struct.unpack(endian + "I", payload_length_word)[0] payload_length = struct.unpack(endian + "I", data[1:5])[0]
if payload_length > 10 * 512 * 1024: if payload_length > 10 * 512 * 1024:
# 10x buffer size of firmware # 10x buffer size of firmware
raise ValueError raise ValueError
# The remaining header length is 11 bytes. # The remaining header length is 11 bytes.
remaining_data = await self.reader.readexactly(payload_length + 11) data.extend(await self.reader.readexactly(payload_length + 11))
data = endian_byte + payload_length_word + remaining_data
self.receive_cb(data) self.receive_cb(data)
except Exception: except Exception:
logger.error("analyzer receiver connection terminating with exception", exc_info=True) logger.error("analyzer receiver connection terminating with exception", exc_info=True)