createIncoming static method

UDXStream createIncoming(
  1. UDX udx,
  2. UDPSocket socket,
  3. int localId,
  4. int remoteId,
  5. String host,
  6. int port, {
  7. required ConnectionId destinationCid,
  8. required ConnectionId sourceCid,
  9. bool framed = false,
  10. int initialSeq = 0,
  11. int? initialCwnd,
  12. bool firewall(
    1. UDPSocket socket,
    2. int port,
    3. String host
    )?,
  13. StreamType streamType = StreamType.bidirectional,
})

Implementation

static UDXStream createIncoming(
  UDX udx,
  UDPSocket socket,
  int localId,
  int remoteId,
  String host,
  int port, {
  required ConnectionId destinationCid,
  required ConnectionId sourceCid,
  bool framed = false,
  int initialSeq = 0,
  int? initialCwnd,
  bool Function(UDPSocket socket, int port, String host)? firewall,
  StreamType streamType = StreamType.bidirectional,
}) {
  if (socket.closing) {
    throw StateError('UDXStream.createIncoming: Socket is closing');
  }

  final stream = UDXStream(
    udx,
    localId,
    streamType: streamType,
    isInitiator: false,
    framed: framed,
    initialSeq: initialSeq,
    firewall: firewall,
  );
  stream._socket = socket;
  stream.remoteId = remoteId;
  stream.remoteHost = host;
  stream.remotePort = port;
  stream.remoteFamily = UDX.getAddressFamily(host);
  socket.registerStream(stream);
  stream._remoteConnectionWindowUpdateSubscription?.cancel();
  stream._remoteConnectionWindowUpdateSubscription = stream._socket!.on('remoteConnectionWindowUpdate').listen(stream._handleRemoteConnectionWindowUpdate);
  stream._connected = true;
  stream.connectedAt = DateTime.now();

  // Send SYN-ACK via socket's connection-level packet manager
  // The SYN has already been received and processed by the socket.
  // We send our SYN back to establish the bidirectional stream.
  socket.sendStreamPacket(
    remoteId,
    localId,
    [StreamFrame(data: Uint8List(0), isSyn: true)],
  );

  if (!socket.closing) {
    socket.sendMaxDataFrame(UDPSocket.defaultInitialConnectionWindow);
  }

  stream.emit('accepted');
  return stream;
}