def read_varint32(sock):
mask = (1 << 32) - 1
result = 0
shift = 0
while True:
unpacker = struct.Struct('! B')
data = sock.recv(unpacker.size, socket.MSG_WAITALL)
(b,) = unpacker.unpack(data)
result |= ((b & 0x7f) << shift)
if not (b & 0x80):
result &= mask
return result
shift += 7
if shift >= 64:
raise IOError("Too many bytes when decoding varint.")
评论列表
文章目录