Read a complete message from socket (sync). Args: sock: Socket to read from Returns: dict: Message dict with 'type', 'id', and payload fields Raises: DirtyProtocolError: If read fails or message is malformed
(sock: socket.socket)
| 518 | |
| 519 | @staticmethod |
| 520 | def read_message(sock: socket.socket) -> dict: |
| 521 | """ |
| 522 | Read a complete message from socket (sync). |
| 523 | |
| 524 | Args: |
| 525 | sock: Socket to read from |
| 526 | |
| 527 | Returns: |
| 528 | dict: Message dict with 'type', 'id', and payload fields |
| 529 | |
| 530 | Raises: |
| 531 | DirtyProtocolError: If read fails or message is malformed |
| 532 | """ |
| 533 | # Read header |
| 534 | header = BinaryProtocol._recv_exactly(sock, HEADER_SIZE) |
| 535 | msg_type, request_id, length = BinaryProtocol.decode_header(header) |
| 536 | |
| 537 | # Read payload |
| 538 | if length > 0: |
| 539 | payload_data = BinaryProtocol._recv_exactly(sock, length) |
| 540 | try: |
| 541 | payload_dict = TLVEncoder.decode_full(payload_data) |
| 542 | except DirtyProtocolError: |
| 543 | raise |
| 544 | except Exception as e: |
| 545 | raise DirtyProtocolError( |
| 546 | f"Failed to decode TLV payload: {e}", |
| 547 | raw_data=payload_data[:50] |
| 548 | ) |
| 549 | else: |
| 550 | payload_dict = {} |
| 551 | |
| 552 | # Build response dict |
| 553 | msg_type_str = MSG_TYPE_TO_STR[msg_type] |
| 554 | result = {"type": msg_type_str, "id": request_id} |
| 555 | result.update(payload_dict) |
| 556 | |
| 557 | return result |
| 558 | |
| 559 | @staticmethod |
| 560 | def write_message(sock: socket.socket, message: dict) -> None: |
no test coverage detected