getAddrKeyAndPort function

_AddrKeyAndPort getAddrKeyAndPort(
  1. MultiAddr a
)

Implementation

_AddrKeyAndPort getAddrKeyAndPort(MultiAddr a) {
  String key = '';
  int port = 0;

  for (var component in a.components) {
    final protocol = component.$1; // Protocol object
    final value = component.$2;   // String value of the component
    final pCode = protocol.code;

    if (pCode == Protocols.tcp.code || pCode == Protocols.udp.code) {
      try {
        port = int.parse(value);
      } catch (e) {
        // Handle parsing error if value is not a valid port number string
        // For now, keep port as 0 or throw, depending on desired strictness.
        // Example: log.warning('Could not parse port: $value for ${protocol.name}');
      }
      key += '/${protocol.name}'; // Add protocol name, not its value (which is the port)
    } else {
      // Mimic Go: if value is empty, use protocol name. Otherwise, use value.
      String valStr = value.isNotEmpty ? value : protocol.name;
      key += '/$valStr';
    }
  }
  return _AddrKeyAndPort(key, port);
}