-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
55 lines (41 loc) · 1.52 KB
/
protocol.py
File metadata and controls
55 lines (41 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json
import socket
import struct
from typing import Any, Dict, Optional
PROTO_VERSION = 1
class ProtocolError(Exception):
pass
def send_json(sock: socket.socket, obj: Dict[str, Any]) -> None:
data = json.dumps(obj, separators=(",", ":")).encode("utf-8")
header = struct.pack(">I", len(data))
sock.sendall(header + data)
def _recv_exact(sock: socket.socket, n: int) -> bytes:
chunks = []
remaining = n
while remaining > 0:
chunk = sock.recv(remaining)
if not chunk:
raise ProtocolError("Connection closed")
chunks.append(chunk)
remaining -= len(chunk)
return b"".join(chunks)
def recv_json(sock: socket.socket, *, max_bytes: int = 1024 * 1024) -> Dict[str, Any]:
header = _recv_exact(sock, 4)
(length,) = struct.unpack(">I", header)
if length <= 0:
raise ProtocolError("Invalid frame length")
if length > max_bytes:
raise ProtocolError(f"Frame too large ({length} > {max_bytes})")
payload = _recv_exact(sock, length)
try:
obj = json.loads(payload.decode("utf-8", errors="strict"))
except Exception as e:
raise ProtocolError(f"Invalid JSON: {e}") from None
if not isinstance(obj, dict):
raise ProtocolError("Message must be a JSON object")
return obj
def make_hello(token: Optional[str], client_name: str) -> Dict[str, Any]:
msg: Dict[str, Any] = {"type": "hello", "proto": PROTO_VERSION, "client": client_name}
if token is not None:
msg["token"] = token
return msg