mirror of
https://github.com/esphome/aioesphomeapi.git
synced 2025-02-03 23:42:05 +01:00
Reduce overhead to process packets (#503)
This commit is contained in:
parent
3432477c20
commit
2df4effef3
@ -721,10 +721,11 @@ class APIConnection:
|
|||||||
def _process_packet(msg_type_proto: int, data: bytes) -> None:
|
def _process_packet(msg_type_proto: int, data: bytes) -> None:
|
||||||
"""Process a packet from the socket."""
|
"""Process a packet from the socket."""
|
||||||
try:
|
try:
|
||||||
# python 3.11 has near zero cost exception handling
|
msg = message_type_to_proto[msg_type_proto]()
|
||||||
# if we do not raise which is almost never expected
|
# MergeFromString instead of ParseFromString since
|
||||||
# so we can just use a try/except here
|
# ParseFromString will clear the message first and
|
||||||
class_ = message_type_to_proto[msg_type_proto]
|
# the msg is already empty.
|
||||||
|
msg.MergeFromString(data)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"%s: Skipping message type %s",
|
"%s: Skipping message type %s",
|
||||||
@ -732,13 +733,6 @@ class APIConnection:
|
|||||||
msg_type_proto,
|
msg_type_proto,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
msg = class_()
|
|
||||||
try:
|
|
||||||
# MergeFromString instead of ParseFromString since
|
|
||||||
# ParseFromString will clear the message first and
|
|
||||||
# the msg is already empty.
|
|
||||||
msg.MergeFromString(data)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.info(
|
_LOGGER.info(
|
||||||
"%s: Invalid protobuf message: type=%s data=%s: %s",
|
"%s: Invalid protobuf message: type=%s data=%s: %s",
|
||||||
|
Loading…
Reference in New Issue
Block a user