register static method

void register(
  1. Protocol protocol
)

Adds protocol to the registry, so that MultiAddr can parse, print and encode addresses that use it.

A transport in another package calls this for a protocol that dart_libp2p does not know. The protocol must have no value (size 0) or a variable-length string value (size -1); a fixed-size value needs a codec that only dart_libp2p can supply.

Registering an equal protocol again does nothing. Throws ArgumentError if a different protocol already has the same name or code, or if the protocol has a fixed-size value.

Implementation

static void register(Protocol protocol) {
  if (protocol.size > 0) {
    throw ArgumentError('Cannot register ${protocol.name}: fixed-size values are not supported');
  }
  final byNameMatch = _protocols[protocol.name];
  if (byNameMatch != null) {
    if (byNameMatch.code == protocol.code &&
        byNameMatch.size == protocol.size &&
        byNameMatch.path == protocol.path) {
      return;
    }
    throw ArgumentError('Protocol name ${protocol.name} is already registered with code ${byNameMatch.code}');
  }
  final byCodeMatch = byCode(protocol.code);
  if (byCodeMatch != null) {
    throw ArgumentError('Protocol code ${protocol.code} is already registered as ${byCodeMatch.name}');
  }
  _protocols[protocol.name] = protocol;
}