createSocket method

UDPSocket createSocket(
  1. UDX udx,
  2. String host,
  3. int port, {
  4. ConnectionId? localCid,
  5. ConnectionId? remoteCid,
})

Creates or retrieves a UDPSocket for a given peer.

Implementation

UDPSocket createSocket(
  UDX udx,
  String host,
  int port, {
  ConnectionId? localCid,
  ConnectionId? remoteCid,
}) {
  final peerKey = '$host:$port';
  if (socketsByPeer.containsKey(peerKey)) {
    return socketsByPeer[peerKey]!;
  }

  // For now, we'll create a new socket for each peer.
  // A more advanced implementation would handle CID reuse.
  final remoteAddress = InternetAddress(host);
  final effectiveLocalCid = localCid ?? ConnectionId.random();
  final effectiveRemoteCid = remoteCid ?? ConnectionId.random();
  final cids = ConnectionCids(effectiveLocalCid, effectiveRemoteCid);

  final newSocket = UDPSocket(
    udx: udx,
    multiplexer: this,
    remoteAddress: remoteAddress,
    remotePort: port,
    cids: cids,
  );

  socketsByCid[effectiveLocalCid] = newSocket;
  socketsByPeer[peerKey] = newSocket;
  return newSocket;
}