connect method

Future<void> connect(
  1. UDPSocket socket,
  2. int remoteId,
  3. int port,
  4. String host,
)

Connects the stream to a remote endpoint

Implementation

Future<void> connect(
  UDPSocket socket,
  int remoteId,
  int port,
  String host,
) async {
  if (_connected) throw StateError('Stream is already connected');
  if (socket.closing) throw StateError('Socket is closing');

  _socket = socket;
  this.remoteId = remoteId;
  this.remoteHost = host;
  this.remotePort = port;
  this.remoteFamily = UDX.getAddressFamily(host);
  _connected = true;

  socket.registerStream(this);
  _remoteConnectionWindowUpdateSubscription?.cancel();
  _remoteConnectionWindowUpdateSubscription = _socket!.on('remoteConnectionWindowUpdate').listen(_handleRemoteConnectionWindowUpdate);
  ////print('[UDXStream ${this.id} Subscribing to remoteConnectionWindowUpdate on socket: ${_socket.hashCode}]');

  packetManager.onRetransmit = (packet) {
    //print('UDXStream ${this.id}: onRetransmit called for packet ${packet.sequence}');
    if (!_connected || _socket == null || remotePort == null || remoteHost == null) {
      //print('UDXStream ${this.id}: Cannot retransmit - not connected or missing socket/remote info');
      return;
    }
    if (_socket!.closing) {
      //print('UDXStream ${this.id}: Socket is closing, skipping packet retransmission.');
      return;
    }
    //print('UDXStream ${this.id}: Sending retransmitted packet ${packet.sequence}');
    _socket!.send(packet.toBytes());
  };

  packetManager.onSendProbe = (packet) {
    if (!_connected || _socket == null || remotePort == null || remoteHost == null) return;
    if (_socket!.closing) {
      ////print('[UDXStream ${this.id}.onSendProbe] Socket is closing, skipping probe packet send.');
      return;
    }
    _socket!.send(packet.toBytes());
  };

  emit('connect');
}