grpcEncodeFrameWithFlags function
Encode a frame with an explicit flags byte (1 flag + 4-byte big-endian
length + message bytes).
This is the flag-byte-aware sibling of grpcEncodeFrame: instead of the
single compressed bool it takes the full flag bitset, so a Connect
streaming end-of-stream envelope can set grpcFlagEndOfStream (bit 1). Build
flags with grpcMakeFlags. grpcEncodeFrame(bytes, compressed: c) is
exactly grpcEncodeFrameWithFlags(bytes, c ? grpcFlagCompressed : 0).
Implementation
List<int> grpcEncodeFrameWithFlags(List<int> messageBytes, int flags) {
var length = messageBytes.length;
var frame = List<int>.filled(_headerSize + length, 0);
// Byte 0: the full flag bitset (compression bit 0, end-of-stream bit 1).
frame[0] = flags & 0xFF;
// Bytes 1-4: message length, big-endian.
frame[1] = (length >>> 24) & 0xFF;
frame[2] = (length >>> 16) & 0xFF;
frame[3] = (length >>> 8) & 0xFF;
frame[4] = length & 0xFF;
// Bytes 5+: message payload.
for (var i = 0; i < length; i++) {
frame[_headerSize + i] = messageBytes[i];
}
return frame;
}