readVarint method
Read a varint from the stream (for length-delimited messages).
Implementation
Future<int> readVarint() async {
final bytes = <int>[];
var i = 0;
while (true) {
final byte = await readByte();
bytes.add(byte);
if (byte & 0x80 == 0) {
break;
}
if (i++ > 9) {
throw Exception('varint too long');
}
}
// Decode varint
var value = 0;
var shift = 0;
for (var byte in bytes) {
value |= (byte & 0x7F) << shift;
shift += 7;
}
return value;
}