deliverWindowUpdate method

void deliverWindowUpdate(
  1. int windowSize
)

Delivers WINDOW_UPDATE from the socket.

windowSize is an ABSOLUTE OFFSET: the highest cumulative byte position the peer will accept on this stream, not a count of bytes that may be outstanding. Both implementations already advertise it this way — see _deliverDataInternal below, which sends _receiveWindow as initialWindow + cumulative bytes received.

The value arrives modulo 2^32 because the frame field is a uint32, so recover the full offset by choosing the candidate nearest the limit we already hold (RFC 1982 serial-number arithmetic). This is unambiguous because the true offset is always within one receive window of the current limit, and the window is orders of magnitude below 2^32.

Implementation

void deliverWindowUpdate(int windowSize) {
  const modulus = 1 << 32;
  final base = _remoteReceiveWindow & ~(modulus - 1);
  var candidate = base + windowSize;
  for (final alt in [candidate - modulus, candidate + modulus]) {
    if ((alt - _remoteReceiveWindow).abs() <
        (candidate - _remoteReceiveWindow).abs()) {
      candidate = alt;
    }
  }
  // Monotonic: a stale or reordered frame must never revoke granted credit.
  if (candidate > _remoteReceiveWindow) {
    _remoteReceiveWindow = candidate;
  }

  if (_drain != null && !_drain!.isCompleted) {
    final connWindowAvailable = _socket?.getAvailableConnectionSendWindow() ?? 0;
    if (inflight < cwnd &&
        bytesWritten < _remoteReceiveWindow &&
        connWindowAvailable > 0) {
      _drain!.complete();
    }
  }
  emit('drain');
}