Encode a message for transmission. Args: data: Dictionary to encode Returns: Length-prefixed JSON bytes
(data: dict)
| 40 | |
| 41 | @staticmethod |
| 42 | def encode_message(data: dict) -> bytes: |
| 43 | """ |
| 44 | Encode a message for transmission. |
| 45 | |
| 46 | Args: |
| 47 | data: Dictionary to encode |
| 48 | |
| 49 | Returns: |
| 50 | Length-prefixed JSON bytes |
| 51 | """ |
| 52 | payload = json.dumps(data).encode('utf-8') |
| 53 | length = struct.pack('>I', len(payload)) |
| 54 | return length + payload |
| 55 | |
| 56 | @staticmethod |
| 57 | def decode_message(data: bytes) -> dict: |