connect method

Future<DtlsConnection> connect(
  1. InternetAddress address,
  2. int port,
  3. DtlsClientContext context, {
  4. String? hostname,
  5. Duration? timeout,
})

Establishes a DtlsConnection with a peer using the given address and port.

If a DtlsConnection to a peer with the given address and port already exists, that connection will be reused instead of opening a new one. If you want to establish a connection using different credentials, then you need to close the old connection first.

If a timeout duration is defined, a OpenSslDtlsTimeoutException will be thrown if no connection could be established within the given time period.

Implementation

Future<DtlsConnection> connect(
  InternetAddress address,
  int port,
  DtlsClientContext context, {
  String? hostname,
  Duration? timeout,
}) async {
  final existingConnection = _retrieveConnection(address, port);

  if (existingConnection == null) {
    return _DtlsClientConnection._connect(
      this,
      hostname,
      address,
      port,
      context._generateSslContext(_libSsl, _libCrypto),
      context._pskCredentialsCallback,
      _libCrypto,
      _libSsl,
      timeout: timeout,
    );
  }

  if (!existingConnection.connected) {
    throw StateError(
      "Client connection to peer "
      "${existingConnection._address}:${existingConnection._port} "
      "has not been properly cleaned up.",
    );
  }

  return existingConnection;
}