def serialize(dec): sign, digits, exponent = dec.as_tuple() unscaled = int(''.join([str(digit) for digit in digits])) if sign: unscaled *= -1 scale = int32_pack(-exponent) unscaled = varint_pack(unscaled) return scale + unscaled
def send(self, f, streamid, compression=False): body = StringIO() self.send_body(body) body = body.getvalue() version = PROTOCOL_VERSION | HEADER_DIRECTION_FROM_CLIENT flags = 0 # no compression supported yet msglen = int32_pack(len(body)) header = '%c%c%c%c%s' % (version, flags, streamid, self.opcode, msglen) f.write(header) if len(body) > 0: f.write(body)
def send(self, f, streamid, compression=None): body = StringIO() self.send_body(body) body = body.getvalue() version = PROTOCOL_VERSION | HEADER_DIRECTION_FROM_CLIENT flags = 0 if compression is not None and len(body) > 0: body = compression(body) flags |= 0x1 msglen = int32_pack(len(body)) header = ''.join(map(int8_pack, (version, flags, streamid, self.opcode))) \ + msglen f.write(header) if len(body) > 0: f.write(body)
def write_int(f, i): f.write(int32_pack(i))