18from .startup
import start_thread, send_gdb, log
22 """Read a JSON-RPC message from STREAM.
23 The decoded object is returned."""
27 line = stream.readline()
31 if line.startswith(b
"Content-Length:"):
32 line = line[15:].strip()
33 content_length = int(line)
35 log(
"IGNORED: <<<%s>>>" % line)
37 while len(data) < content_length:
38 new_data = stream.read(content_length - len(data))
40 result = json.loads(data)
44def start_json_writer(stream, queue):
45 """Start the JSON writer thread.
46 It will read objects from QUEUE and write them to STREAM,
47 following the JSON-RPC protocol."""
61 encoded = json.dumps(obj)
62 body_bytes = encoded.encode(
"utf-8")
63 header =
"Content-Length: " + str(len(body_bytes)) +
"\r\n\r\n"
64 header_bytes = header.encode(
"ASCII")
65 stream.write(header_bytes)
66 stream.write(body_bytes)
69 start_thread(
"JSON writer", _json_writer)