def read_frame(f, decompressor=None): header = f.read(8) version, flags, stream, opcode = \ [int.from_bytes(b.to_bytes(1, 'big'), 'big', signed=True) for b in header[:4]] body_len = int32_unpack(header[4:]) assert version & PROTOCOL_VERSION_MASK == PROTOCOL_VERSION, \ "Unsupported CQL protocol version %d" % version assert version & HEADER_DIRECTION_MASK == HEADER_DIRECTION_TO_CLIENT, \ "Unexpected request from server with opcode %04x, stream id %r" \ % (opcode, stream) assert body_len >= 0, "Invalid CQL protocol body_len %r" % body_len body = f.read(body_len) while body_len != len(body): #TODO: WHYYYYYYYY body += f.read(body_len - len(body)) if flags & 0x1: if decompressor is None: raise ProtocolException("No decompressor available for " "compressed frame!") body = decompressor(body) flags ^= 0x1 if flags: warn("Unknown protocol flags set: %02x. May cause problems." % flags) msgclass = _message_types_by_opcode[opcode] msg = msgclass.recv_body(BytesIO(body)) msg.stream_id = stream return msg
def read_frame(f): header = f.read(8) version, flags, stream, opcode = map(ord, header[:4]) body_len = int32_unpack(header[4:]) assert version & PROTOCOL_VERSION_MASK == PROTOCOL_VERSION, \ "Unsupported CQL protocol version %d" % version assert version & HEADER_DIRECTION_MASK == HEADER_DIRECTION_TO_CLIENT, \ "Unexpected request from server with opcode %04x, stream id %r" % (opcode, stream) assert body_len >= 0, "Invalid CQL protocol body_len %r" % body_len if flags: warn("Unknown protocol flags set: %02x. May cause problems." % flags) body = f.read(body_len) msgclass = _message_types_by_opcode[opcode] msg = msgclass.recv_body(StringIO(body)) msg.stream_id = stream return msg
def read_frame(f, decompressor=None): header = f.read(8) version, flags, stream, opcode = map(int8_unpack, header[:4]) body_len = int32_unpack(header[4:]) assert version & PROTOCOL_VERSION_MASK == PROTOCOL_VERSION, \ "Unsupported CQL protocol version %d" % version assert version & HEADER_DIRECTION_MASK == HEADER_DIRECTION_TO_CLIENT, \ "Unexpected request from server with opcode %04x, stream id %r" % (opcode, stream) assert body_len >= 0, "Invalid CQL protocol body_len %r" % body_len body = f.read(body_len) if flags & 0x1: if decompressor is None: raise ProtocolException("No decompressor available for compressed frame!") body = decompressor(body) flags ^= 0x1 if flags: warn("Unknown protocol flags set: %02x. May cause problems." % flags) msgclass = _message_types_by_opcode[opcode] msg = msgclass.recv_body(StringIO(body)) msg.stream_id = stream return msg
def read_int(f): return int32_unpack(f.read(4))
def deserialize(byts): scale = int32_unpack(byts[:4]) unscaled = varint_unpack(byts[4:]) return Decimal('%de%d' % (unscaled, -scale))