createSocket method

UDPSocket createSocket(
  1. UDX udx,
  2. String host,
  3. int port, {
  4. ConnectionId? localCid,
  5. ConnectionId? remoteCid,
  6. bool isServer = false,
  7. bool shared = true,
})

Creates or retrieves a UDPSocket for a given peer.

For dialing: when shared is true (the default), a second call for the same host:port returns the first socket. Inbound connections don't go through this; each one gets its own socket, keyed by its connection ID.

When shared is false, the call always makes a new socket with its own connection ID, and later calls do not get it back. Use this when each dial must be a separate connection, as in go-udx: two callers that share one socket also share its close, so a caller that gives up on its dial and closes the socket also closes the other caller's connection.

Implementation

UDPSocket createSocket(
  UDX udx,
  String host,
  int port, {
  ConnectionId? localCid,
  ConnectionId? remoteCid,
  bool isServer = false,
  bool shared = true,
}) {
  if (shared) {
    final existing = socketsByPeer['$host:$port'];
    if (existing != null) return existing;
  }
  return _newSocket(udx, host, port,
      localCid: localCid, remoteCid: remoteCid, isServer: isServer, shared: shared);
}