

def check_buffer(buf, namespace=False):
    "\n    Given a mutable bytearray, see if it begins with a complete message.\n    If it does, parse it out and return (connid, message, content).\n    (As an integer, bytes, decoded dict.) The message chunk is then sliced\n    out of the buffer.\n\n    If namespace is true, the content object will wind up as a\n    SimpleNamespace instead of a dict. (That is, you will be able to\n    do obj.foo instead of obj['foo']. But any keys that are not keyable\n    will have to be read using getattr().)\n\n    If the content fails to parse, this throws an exception, but the\n    message will still be sliced out of the buffer.\n    "
    if (len(buf) < HEADER_LENGTH):
        return None
    (datlen,) = struct.unpack('<1I', buf[0:4])
    msglen = (datlen + HEADER_LENGTH)
    if (len(buf) < msglen):
        return None
    (connid,) = struct.unpack('<1I', buf[4:8])
    msgdat = buf[HEADER_LENGTH:msglen]
    buf[0:msglen] = b''
    object_hook = (namespace_wrapper if namespace else None)
    msgstr = msgdat.decode()
    msgobj = json.loads(msgstr, object_hook=object_hook)
    if (type(msgobj) not in [dict, types.SimpleNamespace]):
        raise ValueError('Message was not an object')
    return (connid, msgdat, msgobj)

dataset/ETHPy150Open erkyrath/tworld/lib/twcommon/wcproto.py check_buffer/original