Skip to content

Commit 8309ed2

Browse files
committed
extract UDP fast path type checking to peek_packet_type
1 parent ab9370e commit 8309ed2

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

libqretprop/DeviceControllers/deviceTools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
StreamStartPacket,
2222
decode_packet_server,
2323
get_packet_len,
24+
peek_packet_type,
2425
)
2526

2627

@@ -215,7 +216,7 @@ async def udpListener() -> None:
215216
if isinstance(device, SensorMonitor):
216217
# Fast-path for DATA packets (100% of UDP traffic):
217218
# Check packet type from raw bytes without full decode_packet.
218-
packet_type = data[1] # Important: this relies on header structure outside of decode_packet
219+
packet_type = peek_packet_type(data)
219220
if packet_type == PacketType.DATA:
220221
# Only decode if already verified as DATA packet
221222
packet = decode_packet_server(data)

libqretprop/protocol.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ def get_packet_len(data: bytes) -> int:
5353
_check(_lib.qlcp_get_packet_len(data_len, buf, len(data)), "get_packet_len")
5454
return int(data_len[0])
5555

56+
57+
def peek_packet_type(data: bytes) -> int:
58+
"""Peek the packet-type byte from a raw packet header.
59+
60+
Accesses the packet type from raw bytes for fast-path packet type checks without a full decode
61+
62+
Raises `QLCPError` if the provided buffer is too short to contain that byte.
63+
"""
64+
if len(data) < 2:
65+
raise QLCPError(f"packet too small to peek type: {len(data)} bytes")
66+
# In QLCP v2 the packet-type is the second byte of the header
67+
return data[1]
68+
5669
# ============================================================================
5770
# ENUMS
5871
# ============================================================================

0 commit comments

Comments
 (0)