Write a framed message: 4-byte big-endian length + JSON payload
(writer: asyncio.StreamWriter, payload: dict[str, Any])
| 285 | |
| 286 | |
| 287 | async def write_frame(writer: asyncio.StreamWriter, payload: dict[str, Any]) -> None: |
| 288 | """Write a framed message: 4-byte big-endian length + JSON payload""" |
| 289 | payload_bytes = json.dumps(payload, ensure_ascii=False).encode("utf-8") |
| 290 | length = len(payload_bytes) |
| 291 | |
| 292 | if length > MAX_PAYLOAD_BYTES: |
| 293 | raise ValueError(f"Payload too large: {length} > {MAX_PAYLOAD_BYTES}") |
| 294 | |
| 295 | header = struct.pack(">I", length) # 4-byte big-endian unsigned int |
| 296 | writer.write(header + payload_bytes) |
| 297 | await writer.drain() |
| 298 | |
| 299 | |
| 300 | async def read_frame(reader: asyncio.StreamReader) -> dict[str, Any]: |
no outgoing calls
no test coverage detected