allocateIncomingStreamId method

int allocateIncomingStreamId(
  1. int requested
)

The local id for a stream the peer is opening. A peer that names one (dart-libp2p picks both ids of its first stream) gets it if it's free. go-udx and js-udx never learn our id for a stream they open: they address it to 0 throughout and identify it by their own id. Those, and any id already taken, get the next free even id, as a go-udx acceptor would.

Implementation

int allocateIncomingStreamId(int requested) {
  if (requested != 0 && !_registeredStreams.containsKey(requested)) return requested;
  while (_nextIncomingStreamId == 0 || _registeredStreams.containsKey(_nextIncomingStreamId)) {
    _nextIncomingStreamId = (_nextIncomingStreamId + 2) & 0xFFFFFFFF;
  }
  final id = _nextIncomingStreamId;
  _nextIncomingStreamId = (id + 2) & 0xFFFFFFFF;
  return id;
}