handleIncomingDatagramForTest method

void handleIncomingDatagramForTest(
  1. Uint8List data,
  2. InternetAddress address,
  3. int port
)

Implementation

void handleIncomingDatagramForTest(
    Uint8List data, InternetAddress address, int port) {
  // This is a simplified version of the logic in _listen for test purposes
  if (data.length < 28) return;

  final destinationCid = ConnectionId(data.sublist(0, 8));
  final socketConnection = socketsByCid[destinationCid];

  if (socketConnection != null) {
    socketConnection.handleIncomingDatagram(data, address, port);
  } else {
    try {
      final packet = UDXPacket.fromBytes(data);
      final isNewConnection =
          packet.frames.whereType<StreamFrame>().any((frame) => frame.isSyn);

      if (isNewConnection) {
        final newSocket = createSocket(
          UDX(),
          address.address,
          port,
          localCid: destinationCid, // Use the CID from the SYN packet
          remoteCid: packet.sourceCid,
        );
        // THE FIX: Map the temporary destination CID to the new socket
        // to handle retransmissions during the handshake.
        socketsByCid[destinationCid] = newSocket;
        _connectionsController.add(newSocket);
        newSocket.handleIncomingDatagram(data, address, port);
      }
    } catch (e) {
      // Ignore
    }
  }
}