in project/alcatraz/alcatraz/clusters/_container_proc.py [0:0]
def _demux_multiplexed_stream(read_sock: socket.socket) -> tuple[int | None, bytes]:
"""
Demultiplex the Docker multiplexed stream by reading the header off the
socket to get the fd and payload size, then read the payload.
"""
# Read the 8-byte header
header = read_sock.recv(8)
if len(header) == 0:
# Connection closed
return None, b""
if len(header) < 8:
# Incomplete header
raise Exception("Incomplete header")
stream_type = header[0]
payload_size = struct.unpack(">I", header[4:8])[0]
# Read the payload
payload = b""
while len(payload) < payload_size:
chunk = read_sock.recv(payload_size - len(payload))
if not chunk:
break
payload += chunk
return stream_type, payload