Decode the 16-byte message header. Args: data: 16 bytes of header data Returns: tuple: (msg_type, request_id, payload_length) Raises: DirtyProtocolError: If header is invalid
(data: bytes)
| 129 | |
| 130 | @staticmethod |
| 131 | def decode_header(data: bytes) -> tuple: |
| 132 | """ |
| 133 | Decode the 16-byte message header. |
| 134 | |
| 135 | Args: |
| 136 | data: 16 bytes of header data |
| 137 | |
| 138 | Returns: |
| 139 | tuple: (msg_type, request_id, payload_length) |
| 140 | |
| 141 | Raises: |
| 142 | DirtyProtocolError: If header is invalid |
| 143 | """ |
| 144 | if len(data) < HEADER_SIZE: |
| 145 | raise DirtyProtocolError( |
| 146 | f"Header too short: {len(data)} bytes, expected {HEADER_SIZE}", |
| 147 | raw_data=data |
| 148 | ) |
| 149 | |
| 150 | magic, version, msg_type, length, request_id = struct.unpack( |
| 151 | HEADER_FORMAT, data[:HEADER_SIZE] |
| 152 | ) |
| 153 | |
| 154 | if magic != MAGIC: |
| 155 | raise DirtyProtocolError( |
| 156 | f"Invalid magic: {magic!r}, expected {MAGIC!r}", |
| 157 | raw_data=data[:20] |
| 158 | ) |
| 159 | |
| 160 | if version != VERSION: |
| 161 | raise DirtyProtocolError( |
| 162 | f"Unsupported protocol version: {version}, expected {VERSION}", |
| 163 | raw_data=data[:20] |
| 164 | ) |
| 165 | |
| 166 | if msg_type not in MSG_TYPE_TO_STR: |
| 167 | raise DirtyProtocolError( |
| 168 | f"Unknown message type: 0x{msg_type:02x}", |
| 169 | raw_data=data[:20] |
| 170 | ) |
| 171 | |
| 172 | if length > MAX_MESSAGE_SIZE: |
| 173 | raise DirtyProtocolError( |
| 174 | f"Message too large: {length} bytes (max: {MAX_MESSAGE_SIZE})" |
| 175 | ) |
| 176 | |
| 177 | return msg_type, request_id, length |
| 178 | |
| 179 | @staticmethod |
| 180 | def encode_request(request_id: int, app_path: str, action: str, |