close method

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

Close shuts down the host, its Network, and services.

Implementation

@override
Future<void> close() async {
  if (_closed) return;

  _closeSync.complete(); // Signal that close has been initiated
  _closed = true;

  // Close IDService
  await _idService.close();

  // If PingService was initialized (i.e., enabled), remove its handler.
  if (_pingService != null) {
    // Assuming PingConstants.protocolId is accessible or we use the string directly.
    // For now, let's use the string directly as PingConstants is not imported here.
    // Ideally, PingConstants.protocolId would be exposed or PingService would have a static getter.
    removeStreamHandler('/ipfs/ping/1.0.0');
  }

  // Close RelayManager if initialized
  await _relayManager?.close();

  // Close AutoNATService if initialized
  await _autoNATService?.close();

  // Close HolePunchService if initialized
  await _holePunchService?.close();

  // Close NAT manager if available
  if (_natmgr != null) {
    await _natmgr!.close();
  }

  // Close connection manager
  await _cmgr.close();

  // Close address change channel
  await _addrChangeChan.close();

  // Cancel the address monitor timer
  _addressMonitorTimer?.cancel();

  // Close network
  await _network.close();

  // Close peerstore
  await peerStore.close();
}