add method
Enqueue data for sending.
Returns a future that completes when this message has been sent (or fails
if the send fails). Throws OutboundBufferOverflow synchronously — and
closes the queue — if accepting the message would exceed maxBufferSize.
Implementation
Future<void> add(List<int> data) {
if (_closed) {
throw StateError('OutboundQueue is closed');
}
final projected = _pendingBytes + data.length;
if (projected > _maxBufferSize) {
_closed = true;
throw OutboundBufferOverflow(
attemptedBytes: projected,
maxBufferSize: _maxBufferSize,
);
}
_pendingBytes += data.length;
final future = _tail.then((_) {
// A later overflow may have closed the queue while this message waited;
// drop it silently in that case (the connection is being torn down).
if (_closed) {
return null;
}
return _onSend(data);
}).whenComplete(() {
_pendingBytes -= data.length;
});
// Keep the chain alive even if this send fails.
_tail = future.then((_) {}, onError: (_) {});
return future;
}