close method

Future<void> close()

Closes the RelayManager, stopping the relay service and cleaning up resources.

Implementation

Future<void> close() async {
  if (_isClosed) return;
  _isClosed = true;
  _log.fine('Closing RelayManager...');

  // Cancel the stream subscription
  await _reachabilityStreamSubscription?.cancel();
  _reachabilityStreamSubscription = null;
  _log.fine('Cancelled reachability stream subscription.');

  // Close the EventBus subscription
  await _eventBusSubscription?.close();
  _eventBusSubscription = null;
  _log.fine('Closed EventBus subscription for reachability.');

  await _lock.synchronized(() async {
    if (_activeRelay != null) {
      _log.fine('Closing active Circuit Relay v2 service.');
      try {
        await _activeRelay!.close();
      } catch (e,s) {
          _log.warning('Error closing active Circuit Relay during RelayManager close.', e, s);
      }
      _activeRelay = null;
    }
  });

  if (_backgroundCompleter != null && !_backgroundCompleter!.isCompleted) {
      _backgroundCompleter!.complete();
  }
  _log.fine('RelayManager closed.');
}