plestylib.traffic.utils#

Socket read helpers for mixed protocol payloads.

This module provides robust read utilities that support: - SCPI definite-length binary blocks (#<n><len><payload>) - JSON-style messages starting with { - Line-style messages terminated by a caller-provided delimiter

Functions#

_recv_exact(→ bytes)

Receive exactly n bytes or raise error.

sock_smart_read(→ bytes)

Read one message from a socket using protocol-aware framing.

Module Contents#

plestylib.traffic.utils._recv_exact(sock: socket.socket, n: int) bytes#

Receive exactly n bytes or raise error.

Parameters:
  • sock (socket.socket)

  • n (int)

Return type:

bytes

plestylib.traffic.utils.sock_smart_read(sock: socket.socket, terminator: bytes, max_size: int = 10000000) bytes#

Read one message from a socket using protocol-aware framing.

The function inspects the first byte and chooses a read strategy: - b'#': parse SCPI definite-length binary block and return payload only - b'{': read a JSON-like message until trailing b'}' - otherwise: read line-style data until terminator is observed

Parameters:
  • sock (socket.socket) – Connected socket object.

  • terminator (bytes) – Byte delimiter used for line-style messages.

  • max_size (int) – Maximum allowed message/payload size in bytes.

Returns:

Bytes read from the socket according to detected framing.

Raises:
  • ValueError – If framing is malformed or payload exceeds max_size.

  • TimeoutError – If the socket times out while reading.

  • RuntimeError – If a non-blocking socket has no data.

  • ConnectionError – If the connection closes unexpectedly or OS socket errors occur.

Return type:

bytes