connect method

  1. @override
Future<void> connect()
override

Implementation

@override
Future<void> connect() async {
  if (_socket != null) return;

  _reader.reset();

  final Duration connectTimeout = Duration(seconds: info.connectTimeoutSeconds);

  _socket = info.ssl
      ? await SecureSocket.connect(
          info.hostIp,
          info.hostPort,
          context: _buildSecurityContext(),
          timeout: connectTimeout,

          // This is ALPN, NOT TLS version selection.
          supportedProtocols: const <String>["http/1.1"],

          // Java's custom trust store does not perform normal hostname
          // verification in the same way Dart's TLS implementation does.
          //
          // Only accept the certificate if it is exactly the certificate
          // configured in sslPublicKey.
          onBadCertificate: _validateServerCertificate,
        )
      : await Socket.connect(
          info.hostIp,
          info.hostPort,
          timeout: connectTimeout,
        );

  _socket!.setOption(SocketOption.tcpNoDelay, true);

  IsoTrace.log(
    IsoTraceKind.link,
    "socket open, local "
    "${_socket!.address.address}:${_socket!.port} -> "
    "${_socket!.remoteAddress.address}:${_socket!.remotePort}",
  );

  _subscription = _socket!.listen(
    (Uint8List chunk) {
      for (final Uint8List frame in _reader.add(chunk)) {
        _incoming.add(frame);
      }
    },
    onError: (Object error, StackTrace stackTrace) {
      IsoTrace.log(
        IsoTraceKind.link,
        "socket error: $error",
      );

      _incoming.addError(
        IsoTransportException("socket error", error),
        stackTrace,
      );

      unawaited(close());
    },
    onDone: () {
      IsoTrace.log(
        IsoTraceKind.link,
        "socket closed by the host",
      );

      unawaited(close());
    },
    cancelOnError: true,
  );
}